-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack.cpp
51 lines (50 loc) · 990 Bytes
/
stack.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
#include<iostream>
using namespace std;
template <typename T> class stackN{
private:
int capacity;
T* arr;
int top;
public:
stackN(int sz){
capacity = sz;
arr = new int[sz];
top = -1;
}
bool push(T data);
bool isFull();
bool isEmpty();
T pop();
};
template <typename T> bool stackN<T>::push(T data){
if(!isFull()){
top++;
arr[top]=data;
return true;
}
return false;
}
template <typename T> bool stackN<T>::isFull(){
return top == (capacity-1);
}
template <typename T> bool stackN<T>::isEmpty(){
return top == -1;
}
template <typename T> T stackN<T>::pop(){
if(!isEmpty()){
T temp = arr[top];
top--;
return temp;
}
return NULL;
}
int main(){
stackN<int> stk = stackN<int>(5);
stk.push(5);
stk.push(4);
stk.push(2);
while(stk.isEmpty()==false){
cout<<stk.pop();
}
return 0;
}