-
Notifications
You must be signed in to change notification settings - Fork 4k
Expand file tree
/
Copy pathstackClass.cpp
More file actions
79 lines (69 loc) · 1.34 KB
/
stackClass.cpp
File metadata and controls
79 lines (69 loc) · 1.34 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
70
71
72
73
74
75
76
77
78
79
#include <iostream>
#include<climits>
using namespace std;
template <typename t>
class stack {
t * p;
int top;
int size;
public :
stack(int size = 10){
top = -1;
this -> size = size;
p = new t[size];
}
~stack(){
delete [] p;
}
int getSize(){
return size;
}
int getTop(){
return top;
}
bool isEmpty(){
if (top == -1){
return true;
}else {
return false;
}
}
bool isFull(){
if (top == size - 1){
return true;
}else {
return false;
}
}
void push(int data){
if (isFull()){
t * a = new t[size*2];
int i;
for (i=0;i<size;i++){
a[i] = p[i];
}
top = i;
a[top] = data;
delete [] p;
p = a;
size = size * 2;
}else{
top++;
p[top] = data;
}
}
int pop(){
if (isEmpty()){
cout << "Stack is Empty" << endl;
return 0;
}else {
int x = p[top--];
return x;
}
}
void display(){
for (int i =0; i<top+1;i++){
cout << p[i] << " ";
}cout << endl;
}
};