-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue.cpp
More file actions
40 lines (36 loc) · 764 Bytes
/
queue.cpp
File metadata and controls
40 lines (36 loc) · 764 Bytes
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
#include "queue.hpp"
Queue::Queue() {
size = 0;
head = nullptr;
tail = nullptr;
}
Queue::~Queue() {
for(int i = 0; i < size; ++i)
pop();
}
void Queue::push(int num) noexcept {
if(tail == nullptr) {
head = new ListItem;
head->item = num;
head->p_prevItem = nullptr;
tail = head;
} else {
ListItem* tmp = new ListItem;
tail->p_prevItem = tmp;
tmp->item = num;
tail = tmp;
}
++size;
}
int Queue::pop() {
if(head != nullptr) {
ListItem tmp;
tmp.p_prevItem = head->p_prevItem;
tmp.item = head->item;
delete head;
head = tmp.p_prevItem;
--size;
return tmp.item;
}
throw EmptyListException();
}