-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedQueue.java
More file actions
244 lines (217 loc) · 4.73 KB
/
LinkedQueue.java
File metadata and controls
244 lines (217 loc) · 4.73 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
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
233
234
235
236
237
238
239
240
241
242
243
244
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.NoSuchElementException;
import java.util.Queue;
/**
* This class implements a link-based queue.
*
* @author John Kurlak (kurlak)
* Weston Thayer (weston5)
* Panhavorn Hok (onehok1)
* @version 12.07.09
* @param <T> Any generic type
*/
public class LinkedQueue<T> implements Queue<T>
{
private LinkedList<T> list;
/**
* This constructor creates an empty queue.
*/
public LinkedQueue()
{
list = new LinkedList<T>();
}
/**
* This method adds a new element into the queue, even if it is null.
*/
public boolean add(T element)
{
if (element == null)
{
throw new NullPointerException();
}
return offer(element);
}
/**
* This method returns the first element in the queue.
*
* @return Returns the first element in the queue
* @throws NoSuchElementException
*/
public T element()
{
if (list.isEmpty())
{
throw new NoSuchElementException();
}
return list.getFirst();
}
/**
* This method adds a new element into the queue.
*/
public boolean offer(T element)
{
list.addLast(element);
return true;
}
/**
* This method returns the first element in the queue.
*
* @return Returns the first element in the queue and null if the queue is
* empty
*/
public T peek()
{
if (list.isEmpty())
{
return null;
}
return list.getFirst();
}
/**
* This method removes the first element from the queue and returns it.
*
* @return Returns the element that was removed from the queue
* @throws NoSuchElementException
*/
public T remove()
{
if (list.isEmpty())
{
throw new NoSuchElementException();
}
return poll();
}
/**
* This method returns the first element of the queue.
*
* @return Returns the first element in the queue
*/
public T poll()
{
return list.removeFirst();
}
/**
* This method is not implemented.
*
* @throws UnsupportedOperationException
*/
public boolean addAll(Collection<? extends T> arg0)
{
throw new UnsupportedOperationException();
}
/**
* This method empties the queue.
*/
public void clear()
{
list.clear();
}
/**
* This method determines whether or not the given element is in the queue.
*
* @return Returns true if the given element is in the queue and false if
* it is not
*/
public boolean contains(Object element)
{
return list.contains(element);
}
/**
* This method returns true if the queue contains all of the elements in the
* specified collection.
*
* @return Returns true if the queue contains all of the elements in the
* specified collection.
*/
public boolean containsAll(Collection<?> collection)
{
Iterator<?> i = collection.iterator();
boolean hasAll = true;
while (i.hasNext())
{
if (!list.contains(i.next()))
{
hasAll = false;
}
}
return hasAll;
}
/**
* This method determines whether or not the queue is empty.
*
* @return Returns true if the queue is empty and false if it is not
*/
public boolean isEmpty()
{
return list.isEmpty();
}
/**
* This method gets a new iterator for the current queue.
*
* @return Returns an iterator to the queue
*/
public Iterator<T> iterator()
{
return list.iterator();
}
/**
* This method removes an object from the queue.
*
* @return Returns true if the object was successfully removed, false if
* it was not
*/
public boolean remove(Object element)
{
return list.remove(element);
}
/**
* This method is not implemented.
*
* @return Not used
* @throws UnsupportedOperationException
*/
public boolean removeAll(Collection<?> collection)
{
throw new UnsupportedOperationException();
}
/**
* This method is not implemented.
*
* @return Not used
* @throws UnsupportedOperationException
*/
public boolean retainAll(Collection<?> collection)
{
throw new UnsupportedOperationException();
}
/**
* This method gets the size of the queue.
*
* @return The number of elements in the queue
*/
public int size()
{
return list.size();
}
/**
* This method places all of the queue's elements in an array.
*
* @return Returns an array with the queue's elements
*/
public Object[] toArray()
{
return list.toArray();
}
/**
* This method places all the queue's elements into a generic array.
*
* @return A generic array with the queue's elements
*/
@SuppressWarnings("hiding")
public <T> T[] toArray(T[] returnType)
{
return list.toArray(returnType);
}
}