-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathStack.cpp
More file actions
140 lines (130 loc) · 2.86 KB
/
Stack.cpp
File metadata and controls
140 lines (130 loc) · 2.86 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>
#define maxsize 10
using namespace std;
class Stack
{
int top;
int a[maxsize];
public:
Stack();
bool isfull();
bool isempty();
void push(int x);
void pop();
void display();
void Top();
void Size();
};
Stack::Stack() //initialize the top of Stack to 0 using default like constructor
{
top=0;
}
bool Stack::isfull() //if the top of stack is equal to thr maximum element allowed in stack, its full in size and cant push elements
{
if(top==maxsize)
return true;
else
return false;
}
bool Stack::isempty() //if the top of stack is 0, its empty and nothing to pop from stack
{
if(top==0)
return true;
else
return false;
}
void Stack::push(int x)
{
if(isfull())
{
cout<<"Stack is full"<<endl;
return;
}
else
a[top]=x; //if stack is not full, element is pushed into stack at the top index of stack array
top++;
}
void Stack::pop()
{
if(isempty())
{
cout<<"nothing in list"<<endl;
return;
}
else
top--; //if stack is not empty (when top is not equal to zero) element can be poped out of stack(element can be removed from the top index of stack)
cout<<"pops "<<a[top]<<endl;
}
void Stack::display() //displays the elements of the stack
{
int i;
if(top==0)
{
cout<<"nothing to display"<<endl;
}
else
cout<<"Displaying"<<endl;
for(i=0;i<top;i++)
{
cout<<a[i]<<endl;
}
}
void Stack::Top() //displays the top most element in the array of stack
{
cout<<"top is "<<a[top-1]<<endl;
}
void Stack::Size() //displays the size of stack
{
cout<<"size of Stack is "<<top<<endl;
}
int main()
{
Stack s;
int ch,element,result;
while(1)
{
cout<<"*--------------------------------------------------------------------*"<<endl;
cout<<"1.PUSH 2.POP 3.DISPLAY 4.ISFULL 5.ISEMPTY 6.TOP 7.SIZE 8.EXIT "<<endl;
cout<<"*--------------------------------------------------------------------*"<<endl;
cin>>ch;
if(ch==1)
{
cout<<"enter element ";
cin>>element;
s.push(element);
}
else if(ch==2)
{
s.pop();
}
else if(ch==3)
{
s.display();
}
else if(ch==4)
{
if(s.isfull())
cout<<"STACK IS FULL"<<endl;
else
cout<<"STACK IS NOT FULL"<<endl<<"YOU CAN PUSH"<<endl;
}
else if(ch==5)
{
if(s.isempty())
cout<<"STACK IS EMPTY"<<endl;
else
cout<<"STACK IS NOT EMPTY"<<endl<<"YOU CAN POP"<<endl;
}
else if(ch==6)
{
s.Top();
}
else if(ch==7)
{
s.Size();
}
else
return 0;
}
return 0;
}