-
Notifications
You must be signed in to change notification settings - Fork 0
/
stackAndQueueUsingLL.cpp
194 lines (179 loc) · 3.73 KB
/
stackAndQueueUsingLL.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
#include <bits/stdc++.h>
using namespace std;
struct node
{
int data;
node *next = NULL;
};
node *FRONT = NULL;
node *REAR = NULL;
node *TOS = NULL;
void MainMenu()
{
cout << "\n=============Main Menu=============\n";
cout << "1.Enqueue in Queue \n";
cout << "2.Dequeue in Queue \n";
cout << "3.Push in Stack \n";
cout << "4.Pop in Stack \n";
cout << "5.Peek in Stack \n";
cout << "6.Exit \n";
}
void Enqueue(node *&FRONT)
{
node *ptr = new node();
int data = 0;
cout << "Enqueue integer: ";
cin >> data;
ptr->data = data;
if (FRONT == NULL)
{
FRONT = ptr;
REAR = ptr;
return;
}
REAR->next = ptr;
REAR = ptr;
return;
}
void Dequeue(node *&FRONT)
{
if (FRONT == NULL)
{
cout << "Queue is empty. Underflow! \n";
return;
}
node *temp = FRONT;
FRONT = FRONT->next;
delete (temp);
return;
}
void traverseQueue(node *FRONT)
{
node *temp = FRONT;
while (temp != NULL)
{
cout << temp->data << " <- ";
temp = temp->next;
}
cout << "NULL \n";
return;
}
void Push()
{
node *ptr = new node();
int data;
cout << "Push integer: ";
cin >> data;
ptr->data = data;
if (TOS == NULL)
{
TOS = ptr;
return;
}
ptr->next = TOS;
TOS = ptr;
return;
}
void pop()
{
if (TOS == NULL)
{
cout << "The Stack is empty. UnderFlow! \n";
return;
}
node *temp = TOS;
TOS = TOS->next;
cout << "Popped element is " << temp->data << "\n";
delete (temp);
}
void peek()
{
if (TOS == NULL)
{
cout << "The Stack is empty. \n";
return;
}
cout << "TOP element is " << TOS->data;
}
void display()
{
if (TOS == NULL)
{
cout << "The Stack is empty. \n";
return;
}
node *temp = TOS;
// cout << "The stack will be printed in top to down manner instead of bottoms up approach \n";
while (temp != NULL)
{
cout << temp->data <<"\n---------------------\n ";
temp = temp->next;
}
cout << "NULL\n";
return;
}
int main()
{
int userChoice = 0;
node *stackHead = NULL;
node *queueHead = NULL;
int numQueueElements;
cout << "How many elements do you want to initially enqueue in the queue? ";
cin >> numQueueElements;
for (int i = 0; i < numQueueElements; i++)
{
Enqueue(queueHead);
}
int numStackElements;
cout << "How many elements do you want to initially push into the stack? ";
cin >> numStackElements;
for (int i = 0; i < numStackElements; i++)
{
Push();
}
cout << "Queue after initial enqueues: \n";
traverseQueue(queueHead);
cout << "Stack after initial pushes: \n";
display();
while (userChoice != 6)
{
MainMenu();
cout << "Enter your choice: ";
cin >> userChoice;
switch (userChoice)
{
case 1:
// Enqueue in Queue
Enqueue(queueHead);
traverseQueue(queueHead);
break;
case 2:
// Dequeue from Queue
Dequeue(queueHead);
traverseQueue(queueHead);
break;
case 3:
// Push to Stack
Push();
display();
break;
case 4:
// Pop from Stack
pop();
display();
break;
case 5:
// Peek the top element in Stack
peek();
break;
case 6:
// Exit the loop
cout << "Exiting...\n";
userChoice = 6;
return 0;
default:
cout << "Invalid choice, please try again.\n";
}
}
return 0;
}