-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDeque Implementations.cpp
105 lines (86 loc) · 1.95 KB
/
Deque Implementations.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
//{ Driver Code Starts
// Initial Template for C++
// CPP code to implement dequeue functions
#include <bits/stdc++.h>
using namespace std;
// } Driver Code Ends
// User function Template for C++
// dq : deque in which element is to be pushed
// x : element to be pushed
// Function to push element x to the back of the deque.
void push_back_pb(deque<int> &dq, int x)
{
dq.push_back(x);
}
// Function to pop element from back of the deque.
void pop_back_ppb(deque<int> &dq)
{
if (!dq.empty())
{
dq.pop_back();
}
}
// Function to return element from front of the deque.
int front_dq(deque<int> &dq)
{
if (!dq.empty())
{
return dq.front();
}
return -1;
}
// Function to push element x to the front of the deque.
void push_front_pf(deque<int> &dq, int x)
{
dq.push_front(x);
}
//{ Driver Code Starts.
// Driver code
int main()
{
int testcase;
cin >> testcase;
// declaring deque
deque<int> dq;
while (testcase--)
{
int queries;
cin >> queries;
while (queries--)
{
string s;
cin >> s;
// if query is to push back
if (s == "pb")
{
int x;
cin >> x;
push_back_pb(dq, x);
cout << dq.back() << endl;
}
// if query is to push front
if (s == "pf")
{
int x;
cin >> x;
push_front_pf(dq, x);
cout << dq.front() << endl;
}
// if query is to pop back
if (s == "pp_b")
{
pop_back_ppb(dq);
cout << dq.size() << endl;
}
// if query is to return front
if (s == "f")
{
int x = front_dq(dq);
cout << x << endl;
}
}
dq.clear();
}
return 0;
}
// } Driver Code Ends