-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueue.java
More file actions
72 lines (64 loc) · 1.9 KB
/
Queue.java
File metadata and controls
72 lines (64 loc) · 1.9 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
import java.util.*;
/**
* A generic implementation of the Queue ADT using Java's built-in {@code LinkedList}
* @author Taskin Saadman
*/
public class Queue<T> implements Iterable<T> {
private List<T> queue = new LinkedList<T>();
/**
* Adds an element to the rear of the queue.
*
* @param value the element to be added to the queue
*/
public void enqueue(T value) {
queue.add(value);
}
/**
* Removes and returns the element at the front of the queue.
*
* @return the element at the front of the queue
* @throws RuntimeException if the queue is empty
*/
public T dequeue() throws RuntimeException {
if (queue.size() == 0) throw new RuntimeException("Queue is empty!");
T retVal = queue.get(0);
queue.remove(0);
return retVal;
}
/**
* Returns the element at the front of the queue without removing it.
*
* @return the element at the front of the queue
* @throws RuntimeException if the queue is empty
*/
public T peek() throws RuntimeException {
if (queue.size() == 0) throw new RuntimeException("Queue is empty!");
return queue.get(0);
}
/**
* Tests if the queue is empty.
*
* @return {@code true} if the queue contains no elements; {@code false} otherwise
*/
public boolean isEmpty() {
return queue.size() == 0;
}
/**
* Returns the size of the Queue.
*
* @return size
*/
public int size() {
return queue.size();
}
/**
* Returns an iterator over the elements in the queue in FIFO order (front to rear).
* The iterator starts from the front of the queue and moves toward the rear.
*
* @return an iterator over the elements in the queue
*/
@Override
public Iterator<T> iterator() {
return queue.iterator(); //List's built-in iterator
}
}