-
Notifications
You must be signed in to change notification settings - Fork 127
/
insertion-and-deletion-in-stack.cpp
164 lines (152 loc) · 2.75 KB
/
insertion-and-deletion-in-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
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#include <bits/stdc++.h>
using namespace std;
struct Stack
{
int size;
int top ;
int *arr;
};
class Stack_Class
{
Stack *object;
protected:
bool isEmpty();
bool isFull();
public:
void creation();
void push();
void pop();
void display();
};
bool Stack_Class ::isEmpty()
{
if (object->top == -1)
{
return true;
}
else
{
return false;
}
}
bool Stack_Class ::isFull()
{
if (object->top == object->size-1)
{
return true;
}
else
{
return false;
}
}
void Stack_Class ::creation()
{
int s;
char ch = 'y';
object->top= -1;
cout << "Enter the size of the stack: ";
cin >> s;
object->size = s;
object->arr = new int[object->size];
while (ch == 'y' || ch == 'Y')
{
if (isFull())
{
cout << "The Stack is Full"<<endl;
break;
}
else
{
object->top++;
cout << "Enter the element " << object->top << " : ";
cin >> object->arr[object->top];
cout << endl;
cout << "Do you want to enter more: ";
cin >> ch;
}
}
}
void Stack_Class::display()
{
for (int i = 0; i <=object->top; i++)
{
cout << object->arr[i] << " ";
}
cout << endl;
}
void Stack_Class ::push()
{
if (isFull())
{
cout << "The stack is full"<<endl;
}
else
{
int element;
cout << "Enter the element which you want to insert in the Stack: ";
cin >> element;
object->arr[object->top] = element;
object->top++;
}
}
void Stack_Class ::pop()
{
if (isEmpty())
{
cout << "The Stack is empty"<<endl;
}
else
{
object->top--;
}
}
int main()
{
int choice;
Stack_Class operations;
do
{
cout << "1. Enter the array" << endl;
cout << "2. Display the array" << endl;
cout << "3. Push the element into the array" << endl;
cout << "4. pop the value in the array" << endl;
cout << "5. Exit" << endl;
cout << endl;
cout << "Enter your choice: ";
cin >> choice;
switch (choice)
{
case 1:
{
operations.creation();
break;
}
case 2:
{
operations.display();
break;
}
case 3:
{
operations.push();
break;
}
case 4:
{
operations.pop();
break;
}
case 5:
{
break;
}
default:
{
cout << "WRONG CHOICE !!!!!!!!!" << endl;
break;
}
}
} while (choice != 5);
return 0;
}