-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplate stack.cpp
189 lines (179 loc) · 4.08 KB
/
template stack.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
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
182
183
184
185
186
187
188
189
#include <iostream>
#include <cstdlib>
#define MAXSIZE 100 // or const int MAXSIZE=100;
using namespace std;
template <class T>
class stack
/*
objective: Create a class for implementing Stack using array
input parameters:
top -> top index of stack
capacity-> size of stack
arr -> pointer to array
output -> templet of a stack of maximum size 100
Description:
Stack is a data structure used to store a collection of objects in LIFO(last in first out) manner.
Individual items can be added and stored in a stack using a PUSH operation And Remove using POP operation.
approach: Class defines data member and member function of the stack class
*/
{
T *arr; // for dynamic array
int top;
int capacity; // checks size defined by user
public:
stack(T size = MAXSIZE) // constructor to create array dynamically
{
arr = new int [size]; //dynamicly allocate a size to array
capacity=size;
top=-1;
}
~stack() // destructor to delete dynamically created array
{
delete arr;
}
void push(T &x) //function to push a value into the stack
{
if(isFull()==true) //checking whether stack is full or not!
{
cout<<"Stack Is Full!"<<endl;
}
else
{
top=top+1; //Inserting a value into the stack
arr[top]=x;
}
}
T pop()
{
T x = arr[top--]; //Removing a value into the stack
return x;
}
T peek() //Top of the the stack
{
return(arr[top]);
}
int size() // Current size of stack
{
return(top+1);
}
bool isEmpty() //checking whether stack is empty or not!
{
if(top==-1)
{
return true;
}
else
{
return false;
}
}
bool isFull() //checking whether stack is full or not!
{
if(top==capacity)
{
return true;
}
else
{
return false;
}
}
void show() // To Display the stack
{
int temp; // temprorily store the value of the stack
temp=top;
int i=0;
if(isEmpty()==true)
{
cout<<"Stack is empty";
}
else
{
while(i<=temp)
{
cout<<arr[i]<<"\t"; //Displying element of the stack
i=i+1;
}
}
}
};
int main()
{
char choice1='y';
stack<int> s; // Creating object of stack
cout<<"---Operation on Stack-----"<<endl;
cout<<"1) Push a element to stack"<<endl;
cout<<"2) Pop a element to stack"<<endl;
cout<<"3) Peek element to stack"<<endl;
cout<<"4) Size to stack"<<endl;
cout<<"5) check whether stack is empty or not"<<endl;
cout<<"6) check whether stack is full or not"<<endl;
cout<<"7) print a stack"<<endl;
int choice;
do
{
cout<<"Enter your choice"<<endl;
cin>>choice;
switch(choice)
{
case 1:
int element;
cout<<"Enter the element to be pushed into the stack "<<endl;
cin>>element;
s.push(element);
break;
case 2:
if(s.isEmpty()==true)
{
cout<<"stack is empty"<<endl;
}
else
{
cout<<s.pop()<<" poped from the stack"<<endl;
}
break;
case 3:
element=s.peek();
cout<<"top of the stack : "<<element<<endl;
break;
case 4:
cout<<"size is : "<<s.size()<<endl;
break;
case 5:
if(s.isEmpty()==true)
{
cout<<"TRUE stack is empty"<<endl;
}
else
{
cout<<"false stack is not empty"<<endl;
}
break;
case 6:
if(s.isFull()==true)
{
cout<<"TRUE stack is full"<<endl;
}
else
{
cout<<"false stack is not full"<<endl;
}
break;
case 7:
if(s.isEmpty()==true)
{
cout<<"stack is empty!";
}
else
{
cout<<"element in stack is"<<endl;
s.show();
}
break;
default:
cout<<"enter the right choice";
}
cout<<"Press y for continue"<<endl;
cin>>choice1;
}while(choice1=='y'||choice1=='Y');
}