-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathimplementation_of_dequeue_using_doubly_linkedlist.cpp
158 lines (156 loc) · 3.46 KB
/
implementation_of_dequeue_using_doubly_linkedlist.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
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
#include<bits/stdc++.h>
using namespace std;
// class representing a tree node
class Node {
public:
int data;
Node *next;
Node *prev;
Node(int d) {
data = d;
next = NULL;
prev = NULL;
}
};
// function to create a new node
Node* newNode(int x) {
Node *node = new Node(x);
return node;
}
// front points to start of Deque and rear points to the end of Deque
Node *front = NULL;
Node *rear = NULL;
// Variable representing size of Deque
int Size = 0;
void insertFront(int x) {
// Create a new Node with required parameters
Node *node = newNode(x);
if (front == NULL) {
// This is the first node to be inserted
front = rear = node;
} else {
// Add the node before front
node->next = front;
front->prev = node;
// update front
front = node;
}
// Increment size
Size++;
}
void insertEnd(int x) {
// Create a new Node with required parameters
Node *node = newNode(x);
if (rear == NULL) {
// This is the first node to be inserted
front = rear = node;
} else {
// Insert the node after rear
node->prev = rear;
rear->next = node;
// update rear
rear = node;
}
// Increment size
Size++;
}
void deleteFront() {
if (front == NULL) {
// no node to delete
return;
}
if (front == rear) {
// only 1 node is present
front = rear = NULL;
} else {
// delete front and move front ahead
Node *temp = front;
front = front->next;
front->prev = NULL;
// deallocate the memory taken by temp
delete(temp);
}
// Decrement size
Size--;
}
void deleteEnd() {
if (rear == NULL) {
// no node to delete
return;
}
if (front == rear) {
// only 1 node is present
front = rear = NULL;
} else {
// delete rear and move rear backwards
Node *temp = rear;
rear = rear->prev;
rear->next = NULL;
// deallocate the memory taken by temp
delete(temp);
}
// Decrement size
Size--;
}
int getFront() {
if (front != NULL) {
return front->data;
}
return -1;
}
int getEnd() {
if (rear != NULL) {
return rear->data;
}
return -1;
}
int size() {
return Size;
}
bool isEmpty() {
if (front == NULL) {
return true;
}
return false;
}
void erase() {
// mark rear as null
rear = NULL;
// traverse the doubly linked list
while (front != NULL) {
Node *temp = front;
// delete all the prev pointers
front->prev = NULL;
front = front->next;
// Deallocate the memory taken by temp
delete(temp);
}
// Set size as 0
Size = 0;
}
int main() {
// Example
insertFront(5); // 5
insertEnd(10); // 5 <-> 10
insertEnd(11); // 5 <-> 10 <-> 11
insertFront(19); // 19 <-> 5 <-> 10 <-> 11
cout<<getFront()<<endl;
cout<<getEnd()<<endl;
deleteEnd(); // 19 <-> 5 <-> 10
cout<<getEnd()<<endl;
deleteFront(); // 5 <-> 10
cout<<getFront()<<endl;
cout<<size()<<endl;
if (isEmpty()) {
cout<<"true"<<endl;
} else {
cout<<"false"<<endl;
}
erase();
if (isEmpty()) {
cout<<"true"<<endl;
} else {
cout<<"false"<<endl;
}
return 0;
}