-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathqueue_dll.h
84 lines (66 loc) · 1.83 KB
/
queue_dll.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
#pragma once
// A Queue class based on a DLL implementation.
// Delegates the work to a DLList object.
//
// A queue can be implemented as a linked list by adding to one end
// and deleting from the other end. For example:
// add_to_tail() + remove_head()
// or add_to_head() + remove_tail()
//
// remove_tail() should be avoided with singly-linked lists.
#include "dll.h"
template <class T>
class QueueDLL
{
public:
void enqueue(const T& val); // adds an element to the queue.
T dequeue(); // returns and removes the first
// element in the queue.
T get_first() const; // returns the first element added to the queue.
T get_last() const; // returns the last element added to the queue.
bool is_empty() const;
void clear();
private:
DLList<T> list;
};
// NOTE. The default constructor and destructor of QueueDLL
// automatically call the default constructor and destructor
// of DLList.
template <class T>
void QueueDLL<T>::enqueue(const T& val)
{
list.add_to_tail(val);
}
// Removes and returns the first added element.
// Throws an exception if the queue is empty.
template <class T>
T QueueDLL<T>::dequeue()
{
T val = list.head_val();
list.remove_head();
return val;
}
// Returns the first added element.
// Throws an exception if the queue is empty.
template <class T>
T QueueDLL<T>::get_first() const
{
return list.head_val();
}
// Returns the last added element.
// Throws an exception if the queue is empty.
template <class T>
T QueueDLL<T>::get_last() const
{
return list.tail_val();
}
template <class T>
bool QueueDLL<T>::is_empty() const
{
return list.is_empty();
}
template <class T>
void QueueDLL<T>::clear()
{
list.clear();
}