-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathC++ STL Set 5 (queue).cpp
102 lines (92 loc) · 1.72 KB
/
C++ STL Set 5 (queue).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
//{ Driver Code Starts
#include <bits/stdc++.h>
using namespace std;
void push(queue<int> &q, int x);
int pop(queue<int> &q);
int getSize(queue<int> &q);
int getBack(queue<int> &q);
int getFront(queue<int> &q);
int main()
{
// your code goes here
int t;
cin >> t;
while (t--)
{
queue<int> s;
int q;
cin >> q;
while (q--)
{
char c;
cin >> c;
if (c == 'a')
{
int x;
cin >> x;
push(s, x);
}
if (c == 'b')
{
cout << pop(s) << " ";
}
if (c == 'c')
{
cout << getSize(s) << " ";
}
if (c == 'd')
{
cout << getFront(s) << " ";
}
if (c == 'e')
{
cout << getBack(s) << " ";
}
}
cout << endl;
}
return 0;
}
// } Driver Code Ends
/*You are required to complete below methods*/
/*inserts an element x at
the back of the queue q */
void push(queue<int> &q, int x)
{
q.push(x);
}
/*pop out the front element
from the queue q and returns it */
int pop(queue<int> &q)
{
if (!q.empty())
{
int x = q.front();
q.pop();
return x;
}
return -1;
}
/*returns the size of the queue q */
int getSize(queue<int> &q)
{
return q.size();
}
/*returns the last element of the queue */
int getBack(queue<int> &q)
{
if (!q.empty())
{
return q.back();
}
return -1;
}
/*returns the first element of the queue */
int getFront(queue<int> &q)
{
if (!q.empty())
{
return q.front();
}
return -1;
}