-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack.cpp
120 lines (108 loc) · 2.16 KB
/
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
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include<iostream>
#define maxsize 10
using namespace std;
class Stack{
int top;
int a[maxsize];
public:
Stack();
bool isfull();
bool isempty();
void push(int x);
void pop();
void display();
void Top();
void Size();
};
Stack::Stack(){
top=0;
}
bool Stack::isfull(){
if(top==maxsize)
return true;
else
return false;
}
bool Stack::isempty(){
if(top==0)
return true;
else
return false;
}
void Stack::push(int x){
if(isfull()){
cout<<"Stack is full"<<endl;
return;
}
else
a[top]=x;
top++;
}
void Stack::pop(){
if(isempty()){
cout<<"nothing in list"<<endl;
return;
}
else
top--;
cout<<"pops "<<a[top]<<endl;
}
void Stack::display(){
int i;
if(top==0){
cout<<"nothing to display"<<endl;
}
else
cout<<"Displaying"<<endl;
for(i=0;i<top;i++){
cout<<a[i]<<endl;
}
}
void Stack::Top(){
cout<<"top is "<<a[top-1]<<endl;
}
void Stack::Size(){
cout<<"size of Stack is "<<top<<endl;
}
int main(){
Stack s;
int ch,element,result;
while(1){
cout<<"*--------------------------------------------------------------------*"<<endl;
cout<<"1.PUSH 2.POP 3.DISPLAY 4.ISFULL 5.ISEMPTY 6.TOP 7.SIZE 8.EXIT "<<endl;
cout<<"*--------------------------------------------------------------------*"<<endl;
cin>>ch;
if(ch==1){
cout<<"enter element ";
cin>>element;
s.push(element);
}
else if(ch==2){
s.pop();
}
else if(ch==3){
s.display();
}
else if(ch==4){
if(s.isfull())
cout<<"STACK IS FULL"<<endl;
else
cout<<"STACK IS NOT FULL"<<endl<<"YOU CAN PUSH"<<endl;
}
else if(ch==5){
if(s.isempty())
cout<<"STACK IS EMPTY"<<endl;
else
cout<<"STACK IS NOT EMPTY"<<endl<<"YOU CAN POP"<<endl;
}
else if(ch==6){
s.Top();
}
else if(ch==7){
s.Size();
}
else
return 0;
}
return 0;
}