-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue.cpp
More file actions
84 lines (77 loc) · 1.17 KB
/
queue.cpp
File metadata and controls
84 lines (77 loc) · 1.17 KB
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
#include "queue.h"
struct Node
{
int key;
Node *next;
};
Queue::Queue()
{
this->front_ = nullptr;
this->back_ = nullptr;
}
void Queue::push(int k)
{
if (this->count() == 0)
{
Node *x = new Node;
x->key = k;
x->next = nullptr;
this->front_ = x;
this->back_ = x;
}
else
{
Node *x = new Node{k, nullptr};
this->back_->next = x;
this->back_ = x;
}
}
void Queue::pop()
{
if (this->count() == 0)
{
throw EmptyException();
}
else if (this->count() == 1)
{
this->front_ = this->back_ = nullptr;
}
else
{
Node *x = new Node{this->front_->next->key, this->front_->next->next};
this->front_ = x;
}
}
int Queue::front() const
{
if (this->count() == 0)
{
throw EmptyException();
}
else
{
return this->front_->key;
}
}
int Queue::back() const
{
if (this->count() == 0)
{
throw EmptyException();
}
else
{
return this->back_->key;
}
}
int Queue::count() const
{
Node *end = this->front_;
int tamanho = 0;
while (end != nullptr)
{
tamanho++;
end = end->next;
}
return tamanho;
}