-
Notifications
You must be signed in to change notification settings - Fork 0
/
Stacks and Queues.cpp
256 lines (236 loc) · 5.31 KB
/
Stacks and Queues.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
#include <iostream>
#include <string.h>
using namespace std;
typedef struct person
{
int age;
string firstname;
string lastname;
}person;
typedef struct node
{
person pers;
node* next;
}node;
person newperson(string firstname, string lastname, int age)
{
person* thisperson = new person;
thisperson->firstname = firstname;
thisperson->age = age;
thisperson->lastname= lastname;
return *(thisperson);
}
void showperson(person &p)
{
cout<<p.firstname<<" "<<p.lastname<<" "<<p.age<<endl;
}
typedef node* pNode;
/* QUEUE IMPLEMENTATION */
typedef struct queue
{
pNode head;
pNode tail;
}queue;
void init_queue(queue &q)
{
q.head = nullptr;
q.tail = nullptr;
}
void enqueue(queue &q,person &p)
{
pNode newnode = new node;
newnode->pers = p;
newnode->next = nullptr;
if (q.head == nullptr)
{
q.head = newnode;
q.tail = newnode;
}else
{
newnode->next = q.head;
q.head = newnode;
}
}
person dequeue(queue &q)
{
pNode tmp = q.head;
if(q.head==nullptr)
{
person noperson = newperson("NONE","NONE",0);
return noperson;
}
while(tmp->next != q.tail)
{
if(q.head != q.tail)
{tmp = tmp->next;}
else
{
cout<<"Queue is now empty"<<endl;
init_queue(q);
return tmp->pers;
}
}
q.tail = tmp;
person tmpperson = tmp->next->pers;
tmp->next = nullptr;
free(tmp->next);
return tmpperson;
}
void delete_queue(queue &q,person &p)
{
}
void queue_traverse_forward(queue &q)
{
pNode tmp = q.head;
while(tmp != nullptr)
{
showperson(tmp->pers);
tmp = tmp->next;
}
}
void list_back(pNode &p)
{
pNode tmp = p;
if(tmp->next != nullptr)
{
list_back(tmp->next);
}
showperson(tmp->pers);
}
void queue_traverse_backward(queue &q)
{
if(q.head != nullptr)
{
pNode tmp = q.head;
list_back(tmp);
}
}
/* STACK IMPLEMENTATION */
typedef node* stack;
void init_stack(stack &s)
{
s = nullptr;
}
void stack_push(stack &s, person &p)
{
pNode newnode = new node;
newnode->pers = p;
newnode->next = s;
s = newnode;
}
person stack_pop(stack &s)
{
pNode tmp = s;
s = tmp->next;
person Pertmp = tmp->pers;
free(tmp);
return Pertmp;
}
void forward_traverse_stack(stack &s)
{
pNode tmp = s;
while(tmp != nullptr )
{
showperson(tmp->pers);
tmp = tmp->next;
}
}
void backward_traverse_stack(stack &s)
{
if(s != nullptr)
{
pNode tmp = s;
list_back(tmp);
}
}
int main()
{
queue q;
stack s;
init_queue(q);
init_stack(s);
person tmpperson;
string firstname,lastname;
int age;
char op,op2 ='M';
cout<<"Type S for stack or Q for Queue"<<endl;
cin>>op;
switch(op)
{
case 'S':
while(op2 != 's')
{
cout<<"Choose among the following operations: through the corresponding letter:"<<endl;
cout<<"a- Push a person's info into the stack"<<endl;
cout<<"b- pop a person's info from the stack"<<endl;
cout<<"c- Forward Traverse the Stack"<<endl;
cout<<"d- Backward Traverse the Stack"<<endl;
cout<<"s- stop the program"<<endl;
cin>>op2;
switch(op2)
{
case 'a':
cout<<"Please insert person's first name"<<endl;
cin>>firstname;
cout<<"please insert person's last name"<<endl;
cin>>lastname;
cout<<"please insert person's age"<<endl;
cin>>age;
tmpperson = newperson(firstname,lastname,age);
stack_push(s,tmpperson);
break;
case 'b':
tmpperson = stack_pop(s);
showperson(tmpperson);
break;
case 'c':
forward_traverse_stack(s);
break;
case 'd':
backward_traverse_stack(s);
break;
default:
cout<<"no such command"<<endl;
break;
}
}
case 'Q':
while(op2 != 's')
{
cout<<"Choose among the following operations: through the corresponding letter:"<<endl;
cout<<"a- insert a person's info into the Queue"<<endl;
cout<<"b- exist a person's info from the Queue"<<endl;
cout<<"c- Forward Traverse the Queue"<<endl;
cout<<"d- Backward Traverse the Queue"<<endl;
cout<<"s- stop the program"<<endl;
cin>>op2;
switch(op2)
{
case 'a':
cout<<"Please insert person's first name"<<endl;
cin>>firstname;
cout<<"please insert person's last name"<<endl;
cin>>lastname;
cout<<"please insert person's age"<<endl;
cin>>age;
tmpperson = newperson(firstname,lastname,age);
enqueue(q,tmpperson);
break;
case 'b':
tmpperson = dequeue(q);
showperson(tmpperson);
break;
case 'c':
queue_traverse_forward(q);
break;
case 'd':
queue_traverse_backward(q);
break;
default:
cout<<"no such command"<<endl;
break;
}
}
}
return 0;
}