Skip to content

Commit 32c6d26

Browse files
committed
implemented Interleaving queue halves
1 parent e171d85 commit 32c6d26

File tree

2 files changed

+174
-0
lines changed

2 files changed

+174
-0
lines changed
Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
#include<iostream>
2+
#include<stack>
3+
#include<queue>
4+
5+
//Interleaving the 2 halves of a queue
6+
using namespace std;
7+
8+
stack<int>s;
9+
10+
11+
void Enqueue(queue<int>&q,int data);
12+
int Dequeue(queue<int>&q);
13+
void display(queue<int>q);
14+
15+
16+
17+
18+
void InterleaveQueue(queue<int>&q)
19+
{
20+
21+
if(q.empty()) {
22+
cout<<"Queue is Empty"<<endl;
23+
return;
24+
}
25+
if(q.size()%2 != 0) { //if queue has odd size
26+
cout<<"Please enter even number of elements in queue"<<endl;
27+
28+
}
29+
30+
int halfsize = (q.size()/2);
31+
32+
33+
//Pushing half items of queue to stack
34+
for(int i=0;i<halfsize;i++) {
35+
36+
s.push(Dequeue(q));
37+
38+
}
39+
40+
//now until stack is empty , Enqueue half items from stack to queue from rear of queue
41+
42+
while(!s.empty()) {
43+
Enqueue(q,s.top());
44+
s.pop();
45+
}
46+
47+
//now again dequeue half items of queue from front and enqueue them to back
48+
49+
for(int i=0;i<halfsize;i++) {
50+
51+
Enqueue(q,Dequeue(q));
52+
53+
}
54+
55+
//now Dequeue from front of queue and push them to stack
56+
for(int i=0;i<halfsize;i++) {
57+
58+
s.push(Dequeue(q));
59+
60+
}
61+
62+
//magic happens here
63+
while(!s.empty()) {
64+
//1)pop from stack and enqueue them to queue from back
65+
Enqueue(q,s.top());
66+
s.pop();
67+
68+
//2)Now dequeue from front and enqueue to queue from rear end
69+
70+
Enqueue(q,Dequeue(q));
71+
72+
73+
}
74+
//printing the interleaved queue
75+
display(q);
76+
cout<<"Queue interleaved"<<endl;
77+
}
78+
79+
80+
81+
82+
void Enqueue(queue<int>&q,int data) {
83+
84+
q.push(data); //simply Enqueue data into queue from front
85+
86+
}
87+
88+
89+
int Dequeue(queue<int>&q) {
90+
91+
if(q.empty()) {
92+
cout<<"Queue is empty"<<endl;
93+
return 0;
94+
}
95+
cout<<"\n";
96+
//now dequeue from the front of queue
97+
int item=q.front();
98+
q.pop();
99+
100+
return item;
101+
102+
}
103+
104+
105+
void display(queue<int>q)
106+
{
107+
if(q.empty()) {
108+
cout<<"Queue is Empty"<<endl;
109+
return;
110+
}
111+
112+
while(!q.empty()) {
113+
114+
cout<<" | "<<q.front()<< " | ";
115+
q.pop();
116+
}
117+
118+
119+
}
120+
121+
122+
123+
124+
int main() {
125+
int choice;
126+
127+
queue<int>q;
128+
129+
130+
while(1){
131+
cout<<"\n";
132+
cout<<"\n1)Insert\n2)Delete\n3)Interleave Queue\n4)Display\n5)Exit\n"<<endl;
133+
cin>>choice;
134+
cout<<"\n";
135+
136+
switch(choice) {
137+
138+
case 1:
139+
int data;
140+
cout<<"Enter data :"<<endl;
141+
cin>>data;
142+
Enqueue(q,data);
143+
break;
144+
case 2:
145+
146+
cout<<"Deleted :"<<Dequeue(q);
147+
break;
148+
149+
case 3:
150+
InterleaveQueue(q);
151+
break;
152+
153+
case 4:
154+
display(q);
155+
break;
156+
157+
default:
158+
exit(0);
159+
break;
160+
161+
}
162+
}
163+
return 0;
164+
165+
}
166+
167+
168+
169+
170+
171+
172+
173+

FriendFunction.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ int main() {
3838
Human *obj=new Human(20,2004444,"Anish Singh Walia");
3939
display(obj);
4040

41+
4142
return 0;
4243

4344
}

0 commit comments

Comments
 (0)