-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue_with_linked_list_stl.h
More file actions
66 lines (57 loc) · 1.63 KB
/
queue_with_linked_list_stl.h
File metadata and controls
66 lines (57 loc) · 1.63 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
#ifndef DATA_STRUCTURES_QUEUE_WITH_LINKED_LIST_STL_H
#define DATA_STRUCTURES_QUEUE_WITH_LINKED_LIST_STL_H
#include <list>
namespace DataStructures {
/**
* @class QueueWithLinkedListSTL
* @brief Queue with STL linked list class and all related functionality.
* @tparam T Type of the implementation class.
*/
template <typename T>
class QueueWithLinkedListSTL {
public:
/**
* @brief Push the element at the back of the queue.
* @param element The element to be pushed.
*/
void push(const T& element) { list.push_back(element); }
/**
* @brief Pop the element from the front of the queue and return its
* value.
* @return The value of the popped element.
* @throws std::out_of_range stating that "Queue is empty".
*/
T pop()
{
if (list.empty()) {
throw std::out_of_range("Queue is empty");
}
T front = list.front();
list.pop_front();
return front;
}
/**
* @brief Get the value of the element at the front of the queue.
* @return The value of the element at the front of the queue.
* @throws std::out_of_range stating that "Queue is empty".
*/
T peek() const
{
if (list.empty()) {
throw std::out_of_range("Queue is empty");
}
return list.front();
}
/**
* @brief Get the size of the queue.
* @return The current size of the queue.
*/
size_t size() const { return list.size(); }
private:
/**
* The doubly linked list used for internally implementing the queue.
*/
std::list<T> list;
};
} // namespace DataStructures
#endif // DATA_STRUCTURES_QUEUE_WITH_LINKED_LIST_STL_H