-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathArraystack.h
More file actions
69 lines (56 loc) · 1.36 KB
/
Arraystack.h
File metadata and controls
69 lines (56 loc) · 1.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#ifndef __ARRAYSTACK__
#define __ARRAYSTACK__
#include <iostream>
#include <cmath>
template <typename T>
class Arraystack
{
private:
T* _array;
int _top;
int _capacity;
public:
Arraystack(const int& capacity = 10);
~Arraystack();
Arraystack<T>& push(const T& value);
Arraystack<T>& pop();
T get_top() const { return IsEmpty() ? sqrt(-1):_array[_top]; }
bool IsEmpty() const { return _top == -1; }
bool IsFull() const { return _top == _capacity-1; }
int size() const { return _top+1; }
T* get_array() { return _array; } //this method is used to test code!It should NOT appear in release version!
};
template <typename T>
inline
Arraystack<T>::Arraystack(const int& capacity)
: _top(-1), _capacity(capacity)
{
_array = new T[_capacity];
}
template <typename T>
inline
Arraystack<T>::~Arraystack()
{
delete[] _array;
}
template <typename T>
inline
Arraystack<T>& Arraystack<T>::push(const T& value){
if(this->IsFull()){
std::cout << "Arraystack is full!NO MORE PUSH!" << std::endl;
return *this;
}
_array[++_top] = value;
return *this;
}
template <typename T>
inline
Arraystack<T>& Arraystack<T>::pop(){
if(this->IsEmpty()){
std::cout << "Arraystack is empty!NO MORE POP!" << std::endl;
return *this;
}
_top--;
return *this;
}
#endif