Data Structure : Deque(Double-ended queue)
Deque
데크는 앞과 뒤에서 삽입, 삭제를 할 수 있는 자료구조임.
이중 연결 리스트를 이용해서 데크를 구현할 수 있음.
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
#include <iostream>
using namespace std;
typedef struct Qnode{
int data;
struct Qnode* llink;
struct Qnode* rlink;
} Qnode;
typedef struct PointQ {
Qnode* front, *rear;
} PointQ;
template <class T>
class Deque {
private :
int q_size=0;
PointQ* DQ = new PointQ;
public :
Deque() {
DQ->front=NULL;
DQ->rear=NULL;
}
bool empty() {
if(DQ->front == NULL ) {
return true;
}
else return false;
}
void push_front(const T& val) {
Qnode* newNode = new Qnode;
newNode->data=val;
newNode->llink=NULL;
newNode->rlink=NULL;
if(empty()) {
DQ->front=newNode;
DQ->rear=newNode;
}
else {
newNode->rlink=DQ->front;
DQ->front->llink = newNode;
DQ->front=newNode;
}
q_size++;
}
void push_back(const T& val) {
Qnode* newNode = new Qnode;
newNode->data=val;
newNode->llink=NULL;
newNode->rlink=NULL;
if(empty()) {
DQ->front=newNode;
DQ->rear=newNode;
}
else {
newNode->llink=DQ->rear;
DQ->rear->rlink = newNode;
DQ->rear=newNode;
}
q_size++;
}
void pop_front() {
if(empty()) {
cout << "Queue Empty!!" << '\n';
}
else {
Qnode* dNode = DQ->front;
if(dNode->rlink == NULL) {
DQ->front = NULL;
DQ->rear = NULL;
}
else {
DQ->front=dNode->rlink;
DQ->front->llink=NULL;
}
delete dNode;
q_size--;
}
}
void pop_back() {
if(empty()) {
cout << "Queue Empty!!" << '\n';
}
else {
Qnode* dNode = DQ->rear;
if(dNode->llink == NULL) {
DQ->front = NULL;
DQ->rear = NULL;
}
else {
DQ->rear=dNode->llink;
DQ->rear->rlink=NULL;
}
delete dNode;
q_size--;
}
}
int size() {
if(empty()) {
return 0;
}
else
return q_size;
}
int front() {
if(empty()) {
return -1;
}
else {
return DQ->front->data;
}
}
int back() {
if(empty()) {
return -1;
}
else {
return DQ->rear->data;
}
}
void printDQ() {
Qnode* pNode = DQ->front;
cout << "Node : [";
while(pNode) {
cout << " " << pNode->data;
pNode=pNode->rlink;
}
cout << " ] \n";
}
~Deque() {
delete DQ;
}
};
This post is licensed under CC BY 4.0 by the author.