-
Notifications
You must be signed in to change notification settings - Fork 344
/
Copy pathstack_using_array.cpp
97 lines (83 loc) · 1.69 KB
/
stack_using_array.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
#include<iostream>
#define MAX 1000
using namespace std;
int STACK[MAX],TOP;
//stack initialization
void Stack(){
TOP=-1;
}
//check if stack is empty or not
int isEmpty(){
if(TOP==-1)
return 1;
else
return 0;
}
//check if stack is full or not
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 TO the stack."<<endl;
}
void display(){
int i;
if(isEmpty()){
cout<<"STACK UNDERFLOW"<<endl;
return;
}
for(i=TOP;i>=0;i--){
cout<<STACK[i]<<" ";
}
cout<<endl;
}
//pop from stack
void pop(){
int temp;
if(isEmpty()){
cout<<"STACK UNDERFLOW"<<endl;
return;
}
temp=STACK[TOP];
TOP--;
cout<<temp<<" has been deleted from the stack."<<endl;
}
int main(){
int num;
Stack();
char ch;
do{
int a;
cout<<"Chosse \n1.push\n"<<"2.pop\n"<<"3.display\n";
cout<<"Please enter your choice: ";
cin>>a;
switch(a)
{
case 1:
cout<<"Enter an Integer Number: ";
cin>>num;
push(num);
break;
case 2:
pop();
break;
case 3:
display();
break;
default :
cout<<"An Invalid Choice!!!\n";
}
cout<<"Do you want to continue ? ";
cin>>ch;
}while(ch=='Y'||ch=='y');
return 0;
}