-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
QueueReferenceBased.java
104 lines (94 loc) · 2.77 KB
/
QueueReferenceBased.java
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
/*
* @author Skully (https://github.com/ImSkully)
* @email [email protected]
*/
public class QueueReferenceBased implements QueueInterface {
private Node lastNode;
private int size;
// Default constructor.
public QueueReferenceBased() {
lastNode = null;
}
/**
* dequeueAll()
* Removes all items from the queue.
*/
public void dequeueAll() {
lastNode = null;
}
/**
* enqueue(Object newItem)
* Adds a new item to the end of the queue.
*
* @param newItem The item to be added to the queue.
* @returns true if the item was successfully added, false otherwise.
*/
public void enqueue(Object newItem) {
Node newNode = new Node(newItem);
size++; // Increment the queue size.
// Insert the new node.
if (isEmpty()) {
// Insertion into empty queue.
newNode.setNext(newNode);
} else {
// Insertion into non-empty queue.
newNode.setNext(lastNode.getNext());
lastNode.setNext(newNode);
}
lastNode = newNode; // new node is at back.
}
/**
* dequeue()
* Removes the item at the front of the queue.
*
* @return The item that was removed from the queue.
* @throws QueueException if the queue is empty.
*/
public Object dequeue() throws QueueException {
if (!isEmpty()) {
size--; // Decrement the queue size.
// Remove the front node since the queue is not empty.
Node firstNode = lastNode.getNext();
if (firstNode == lastNode) {
lastNode = null;
} else {
lastNode.setNext(firstNode.getNext());
}
return firstNode.getItem();
} else {
throw new QueueException("QueueException on dequeue: queue empty");
}
}
/**
* peek()
*
* @return The object at the front of the queue.
*/
public Object peek() throws QueueException {
if (!isEmpty()) {
// Queue is not empty; get the first node.
Node firstNode = lastNode.getNext();
return firstNode.getItem();
} else {
throw new QueueException("QueueException on peek: queue empty");
}
}
/**
* getSize()
* Gets the current size of the queue.
*
* @return The size of the queue.
*/
public int getSize() {
return this.size;
}
/**
* isEmpty()
* Checks if the queue is empty.
*
* @return true if the queue is empty, false otherwise.
*/
public boolean isEmpty() {
return lastNode == null;
}
}