-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack using Queue.cpp
More file actions
182 lines (136 loc) · 2.23 KB
/
Stack using Queue.cpp
File metadata and controls
182 lines (136 loc) · 2.23 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
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
#include <iostream>
#include <conio.h>
#include <string>
using namespace std;
int push_count=0;
struct node
{
int info;
node *link;
};
struct queue
{
node *queueFront, *queueRear;
queue ()
{
queueFront=NULL;
queueRear=NULL;
}
bool emptyQueue ()
{
return (queueFront==NULL);
}
bool fullQueue ()
{
return false;
}
void initialize ()
{
node *temp;
while ( queueFront!=NULL )
{
temp=queueFront;
queueFront=queueFront->link;
delete temp;
}
}
int enqueue (int item)
{
node *newNode = new node;
newNode->info=item;
newNode->link=NULL;
if (queueFront==NULL)
{
queueFront=newNode;
queueRear=newNode;
}
else
{
queueRear->link=newNode;
queueRear=queueRear->link;
}
// cout<<endl<<"The queue has been enqueued with: "<<item<<endl;
return item;
}
int dequeue ()
{
node *temp;
if (!emptyQueue())
{
int a = queueFront->info;
// cout<<endl<<"The queue has been dequeued of: "<<queueFront->info<<endl;
temp=queueFront;
queueFront=queueFront->link;
delete temp;
if (queueFront == NULL)
queueRear=NULL;
return a;
}
else
{
cout<<"Empty Queue."<<endl;
return 0;
}
}
void peek () // void front
{
if (queueFront!=NULL)
cout<<endl<<"The top element in the queue is: "<<queueFront->info<<endl;
}
void back ()
{
if (queueRear!=NULL)
cout<<endl<<"The last item in the queue is: "<<queueRear->info<<endl;
}
void display()
{
node *current;
cout<<endl<<endl<<"The remaining items in the stack are: ";
current=queueFront;
while (current!=NULL)
{
cout<<current->info<<" ";
current=current->link;
}
}
};
struct stack
{
queue q1, q2;
void push(int x)
{
while(!q1.emptyQueue())
{
q2.enqueue(q1.dequeue());
}
q1.enqueue(x);
while(!q2.emptyQueue())
{
q1.enqueue(q2.dequeue());
}
cout<<endl<<"The stack has been pushed with: "<<x<<endl;
}
void pop()
{
int p=q1.dequeue();
cout<<endl<<"The stack has been popped of: "<<p;
}
void display()
{
q1.display();
}
};
void main ()
{
stack s;
int keys [] = {1, 2, 3, 4, 5};
for (int key : keys)
{
s.push(key);
push_count++; //globally delcared and initialized
}
s.pop();
s.pop();
s.display();
_getch();
}