-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack using array.cpp
117 lines (114 loc) · 2.72 KB
/
stack using array.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
#include <iostream>
using namespace std;
#define max 5
class Stack{
int top;
int arr[max];
public:
Stack ()
{
top=-1;
}
int isFull()
{
if (top==max-1)
return 1;
else
return 0;
}
int isEmpty()
{
if (top==-1)
return 1;
else
return 0;
}
void push (int val)
{
top++;
arr[top]=val;
}
int pop ()
{
if (!isEmpty())
{
int x=arr[top];
top--;
cout<<"Poped value: "<<x<<endl;
}
else
cout<<"Stack is Empty."<<endl;
}
int peek ()
{
if (!isEmpty())
cout<<"Peeked value: "<<arr[top]<<endl;
else
cout<<"Stack is Empty."<<endl;
}
void print()
{
if (isEmpty())
cout<<"Stack is Empty."<<endl;
else
{
cout<<"Stack values are: "<<endl;
for (int i=top;i>=0;--i)
cout<<arr[i]<<" ";
cout<<endl;
}
}
};
int main ()
{
Stack s;
int ch,value;
do
{
cout<<"1 for push"<<endl;
cout<<"2 for pop"<<endl;
cout<<"3 for peek"<<endl;
cout<<"4 for empty"<<endl;
cout<<"5 for Full"<<endl;
cout<<"6 for print"<<endl;
cout<<"Enter choice: ";
cin>>ch;
switch (ch)
{
case 1:
if (!(s.isFull()))
{
cout<<"Enter the item: ";
cin>>value;
s.push(value);
}
else
cout<<"Stack is Full."<<endl;
break;
case 2:
s.pop();
break;
case 3:
s.peek();
break;
case 4:
if (s.isEmpty())
cout<<"Stack is empty."<<endl;
else
cout<<"Stack is not empty."<<endl;
break;
case 5:
if (s.isFull())
cout<<"Stack is full."<<endl;
else
cout<<"Stack is not full."<<endl;
break;
case 6:
s.print();
break;
default:
cout<<"Check again."<<endl;
}
}while (ch<7);
return 0;
}