File tree 2 files changed +123
-0
lines changed
2 files changed +123
-0
lines changed Original file line number Diff line number Diff line change
1
+ #include < iostream>
2
+ #define SIZE 100
3
+ using namespace std ;
4
+
5
+ class StackProgram
6
+ {
7
+ int top = -1 ;
8
+ int st[SIZE];
9
+
10
+ public:
11
+ StackProgram ()
12
+ {
13
+ top = -1 ;
14
+ }
15
+ void push (int );
16
+ void pop ();
17
+ int peep ();
18
+ void display ();
19
+ void chocie ();
20
+ };
21
+
22
+ void StackProgram::push (int v)
23
+ {
24
+ if (top == SIZE - 1 )
25
+ {
26
+ cout << " Stack is full!\n Cannot insert element " << v << endl;
27
+ return ;
28
+ }
29
+ st[++top] = v;
30
+ }
31
+
32
+ void StackProgram::pop ()
33
+ {
34
+ if (top == -1 )
35
+ {
36
+ cout << " Stack is empty!" << endl;
37
+ return ;
38
+ }
39
+ cout << " Popped element is " << st[top--] << endl;
40
+ }
41
+
42
+ int StackProgram::peep ()
43
+ {
44
+ if (top == -1 )
45
+ {
46
+ cout << " Stack is empty!" << endl;
47
+ return -1 ;
48
+ }
49
+ return st[top];
50
+ }
51
+
52
+ void StackProgram::display ()
53
+ {
54
+ if (top == -1 )
55
+ {
56
+ cout << " Stack is empty!" << endl;
57
+ return ;
58
+ }
59
+ cout << " Stack elements are:" << endl;
60
+ for (int i = top; i >= 0 ; i--)
61
+ {
62
+ cout << st[i] << endl;
63
+ }
64
+ }
65
+
66
+ void StackProgram::chocie ()
67
+ {
68
+ int ch;
69
+ cout << " 1: Insert elements" << endl;
70
+ cout << " 2: Delete topmost element" << endl;
71
+ cout << " 3: Delete all elements" << endl;
72
+ cout << " 4: Check topmost element" << endl;
73
+ cout << " 5: Display stack elements" << endl;
74
+ cout << " 6: Terminate the program" << endl;
75
+ cout << " Enter your choice: " ;
76
+ cin >> ch;
77
+ cout << endl;
78
+ switch (ch)
79
+ {
80
+ case 1 :
81
+ cout << " Enter element to be inserted: " ;
82
+ int e;
83
+ cin >> e;
84
+ push (e);
85
+ break ;
86
+
87
+ case 2 :
88
+ pop ();
89
+ break ;
90
+
91
+ case 3 :
92
+ top = -1 ;
93
+ break ;
94
+
95
+ case 4 :
96
+ if (peep () != -1 )
97
+ cout << " Topmost element is " << peep () << endl;
98
+ break ;
99
+
100
+ case 5 :
101
+ display ();
102
+ break ;
103
+
104
+ case 6 :
105
+ exit (0 );
106
+ break ;
107
+
108
+ default :
109
+ cout << " Invalid choice entered." << endl;
110
+ chocie ();
111
+ }
112
+ }
113
+
114
+ int main ()
115
+ {
116
+ StackProgram sp;
117
+ while (true )
118
+ {
119
+ sp.chocie ();
120
+ cout << " ------------------------------" << endl;
121
+ }
122
+ return 0 ;
123
+ }
You can’t perform that action at this time.
0 commit comments