-
Notifications
You must be signed in to change notification settings - Fork 28
/
stackusing-array.cpp
92 lines (85 loc) · 1.23 KB
/
stackusing-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
#include<iostream>
#define MAX 10
using namespace std;
int arr[MAX];
int item;
int ch;
int top=-1;
int stackCount=0;
//push operation
void push(int stack[],int item)
{
if(top==(MAX-1))
cout << "Stack Overflow..." << endl;
else
{
top=top+1;
stack[top]=item;
stackCount++;
}
}
//pop operation
int pop(int stack[])
{
int itemTop;
if(top==-1)
cout << "Stack Underflow..." << endl ;
else
{
itemTop=stack[top];
top=top-1;
stackCount--;
cout << "Pop item is " << itemTop << endl ;
}
return itemTop;
}
//display
void display(int stack[])
{
if(top==-1)
cout << "Stack Empty" << endl;
else
{
cout << "******************" << endl;
for(int i=top; i>=0; i--)
{
cout << stack[i] << endl;
}
cout << "*******************" << endl;
}
}
int main()
{
do
{
cout <<"Program Menu\n";
cout <<"1. Push an Element.\n";
cout <<"2. Pop an Element.\n";
cout <<"3. Display Stack.\n";
cout <<"4. Exit\n";
cout <<"Enter your Choice:";
cin >> ch;
switch(ch)
{
case 1:
cout << "Enter an element:";
cin >> item;
push(arr,item);
break;
case 2:
item=pop(arr);
cout << "Element poped " << item << endl;
break;
case 3:
display(arr);
break;
case 4:
exit(0);
default:
cout << "Invalid Choice...";
break;
}
}
while(ch!=4);
return 0;
}