-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.cpp
More file actions
181 lines (156 loc) · 2.75 KB
/
stack.cpp
File metadata and controls
181 lines (156 loc) · 2.75 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
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// implementation of stack Data Structure in C++
//with functions like push, pop, change, display, isEmpty, isFull, peek and count
//using the concepts of OOPs
#include<iostream>
using namespace std;
class Stack
{
private:
int top;
int arr[10];
public:
Stack()
{
top = -1;
for (int i =0; i<10; i++)
{
arr[i] = 0;
}
}
bool isEmpty()
{
if (top == -1)
return true;
else
return false;
}
bool isFull()
{
if (top == (10-1))
return true;
else
return false;
}
void push(int element)
{
if (!isFull())
{
top ++;
arr[top] = element;
cout<<element<<"inserted"<<endl;
}
else
cout<<"Stack is full"<<endl;
}
int pop ()
{
if (!isEmpty())
{
int popped = arr[top];
arr[top] = 0;
top --;
cout<<popped<<"popped"<<endl;
return popped;
}
else
{
cout<<"Stack is empty"<<endl;
return 0;
}
}
int count()
{
return (top+1);
}
int peek (int position)
{
if (!isEmpty())
return arr[position-1];
else
{
cout<<"stack is empty"<<endl;
return 0;
}
}
void change (int position, int value)
{
arr[position-1] = value;
cout<<"value at location "<<position<<"position changed to "<<value<<endl;
}
void display()
{
for (int i =0; i<=top; i++)
{
cout<<arr[i]<<" ";
}
cout<<"\n";
}
};
int main()
{
Stack s;
int x=1, element, position, popped;
while(x!=0)
{
cout<<"\nEnter the serial number to select the suitable stack operation\n"<<
"1. push element \n2. pop element \n3. count the number of elements \n"<<
"4. display elements \n5. change an element at a given position \n"<<
"6. peek at a given position \n0. Exit\n\n";
cin>>x;
switch(x)
{
case 0:
{
cout<<"Execution ended. \nThanks!"<<endl;
break;
}
case 1:
{
cout<<"Enter element to push on stack:"<<endl;
cin>>element;
s.push(element);
break;
}
case 2:
{
popped = s.pop();
break;
}
case 3:
{
int count =s.count();
if (count)
cout<<"there are "<< count <<"elements in the stack\n";
else
break;
}
case 4:
{
s.display();
break;
}
case 5:
{
cout<<"enter position \n";
cin>>position;
cout<<"enter value \n";
cin>>element;
s.change(position, element);
break;
}
case 6:
{
cout<<"enter position \n";
cin>>position;
cout<<"value at location"<<position<<" is "<<s.peek(position)<<endl;
break;
}
default:
{
cout<<"Invalid input\n";
break;
}
}
}
return 0;
}