-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlb-27-stack-using-dequeue.cpp
More file actions
46 lines (42 loc) · 1.03 KB
/
Copy pathlb-27-stack-using-dequeue.cpp
File metadata and controls
46 lines (42 loc) · 1.03 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
// De-queue is a data structure where insert and delete operation can be done
// in both ends of queue in O(1) time.
// DLL with head and tail pointer
// DEQUEUE STACK QUEUE
// Insert_first() - -
// insert_last() push enqueue
// remove_fist - dequeue
// remove_last pop -
#include <deque>
#include <iostream>
using namespace std;
class Stack {
public:
deque<int> dq;
void push(int val) {
dq.push_back(val);
}
int pop() {
if (dq.empty()) return -1;
int val = dq.back();
dq.pop_back();
return val;
}
int size() {
return dq.size();
}
};
int main() {
Stack s;
s.push(1);
s.push(3);
s.push(2);
cout << s.pop() << endl;
s.push(5);
s.push(7);
cout << s.pop() << endl;
cout << s.size() << endl;
}
// DeQ can be implemented using the doubly linked list
// with head and tail pointer
// insert and delete at head
// insert and delete from tail