-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstack.cpp
More file actions
140 lines (130 loc) · 2.52 KB
/
stack.cpp
File metadata and controls
140 lines (130 loc) · 2.52 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
#include<iostream>
using namespace std;
class stack {
private:
int arr[5];
int top;
public:
//creating empty stack
void createEmptyStack()
{
top=-1;
for (int i = 0; i < 5; i++)
{
arr[i]=0;
}
}
bool isempty()
{
if (top==-1)
{
return true;
}
else
return false;
}
bool isfull()
{
if(top== 4)
return true;
else
return false;
}
void peek()
{
if(isempty())
cout<<"stack underflow\n";
else
cout<<arr[top]<<endl;
}
void push(int value)
{
if(isfull())
{
cout<<"Stack overflow\n";
}
else
{
top++;
arr[top]=value;
}
}
int pop()
{
if(isempty())
{
cout<<"Stack underflow\n";
return 0;
}
else
{
int popval=arr[top];
arr[top]=0;
top--;
return popval;
}
}
void display()
{
for (int i = 4; i > -1; i--)
{
cout<<arr[i]<<endl;
}
}
int count()
{
return (top+1);
}
};
int main()
{
//Trying to create a Stack
cout<<"Enter the size of stack that you want to create : \n";
int size;
cin>>size;
stack one;
int option,position,value;
do
{
cout<<"What operation do you want to perform -- select the option number--\nEnter 0 to exit\n";
cout<<"1. Create Empty Stack\n";
cout<<"2. Is Stack Empty\n";
cout<<"3. Is Stack Full\n";
cout<<"4. PUSH\n";
cout<<"5. POP\n";
cout<<"6. PEEK\n";
cout<<"7. Display\n";
cout<<"8. Count the number of elements in Stack\n";
cout<<"9. Clear Screen\n";
cout<<"0. Exit\n";
cin>>option;
switch (option)
{
case 1:
one.createEmptyStack();
break;
case 2:
cout<<one.isempty();
case 3:
cout<<one.isfull();
case 4:
cout<<"Enter the value to be pushed in the stack\n";
cin>>value;
one.push(value);
case 5:
cout<<one.pop();
case 6:
one.peek();
case 7:
one.display();
case 8:
cout<<one.count();
case 9:
system("clear");
default:
cout<<"choose the valid option\n";
break;
}
}while(option!=0);
return 0;
}