forked from siddhant3s/cs215
-
Notifications
You must be signed in to change notification settings - Fork 0
/
q6.cpp
executable file
·94 lines (91 loc) · 1.41 KB
/
q6.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
#include<iostream>
#include<string>
const int MAX_SIZE=20;
class Stack
{
int data[MAX_SIZE];
unsigned int top;
public:
void push(int);
int pop();
int peek();
int isfull();
int isempty();
void display();
Stack()
{
top=0;
}
};
int Stack::isfull()
{
if (top<MAX_SIZE) return 0;
return 1;
}
int Stack::isempty()
{
if (top==0) return 1;
return 0;
}
void Stack::push(int d)
{
if (!isfull())
data[top++]=d;
else{
std::cout<<"Stack is FULL\n";
}
}
int Stack::pop()
{
if(!isempty())
return data[--top];
else{
std::cout<<"Stack is Empty\n";
return -1;
}
}
void Stack::display()
{
for(int i=0;i<top;++i)
std::cout<<data[i]<< ' ';
}
int Stack::peek()
{
if(!isempty())
return data[top-1];
else
std::cout<<"Stack is Empty\n";
}
int main()
{
Stack s;
do
{
std::cout<<"Static Stack Implementation. Maximum stack size is "<<MAX_SIZE<<std::endl;
std::cout<< \
"Menu\n"
"1.Push\n2.Pop\n3.Peek\n4.Display\n5.Exit";
int opt;
std::cin>>opt;
switch(opt)
{
case 1:
std::cout<<"Enter an element to push: ";
int e;
std::cin>>e;
s.push(e);
break;
case 2:
std::cout<<"The poped element is:"<<s.pop();break;
case 3:
std::cout<<"The Peeked elementis:"<<s.peek();break;
case 4:
std::cout<<std::endl;
s.display();
break;
case 5: return 0;
default:
std::cout<<"Enter a valid input\n";
}
}while(1);
}