This repository has been archived by the owner on Nov 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDeque.h
167 lines (148 loc) · 3.72 KB
/
Deque.h
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
159
160
161
162
163
164
165
166
167
/* Copyright 2018 Margineanu Nicolae-Vladut */
#ifndef DEQUE_H__
#define DEQUE_H__
template <typename T>
struct Node {
T data;
Node<T> *next;
Node<T> *prev;
// Constructor
explicit Node(T data) {
this->data = data;
next = nullptr;
prev = nullptr;
}
};
template <typename T>
class Deque {
private:
Node<T> *head;
Node<T> *tail;
int numElements;
public:
// Conctructor
Deque() {
head = nullptr;
tail = nullptr;
numElements = 0;
}
// Copy-constructor
// Coada mea nu trebuie sa copieze o alta coada
Deque(const Deque &object) {}
// Destructor
~Deque() {
if (!this->isEmpty()) {
Node<T> *aux = head;
while (head) {
head = head->next;
delete aux;
aux = head;
}
}
}
// Copy-assignment
// Coada mea nu trebuie sa i se atribuie unei alte cozii
Deque& operator=(const Deque& obj) {}
// adaugare element la sfarsit
void push_back(T data) {
if (isEmpty()) {
head = new Node<T>(data);
tail = head;
} else {
Node<T> *aux = new Node<T>(data);
aux->prev = tail;
tail->next = aux;
tail = aux;
}
++numElements;
}
// adaugare element la inceput
void push_front(T data) {
if (isEmpty()) {
head = new Node<T>(data);
tail = head;
} else {
Node<T> *aux = new Node<T>(data);
aux -> next = head;
head -> prev = aux;
head = aux;
}
++numElements;
}
// stergere element la sfarsit
void pop_back() {
if (!isEmpty()) {
if (head == tail) {
// deque are un singur nod
delete head;
head = tail = nullptr;
} else {
tail = tail->prev;
delete tail->next;
tail->next = nullptr;
}
--numElements;
}
}
// stergere element la inceput
void pop_front() {
if (!isEmpty()) {
if (head == tail) {
delete head;
head = tail = nullptr;
} else {
head = head->next;
delete head->prev;
head->prev = nullptr;
}
--numElements;
}
}
// afisare ultimul element
T& back() {
return tail->data;
}
// afisare primul element
T& front() {
return head->data;
}
bool isEmpty() {
return (!numElements) ? true : false;
}
int size() {
return numElements;
}
Node<T> *getHead() {
return head;
}
// supraincarcarea operatorului ostream <<
template <typename U>
friend std::ostream& operator<<(std::ostream& out, Deque<U>& list);
};
/* Operatorul << (ostream) este prieten cu clasa Deque, deci ii poate
accesa membrii, chiar daca acestia sunt private. */
template <typename T>
std::ostream& operator<<(std::ostream& out, Deque<T>& list) {
Node<T> *curr = list.getHead();
if (list.size() > 0) {
while (curr->next != nullptr) {
if (curr->data.CommandType == "ADD_GET_BOX") {
out << "GET ";
} else {
out << "DROP ";
}
out << curr->data.x << ' ' <<curr->data.y
<< ' ' <<curr->data.NrBoxes << "; ";
curr = curr->next;
}
if (curr->data.CommandType == "ADD_GET_BOX") {
out << "GET ";
} else {
out << "DROP ";
}
out << curr->data.x << ' ' <<curr->data.y
<< ' ' <<curr->data.NrBoxes;
}
return out;
}
#endif // DEQUE_H__