-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSTACK.cpp
104 lines (104 loc) · 1.49 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
//Stack Operations
#include<iostream.h>
#include<conio.h>
class Stack
{
int StackData[100], size, top;
public:
Stack() {top=-1;}
void SetSize(int n)
{
size=n;
}
void Push(int x);
int Pop(void);
void Display(void);
};
void Stack::Push(int x)
{
if(top>=size-1)
{
cout<<"\n\t\t Stack is over flow...";
getch();
}
else
{
top++;
StackData[top]=x;
cout<<"\n\t\t"<<x<<" is pushed \n\n\n";
}
}
int Stack::Pop(void)
{
int x;
if(top<=-1)
{
cout<<"\n\t\tStack is under flow";
return -9999;
}
else
{
x=StackData[top];
top--;
return x;
}
}
void Stack::Display(void)
{
if(top>=0)
{
cout<<"\n The elements in stack.";
cout<<"\n******************";
for(int i=top;i>=0;i--)
cout<<"\n\nDATA "<<i+1<<" = "<<StackData[i];
cout<<"\n\n\n\n Press any key to continue...";
}
else
{
cout<<"\nThe Stack is empty.";
}
}
int main()
{
Stack S;
clrscr();
int option,n;
cout<<"\nEnter the size of stack [MAX=100] : ";
cin>>n;
S.SetSize(n);
do
{
clrscr();
cout<<"\n\t\t Stack Operation";
cout<<"\n\t\t**************";
cout<<"\n\t 1.PUSH\n\t 2.POP\n\t 3.DISPLAY\n\t 4.EXIT";
cout<<"\n\n Enter your option : ";
cin>>option;
int x, topvalue;
switch(option)
{
case 1:
cout<<"\nEnter a value to be pushed : ";
cin>>x;
S.Push(x);
S.Display();
break;
case 2:
topvalue=S.Pop();
if (topvalue!=-9999)
cout<<"\n\t\t The popped element is "<<topvalue;
S.Display();
break;
case 3:
S.Display();
break;
case 4:
cout<<"\n\n\t\t You are on EXIT...";
break;
default:
cout<<"\n\n\t Enter only 1,2,3,4.";
}
getch();
}while(option!=4);
return 0;
}