-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGeeksforGeeks - Implement Stack and Queue using Deque - GoldmanSachs - Easy
94 lines (93 loc) · 2.2 KB
/
GeeksforGeeks - Implement Stack and Queue using Deque - GoldmanSachs - Easy
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
struct DQueNode {
int value;
DQueNode* next;
DQueNode* prev;
};
class Deque {
private:
DQueNode* head;
DQueNode* tail;
public:
Deque(){
head = tail = NULL;
}
bool isEmpty(){
if (head == NULL)
return true;
return false;
}
int size(){
if (!isEmpty()) {
DQueNode* temp = head;
int len = 0;
while (temp != NULL) {
len++;
temp = temp->next;
}
return len;
}
return 0;
}
void insert_first(int element){
DQueNode* temp = new DQueNode[sizeof(DQueNode)];
temp->value = element;
if (head == NULL) {
head = tail = temp;
temp->next = temp->prev = NULL;
}
else {
head->prev = temp;
temp->next = head;
temp->prev = NULL;
head = temp;
}
}
void insert_last(int element){
DQueNode* temp = new DQueNode[sizeof(DQueNode)];
temp->value = element;
if (head == NULL) {
head = tail = temp;
temp->next = temp->prev = NULL;
}
else {
tail->next = temp;
temp->next = NULL;
temp->prev = tail;
tail = temp;
}
}
void remove_first(){
if (!isEmpty()) {
DQueNode* temp = head;
head = head->next;
if(head) head->prev = NULL;
delete temp;
if(head == NULL) tail = NULL;
return;
}
cout << "List is Empty" << endl;
}
void remove_last(){
if (!isEmpty()) {
DQueNode* temp = tail;
tail = tail->prev;
if(tail) tail->next = NULL;
delete temp;
if(tail == NULL) head = NULL;
return;
}
cout << "List is Empty" << endl;
}
void display(){
if (!isEmpty()) {
DQueNode* temp = head;
while (temp != NULL) {
cout << temp->value << " ";
temp = temp->next;
}
cout << endl;
return;
}
cout << "List is Empty" << endl;
}
};