-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueue.cpp
77 lines (71 loc) · 1.54 KB
/
queue.cpp
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
/**
* @file queue.cpp
* Implementation of the Queue class.
*
*/
/**
* Adds the parameter object to the back of the Queue.
*
* @param newItem object to be added to the Queue.
*/
template <class T> void Queue<T>::enqueue(T const &newItem)
{
stack_1.push(newItem);
}
/**
* Removes the object at the front of the Queue, and returns it to the
* caller.
*
* @return The item that used to be at the front of the Queue.
*/
template <class T> T Queue<T>::dequeue()
{
if (stack_2.isEmpty()) {
while (!stack_1.isEmpty()) {
stack_2.push(stack_1.pop());
}
}
return stack_2.pop();
}
/**
* Adds an element to the ordering structure.
*
* @see OrderingStructure::add()
*/
template <class T> void Queue<T>::add(const T &theItem)
{
enqueue(theItem);
}
/**
* Removes an element from the ordering structure.
*
* @see OrderingStructure::remove()
*/
template <class T> T Queue<T>::remove()
{
return dequeue();
}
/**
* Finds the object at the front of the Queue, and returns it to the
* caller. Unlike dequeue(), this operation does not alter the queue.
*
* @return The item at the front of the queue.
*/
template <class T> T Queue<T>::peek()
{
if (stack_2.isEmpty()) {
while (!stack_1.isEmpty()) {
stack_2.push(stack_1.pop());
}
}
return stack_2.peek();
}
/**
* Determines if the Queue is empty.
*
* @return bool which is true if the Queue is empty, false otherwise.
*/
template <class T> bool Queue<T>::isEmpty() const
{
return stack_1.isEmpty() && stack_2.isEmpty();
}