-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueue.cc
232 lines (206 loc) · 6.96 KB
/
queue.cc
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/****************************************************
* File Name: queue.cc
* Author: Hayden Ivatts
* Modification Date: 10/18/18
* Adapted from: Robert Kruse and Alexander Ryba
* from "Data Structures and Program Design in C++"
* Course: CSCI 132
* Purpose: Implementation of Queue class
*****************************************************/
#include "queue.h"
Queue :: Queue(const Queue &original) // copy constructor
/**********************************************************************
* Pre: None
* Post: The Queue is initialized as a copy of Queue original.
**********************************************************************/
{
//Keep track of original front pointer
Node *old_node = original.front;
front = rear = NULL;
if(old_node != NULL) {
//Creates new node for new queue
Node *newNode = new Node(old_node->entry);
front = newNode;
while (old_node->next != NULL) {
old_node = old_node->next;
newNode->next = new Node(old_node->entry);
newNode = newNode->next;
}//end while
rear = newNode;
}//end if
}
void Queue :: operator = (const Queue &original) //overloaded assignment
/**********************************************************************
* Pre: None
* Post: The Queue is reset as a copy of Queue original.
**********************************************************************/
{
//Clears queue being changed
while(!empty()) {
serve();
}
//Copy constructor
Node *old_node = original.front;
Node *newNode;
front = rear = NULL;
if(old_node != NULL) {
Node *newNode = new Node(old_node->entry);
front = newNode;
while (old_node->next != NULL) {
old_node = old_node->next;
newNode->next = new Node(old_node->entry);
newNode = newNode->next;
}//end while
rear = newNode;
}//end if
}
Queue :: ~Queue() //deconstructor
/**********************************************************************
* Pre: None
* Post: The Queue is cleared and the memory returned to the heap.
**********************************************************************/
{
//Clears queue
while(!empty()) {
serve();
}
}
Term :: Term(int exponent, double scalar)
/**********************************************************************
* Pre: None
* Post: The Term is initialized with the given coefficient
* and exponent, or with default parameter values of 0.
**********************************************************************/
{
degree = exponent;
coefficient = scalar;
} //end Term()
Node ::Node()
/**********************************************************************
* Pre: None
* Post: An empty node with a NULL pointer to next is created.
**********************************************************************/
{
next =NULL ;
} //end Node()
Node ::Node(Node_entry item ,Node *add_on)
/**********************************************************************
* Pre: None
* Post: A Node is created with data entry of item and next pointer
* given by add_on (default is NULL)
**********************************************************************/
{
entry =item ;
next =add_on ;
} //end Node(Node_entry, Node)
int Extended_queue::size( ) const
/**********************************************************************
* Pre: None
* Post: Return the number of entries in the Extended_queue.
**********************************************************************/
{
Node *window = front;
int count = 0;
while (window != NULL) {
window = window->next;
count++;
} // end while
return count;
} //end size()
bool Extended_queue :: full() const
/**********************************************************************
* Pre: None
* Post: Return true if no room in queue.
**********************************************************************/
{
Node *new_node = new Node();
return new_node == NULL;
} //end full()
void Extended_queue::clear()
/**********************************************************************
* Pre: None
* Post: Delete all the entries of the queue.
**********************************************************************/
{
while (!empty()) {
serve();
} //end while
} //end clear()
Error_code Extended_queue :: serve_and_retrieve(Queue_entry &item)
/**********************************************************************
* Pre: None
* Post: Return entry at front of queue in item, then delete front node
**********************************************************************/
{
if( retrieve(item) == underflow) {
return underflow;
} //end if
return serve();
} //end serve_and_retrieve( )
Queue::Queue()
/**********************************************************************
* Pre: None
* Post: The Queue is initialized to be empty.
**********************************************************************/
{
front = rear = NULL;
} //end Queue()
bool Queue::empty() const
/**********************************************************************
* Pre: None
* Post: Return true if the Queue is empty, otherwise return false.
**********************************************************************/
{
return front == NULL;
} //end empty()
Error_code Queue::append(const Queue_entry &item)
/**********************************************************************
* Pre: None
* Post: item is added to the rear of the Queue. If the Queue is full
* return an Error_code of overflow and leave the Queue unchanged.
**********************************************************************/
{
Node *new_rear = new Node(item);
if (new_rear == NULL) {
return overflow;
} //end if
if (rear == NULL) {
front = rear = new_rear;
} else {
rear->next = new_rear;
rear = new_rear;
} //end if-else
return success;
} //end append()
Error_code Queue::serve()
/**********************************************************************
* Pre: None
* Post: The front of the Queue is removed. If the Queue
* is empty return an Error_code of underflow.
**********************************************************************/
{
Node *old_front;
if (front == NULL) {
return underflow;
} //end if
old_front = front;
front = old_front->next;
if (front == NULL) {
rear = NULL;
} //end if
delete old_front;
return success;
} //end serve()
Error_code Queue::retrieve(Queue_entry &item) const
/**********************************************************************
* Pre: None
* Post: The front of the Queue retrieved to the output
* parameter item. If the Queue is empty return an Error_code of underflow.
**********************************************************************/
{
if (front == NULL) {
return underflow;
} //end if
item = front->entry;
return success;
} //end retrieve()