-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.cpp
More file actions
125 lines (107 loc) · 2.07 KB
/
stack.cpp
File metadata and controls
125 lines (107 loc) · 2.07 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include <iostream>
#define MAX 5
using namespace std;
struct stack
{
int ele[MAX];
int top;
};
typedef struct stack STACK;
STACK s;
int isEmpty(void);
int isFull(void);
void push(int new_ele);
int pop(void);
int peek(void);
void traverse(void);
int main()
{
int option, choice = 1, num;
s.top = -1;
cout << "STACK OPERATIONS" << endl;
while (choice)
{
cout << "------------------------------------------------" << endl;
cout << " 1--> PUSH " << endl;
cout << " 2--> POP " << endl;
cout << " 3--> PEEK " << endl;
cout << " 4--> TRAVERSE " << endl;
cout << " 5--> EXIT " << endl;
cout << "-------------------------------------------------" << endl;
cout << "Enter your choice: ";
cin >> choice;
switch (choice)
{
case 1:
cout << "Enter the number to be pushed: ";
cin >> num;
push(num);
break;
case 2:
num = pop();
if (num != -1)
cout << "Number " << num << " is Popped from the stack";
break;
case 3:
cout << "Top elements with PEEK operation: " << peek() << endl;
break;
case 4:
traverse();
break;
case 5:
return 0;
default:
cout << "Type 1 to continue: ";
cin >> option;
}
}
}
int isEmpty(void)
{
if (s.top == -1)
{
cout << "stack underflow" << endl;
return 1;
}
return 0;
}
int isFull(void)
{
if (s.top == MAX - 1)
{
cout << "stack overflow" << endl;
return 1;
}
return 0;
}
void push(int new_ele)
{
if (!isFull())
s.ele[++s.top] = new_ele;
}
int pop(void)
{
if (!isEmpty())
return (s.ele[s.top--]);
return -1;
}
int peek(void)
{
return s.ele[s.top];
}
void traverse(void)
{
if (s.top != -1)
{
cout << "status of the stack is: ";
for (int i = s.top; i >= 0; i--)
{
cout << s.ele[i] << " ";
}
cout << endl;
}
else
{
cout << "stack is empty" << endl;
}
}