-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStack_LinkedListImplementation.cpp
197 lines (170 loc) · 3.2 KB
/
Stack_LinkedListImplementation.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
190
191
192
193
194
195
196
197
/* Basic Stack Operations Using Linked List.*/
/*Created By Murtaza S Vhora
at 18:28 04/03/2016 */
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
using namespace std;
class Stack
{
private:
int arr[100],max,count;
struct LList
{
int data;
struct LList *next;
}*start;
public:
/*Empty Constructor */
Stack(){}
/*Constructor to create stack */
Stack(int size)
{
count = 0;
max = size;
start = NULL;
}
/*Function for push element to the stack. */
void push(int value)
{
if(count < max)
{
count++;
struct LList *temp = new struct LList;
temp->data=value;
if(start == NULL)
{
temp->next = NULL;
start = temp;
}
else
{
temp->next = start;
start = temp;
}
cout<<endl<<" Element "<<value<<" pushed succesfully."<<endl<<endl;
}
else
cout<<endl<<" Stack Overflow."<<endl<<endl;
}
/*Function to pop element from stack. */
int pop()
{
int temp = 0;
if(count > 0)
{
temp = start->data;
struct LList *t = start;
start = start->next;
count--;
delete t;
}
return temp;
}
/*Function to print Stack */
void printStack()
{
if(count > 0)
{
cout<<"\n\n\t\t|\t\t|\n\t\t|\t\t|\n\t\t|\t\t|\n\t\t|\t\t|\n";
struct LList *tempStart = start;
cout<<"\ttop---->|\t"<<tempStart->data<<"\t|\n";
tempStart=tempStart->next;
while(tempStart != NULL)
{
cout<<"\t\t|\t"<<tempStart->data<<"\t|\n";
tempStart=tempStart->next;
}
cout<<"\t\t`````````````````\n\n";
}
else
cout<<endl<<" Stack is empty."<<endl<<endl;
}
/*Function to empty stack. */
void emptyStack()
{
while(start != NULL)
{
struct LList *t = start->next;
delete start;
start = t;
count--;
}
cout<<endl<<" Stack emptied."<<endl<<endl;
}
};
/*Main Function */
int main()
{
system("clear");
cout<<"\t\t*******STACK IMPLEMENTATION USING ARRAY*******"<<endl<<endl;
cout<<"Enter the size of stack : ";
int size;
cin>>size;
Stack s(size);
while(true)
{
/*Description to be printed on screen. */
system("clear");
cout<<"\t\t*******STACK IMPLEMENTATION USING ARRAY*******"<<endl<<endl;
cout<<" 1. Push Value"<<endl;
cout<<" 2. Pop Value"<<endl;
cout<<" 3. Print Stack"<<endl;
cout<<" 4. Empty Stack"<<endl;
cout<<" 5. Exit"<<endl<<endl;
cout<<"Enter your choice : ";
int choice;
cin>>choice;
/*Switch Cases*/
switch(choice)
{
/*Push Value Case*/
case 1:
{
cout<<"Enter the value of element : ";
int element;
cin>>element;
s.push(element);
break;
}
/*Pop Value Case*/
case 2:
{
int element=s.pop();
if(element == 0)
cout<<endl<<" Stack Underflow."<<endl<<endl;
else
cout<<endl<<" Element "<<element<<" popped successfully."<<endl<<endl;
break;
}
/*Print Stack Case*/
case 3:
{
s.printStack();
break;
}
/*Empty Stack Case*/
case 4:
{
s.emptyStack();
break;
}
/*Exit Case*/
case 5:
{
return 0;
}
default:
{
cout<<"You have entered an invalid choice. Please try again.";
break;
}
}
cout<<"Do you wanna continue(1-YES else No) : ";
int con;
cin>>con;
if(con != 1)
return 0;
}
return 0;
}