forked from Shailendra-Java/Data-Structures
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStackUsingArray.cpp
More file actions
87 lines (81 loc) · 1.98 KB
/
StackUsingArray.cpp
File metadata and controls
87 lines (81 loc) · 1.98 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
80
81
82
83
84
85
86
87
#include<iostream>
#define MAX 5
using namespace std;
class StackUsingArray
{
public:
int STACK[MAX],TOP;
StackUsingArray()
{
TOP = -1;
}
int isEmpty(){
if(TOP==-1)
return 1;
else
return 0;
}
int isFull(){
if(TOP==MAX-1)
return 1;
else
return 0;
}
void push(int num){
if(isFull()){
cout<<"STACK Overflow"<<endl;
return;
}
++TOP;
STACK[TOP] = num;
cout<<num<<" Has been inserted"<<endl;
}
void pop(){
int temp;
if(isEmpty()){
cout<<"STACK Underflow"<<endl;
return;
}
temp = STACK[TOP];
--TOP;
cout<<temp<<" Has been deleted"<<endl;
}
void display(){
int i;
if(isEmpty()){
cout<<"STACK Underflow"<<endl;
return;
}
for(i=TOP; i>=0; i--)
cout<<STACK[i]<<" ";
cout<<endl;
}
};
int main(){
StackUsingArray sua;
int num, opn;
char ch;
do{
cout<<"1 => Push\n2 => Pop\n3 => Display"<<endl;
cout<<"Enter your choice"<<endl;
cin>>opn;
switch(opn){
case 1:
cout<<"Enter an Integer number"<<endl;
cin>>num;
sua.push(num);
break;
case 2:
sua.pop();
break;
case 3:
sua.display();
break;
default:
cout<<"An invalid choice!"<<endl;
}
cout<<"Do you want to continue(y/n)"<<endl;
cin>>ch;
}while(ch == 'y' || ch == 'Y');
return 0;
}