-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack_by_Array.cpp
62 lines (50 loc) · 1.14 KB
/
stack_by_Array.cpp
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
#include <iostream>
using namespace std;
template<typename T>
class Stack{
private:
int MaxSize;
int _top;
T *stack;
public:
Stack(int size){
MaxSize = size;
_top = -1;
stack = new T[MaxSize];
}
bool isFull() {return (MaxSize - 1 <= _top);}
bool isEmpty() {return (_top == -1);}
void push(T data){
if (isFull()) printf("Stack is FULL\n");
else stack[++_top] = data;
}
T pop(){
if (isEmpty()) printf("stack is Empty\n");
else return stack[_top--];
}
T top(){
if (isEmpty()) printf("stack is Empty\n");
else return stack[_top];
}
void Print(){
for (int i = 0; i <= _top; i++) cout << i << " ";
cout << "\n";
}
};
int main(){
Stack<int> st(5);
cout << st.isEmpty() << "\n";
cout << st.isFull() << "\n";
cout << st.top() << "\n";
cout << st.pop() << "\n";
st.push(1);
st.push(2);
st.push(3);
st.push(4);
st.push(5);
cout << st.isEmpty() << "\n";
cout << st.isFull() << "\n";
cout << st.top() << "\n";
cout << st.pop() << "\n";
st.Print();
}