-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueue.cpp
116 lines (101 loc) · 2.71 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
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
112
113
114
115
116
/*
* File: queue.cpp
* Description: Implementation of the Queue class.
*
* Complete your implementation in this file
*/
/*
* Adds the parameter object to the back of the Queue.
*
* NOTE: This fuction should have O(1) behavior over n operations!
*
* PARAM: item - object to be added to the Queue.
*/
template <class T>
void Queue<T>::Enqueue(T const& item)
{
// complete your implementation below
while (!stack_1.IsEmpty()) {
stack_2.Push(stack_1.Pop());
}
stack_1.Push(item);
while (!stack_2.IsEmpty()) {
stack_1.Push(stack_2.Pop());
}
}
/*
* Removes the object at the front of the Queue, and returns it to
* the caller. You may assume that this function is only called
* when the Queue is non-empty.
*
* NOTE: This function should have O(1) behavior over n operations!
*
* RETURN: the item that used to be at the front of the Queue.
*/
template <class T>
T Queue<T>::Dequeue()
{
// complete your implementation below
T item = stack_1.Pop(); // REPLACE THESE LINES
return item; // REPLACE THESE LINES
}
/*
* Adds an element to the ordering structure.
*
* See OrderingStructure::Add()
*/
template <class T>
void Queue<T>::Add(const T& item)
{
// complete your implementation below
// Hint: this function should call a Queue
// function to add the element to the Queue.
Enqueue(item);
}
/*
* Removes an element from the ordering structure.
*
* See OrderingStructure::Remove()
*/
template <class T>
T Queue<T>::Remove()
{
// complete your implementation below
// 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.
T item = Dequeue(); // REPLACE THESE LINES
return item; // REPLACE THESE LINES
}
/*
* Finds the object at the front of the Queue, and returns it to
* the caller. Unlike Dequeue(), this operation does (conceptually!) not alter the
* Queue; however, you may need to alter the internal representation of the Queue.
* You may assume that this function is only called when the
* Queue is non-empty.
*
* NOTE: This function should have O(1) behavior over n operations!
*
* RETURN: the item at the front of the queue.
*/
template <class T>
T Queue<T>::Peek()
{
// complete your implementation below
T item = stack_1.Peek(); // REPLACE THESE LINES
return item; // REPLACE THESE LINES
}
/*
* Determines if the Queue is empty.
*
* RETURN: true if the stack is empty,
* false otherwise.
*
* NOTE: This function must always run in O(1) time!
*/
template <class T>
bool Queue<T>::IsEmpty() const
{
// complete your implementation below
return stack_1.IsEmpty(); // REPLACE THIS STUB
}