-
Notifications
You must be signed in to change notification settings - Fork 1
/
stack.cpp
97 lines (85 loc) · 1.84 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
/********************************************************************
* Note Assumed a stack of size 10
********************************************************************/
#include <iostream>
using namespace std;
int push(int top, int stack[], int size)
{
top++;
if (top > size - 1)
{
cout << "Stack Overflow" << endl;
return -1;
}
else
{
cout << "Enter data to be inserted ";
cin >> stack[top];
cout << "Element pushed" << endl;
return top;
}
}
int pop(int top, int stack[])
{
if (top == -1)
{
cout << "Stack Underflow" << endl;
return -1;
}
else
{
cout << "Popped item is " << stack[top] << endl;
top--;
return top;
}
}
void display(int top, int stack[])
{
if (top == -1)
{
cout << "stack is empty" << endl;
}
else
{
cout<<endl;
cout << stack[top] <<" <----- top"<< endl;
for (int i = top-1; i >= 0; i--)
{
cout << stack[i] << endl;
}
cout << endl;
}
}
int main()
{
const int size = 10;
int stack[size], top = -1, ch, data;
string ans;
do
{
cout << "1. Push" << endl;
cout << "2. Pop" << endl;
cout << "3. Display" << endl;
cout << "Entre your Choice: ";
cin >> ch;
int size = sizeof(stack) / sizeof(stack[0]);
switch (ch)
{
case 1:
top = push(top, stack, size);
break;
case 2:
top = pop(top, stack);
break;
case 3:
display(top, stack);
break;
default:
cout << "Wrong Choice";
break;
}
cout << "Do you wish to continue? ";
cin >> ans;
} while (ans == "Y" || ans == "y");
return 0;
}