-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue.cpp
More file actions
111 lines (100 loc) · 2.43 KB
/
Copy pathqueue.cpp
File metadata and controls
111 lines (100 loc) · 2.43 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/**
* @file queue.cpp
* Implementation of the Queue class.
*
* @author CS 225 course staff
* @date Fall 2009
*/
/**
* 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)
{
/**
* @todo Your code here!
*/
inStack.add(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()
{
/**
* @todo Your code here! You will need to replace the following line.
*/
//To get the front (The first object we enter), we need to put the inStack in the reverse order, then "pop" the top one out
if(outStack.isEmpty())
while(!inStack.isEmpty())
{
outStack.add(inStack.pop());
}
return outStack.remove();
}
/**
* Adds an element to the ordering structure.
*
* @see OrderingStructure::add()
*/
template <class T>
void Queue<T>::add( const T & theItem ) {
/**
* @todo Your code here! Hint: this function should call a Queue
* function to add the element to the Queue.
*/
enqueue(theItem);
}
/**
* Removes an element from the ordering structure.
*
* @see OrderingStructure::remove()
*/
template <class T>
T Queue<T>::remove() {
/**
* @todo Your code here! Hint: this function should call a Queue
* function to remove an element from the Queue and return it. You will
* need to replace the following line.
*/
return (dequeue());
}
/**
* Finds the object at the front of the Queue, and returns it to the
* caller. Unlike pop(), this operation does not alter the queue.
*
* @return The item at the front of the queue.
*/
template<class T>
T Queue<T>::peek()
{
/**
* @todo Your code here! You will need to replace the following line.
*/
//Again we need to put InStack into the OutStack to get the reverse order, then access the "top" element
if(outStack.isEmpty())
while(!inStack.isEmpty())
{
outStack.push(inStack.pop());
}
return outStack.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
{
/**
* @todo Your code here! You will need to replace the following line.
*/
return inStack.isEmpty() && outStack.isEmpty();
}