-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathQueue_using_two_stack.cpp
More file actions
171 lines (153 loc) · 4.2 KB
/
Queue_using_two_stack.cpp
File metadata and controls
171 lines (153 loc) · 4.2 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
//https://www.hackerrank.com/contests/cse205-16920-day24
/*
A queue is an abstract data type that maintains the order in which elements were added to it, allowing the oldest elements to be removed from the front and new elements to be added to the rear. This is called a First-In-First-Out (FIFO) data structure because the first element added to the queue (i.e., the one that has been waiting the longest) is always the first one to be removed.
A basic queue has the following operations:
Enqueue: add a new element to the end of the queue.
Dequeue: remove the element from the front of the queue and return it.
In this challenge, you must first implement a queue using two stacks. Then process queries, where each query is one of the following types:
1 x: Enqueue element into the end of the queue.
2: Dequeue the element at the front of the queue.
3: Print the element at the front of the queue.
Input Format
The first line contains a single integer, , denoting the number of queries.
Each line of the subsequent lines contains a single query in the form described in the problem statement above. All three queries start with an integer denoting the query , but only query is followed by an additional space-separated value, , denoting the value to be enqueued.
Constraints
It is guaranteed that a valid answer always exists for each query of type .
Output Format
For each query of type , print the value of the element at the front of the queue on a new line.
Sample Input
STDIN Function
----- --------
10 q = 10 (number of queries)
1 42 1st query, enqueue 42
2 dequeue front element
1 14 enqueue 42
3 print the front element
1 28 enqueue 28
3 print the front element
1 60 enqueue 60
1 78 enqueue 78
2 dequeue front element
2 dequeue front element
Sample Output
14
14
Explanation
Perform the following sequence of actions:
Enqueue ; .
Dequeue the value at the head of the queue, ; .
Enqueue ; .
Print the value at the head of the queue, ; .
Enqueue ; .
Print the value at the head of the queue, ; .
Enqueue ; .
Enqueue ; .
Dequeue the value at the head of the queue, ; .
Dequeue the value at the head of the queue, ; .
*/
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
class queue
{
int data;
queue *next=NULL;
queue *pre=NULL;
public:
queue *top=NULL;
void push(int x)
{ queue *p=NULL;
if(top==NULL)
{
top=new queue;
top->data=x;
}
else
{
p=new queue;
p->data=x;
top->next=p;
p->pre=top;
top=p;
}
}
int pop()
{
int deleted=-1;;
if(top!=NULL && top->pre ==NULL)
{
deleted = top->data;
top=NULL;
}
else if(top!=NULL)
{
deleted = top->data;
top=top->pre;
top->next=NULL;
}
return deleted;
}
void print()
{
if(top!=NULL)
cout<< top->data<<endl;
}
};
queue input,output;
void enqueue(int x)
{
input.push(x);
}
void dequeue()
{
if(output.top!=NULL)
{
output.pop();
}
else
{
while(input.top!=NULL)
{
output.push(input.pop());
}
output.pop();
}
}
void print()
{
if(output.top!=NULL)
{
output.print();
}
else
{
while(input.top!=NULL)
{
output.push(input.pop());
}
if(output.top!=NULL)
output.print();
}
}
int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
int query;
int choice;
cin>>query;
for(int i=0;i<query;i++)
{
cin>>choice;
switch(choice)
{
case 1: int x;
cin >> x;
enqueue(x); break;
case 2: dequeue(); break;
case 3: print(); break;
}
}
return 0;
}