forked from MissMeriel/ShoppingCenterSystem
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueue.java
More file actions
150 lines (138 loc) · 3.24 KB
/
Queue.java
File metadata and controls
150 lines (138 loc) · 3.24 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
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
package master1;
/**
* Array-based implementation of the Queue ADT.
* @author Jon Spratt, Meriel Stein
*
* @param <T> the type of item stored in this ADT
*/
public class Queue< T extends KeyedItem<?>> implements QueueInterface <T> {
/**
* The collection of items
*/
protected T[] items;
/**
* The index of the front of the queue
*/
protected int front;
/**
* The index of the back of the queue
*/
protected int back;
/**
* The amount of items currently stored in the collection
*/
protected int numItems;
/**
* Constructor for Queue ADT
*/
@SuppressWarnings("unchecked")
public Queue() {
items = (T[]) new KeyedItem[3];
front = 0;
back = items.length - 1;
numItems = 0;
}
/**
* Determine whether or not the collection is empty
* @return returns true if the collection is empty, false otherwise
*/
@Override
public boolean isEmpty() {
return numItems == 0;
}
/**
* Add an item to the back of the queue
* @param newItem the item to be added to the queue
*/
@Override
public void enqueue(T newItem) throws QueueException {
if (numItems == items.length) {
resize();
}
back = (back + 1) % items.length;
items[back] = (T) newItem;
numItems++;
}
/**
* Remove an item from the front of the queue
* @return returns the item that was removed from the front of the queue
*/
@Override
public T dequeue() throws QueueException {
T myItem = null;
if (numItems == 0) {
throw new QueueException("Queue is empty.");
}
else {
myItem = items[front];
items[front] = null;
front = (front + 1) % items.length;
numItems--;
}
return myItem;
}
/**
* Remove all items from the collection
*/
@SuppressWarnings("unchecked")
@Override
public void dequeueAll() {
items = (T[]) new Object[3];
numItems = 0;
}
/**
* Retrieve the item at the front of the queue and leave in place
* @return returns the item at the front of the queue
*/
@Override
public T peek() throws QueueException {
if (numItems == 0) {
throw new QueueException("Queue empty.");
}
return items[front];
}
/**
* Resize the underlying collection to make room for more items
*/
@SuppressWarnings("unchecked")
protected void resize() {
T[] temp = (T[]) new Object[(int)(items.length * 1.5)];
for (int i = 0; i < numItems; i++) {
temp[i] = items[(front + i) % items.length];
}
items = temp;
front = 0;
back = numItems - 1;
}
/**
* Determine the length of the queue
* @return returns the number of items in the collection
*/
public int getLength(){
return numItems;
}
/**
* Determine whether or not the queue contains a specific item
* @param item the item to check for
* @return returns true if the item in in the queue, false otherwise
*/
public boolean contains(T item){
for(int i = 0; i< numItems; i++){
if(item.getKey().equals(items[(front + i) % items.length].getKey())){
return true;
}
}
return false;
}
/**
* Standard toString method to represent all of the items in the collection
* @return returns a String of all items in the collection
*/
public String toString() {
String myString = "";
for (int i = 0, j = front; i < numItems; i++, j = (j+1) % items.length) {
myString += items[j].toString() + " ";
}
return myString;
}
}