-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStackUsingArrays.cpp
More file actions
108 lines (95 loc) · 1.68 KB
/
StackUsingArrays.cpp
File metadata and controls
108 lines (95 loc) · 1.68 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
//Stacks using array//
#include <iostream>
#include <stdio.h>
#include <conio.h>
#include <process.h>
using namespace std;
#define SIZE 5
class stk
{
int stack[SIZE];
int top;
public:
int push();
int pop();
void display();
stk()
{
top = -1;
}
};
int stk::push()
{
int item;
if (top == SIZE - 1)
{
cout << "Stack is Full\n";
}
else
{
cout << "Enter element to push: \n";
cin >> item;
top = top + 1;
stack[top] = item;
}
}
int stk::pop()
{
int temp;
if (top == -1)
{
cout << "Empty Stack\n";
}
else
{
temp = stack[top];
top = top - 1;
cout << "----successful----" << endl;
cout << "popped element is " << temp << endl;
}
}
void stk::display()
{
if (top == -1)
cout << "stack empty\n";
else
{
cout << "Elements are:\n";
for (int i = top; i >= 0; i--)
{
cout << stack[i] << endl;
}
}
cout << endl;
}
int main()
{
int option = 0;
stk s;
int element;
while (1)
{
cout << "---------------------------------\n";
cout << "enter 1 to push:\nenter 2 for pop:\n";
cout << "enter 3 for display:\nenter any number to exit\n";
cout << "---------------------------------\n";
cout << "Enter option: \n";
cin >> option;
switch (option)
{
case 1:
s.push();
break;
case 2:
s.pop();
break;
case 3:
s.display();
break;
default:
exit(0);
}
}
getch();
return 0;
}