forked from somma/_MyLib
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueue.h
More file actions
72 lines (62 loc) · 1.86 KB
/
Queue.h
File metadata and controls
72 lines (62 loc) · 1.86 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
/* The following code example is taken from the book
* "The C++ Standard Library - A Tutorial and Reference"
* by Nicolai M. Josuttis, Addison-Wesley, 1999
*
* (C) Copyright Nicolai M. Josuttis 1999.
* Permission to copy, use, modify, sell and distribute this software
* is granted provided this copyright notice appears in all copies.
* This software is provided "as is" without express or implied
* warranty, and with no claim as to its suitability for any purpose.
*/
/* ************************************************************
* Queue.hpp
* - safer and more convenient queue class
* ************************************************************/
#ifndef QUEUE_HPP
#define QUEUE_HPP
#include <deque>
#include <exception>
template <class T>
class Queue {
protected:
std::deque<T> c; // container for the elements
public:
/* exception class for pop() and top() with empty queue
*/
class ReadEmptyQueue : public std::exception {
public:
virtual const char* what() const throw() {
return "read empty queue";
}
};
// number of elements
typename std::deque<T>::size_type size() const {
return c.size();
}
// is queue empty?
bool empty() const {
return c.empty();
}
// insert element into the queue
void push (const T& elem) {
c.push_back(elem);
}
// read element from the queue and return its value
T pop () {
if (c.empty()) {
throw ReadEmptyQueue();
}
T elem(c.front());
c.pop_front();
return elem;
}
// return value of next element
T& front () {
if (c.empty()) {
throw ReadEmptyQueue();
}
return c.front();
}
void clear () { c.clear(); }
};
#endif /* QUEUE_HPP */