-
Notifications
You must be signed in to change notification settings - Fork 0
/
arraystack.c++
52 lines (48 loc) · 982 Bytes
/
arraystack.c++
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;
const int MAX = 100;
class Stack{
private:
int top;
int data[MAX];
public:
Stack(){
top = -1;
}
void push (int value){
if(top == MAX -1){
cout<<"Stack overflow! cannot push items"<<endl;
return;
}
data[++top] = value;
}
int pop(){
if(top == -1){
cout<<"Stack is underflow! cannot pop items"<<endl;
return -1;
}
return data[top--];
}
int peek(){
if(top == -1 ) {
cout<<"Stack is empty"<<endl;
return -1;
}
return data[top];
}
bool isEmpty(){
return top == -1;
}
bool isFull(){
return top == MAX -1;
}
};
int main(){
Stack s;
s.push(10);
s.push(20);
s.push(30);
cout<<"Top Element"<<s.peek()<<endl;
cout<<"Popped Element"<<s.pop()<<endl;
cout<<"Top Element"<<s.peek()<<endl;
}