-
Notifications
You must be signed in to change notification settings - Fork 497
/
Queue.java
143 lines (125 loc) · 3.84 KB
/
Queue.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
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
import java.util.NoSuchElementException;
/**
* Implements https://docs.oracle.com/javase/8/docs/api/java/util/Queue.html
*/
public class Queue<T> {
private int total;
// Linked Lists used to store the data
private Node first, last;
private class Node {
private T data;
private Node next;
public Node(T data) {
this.data = data;
}
}
/**
* Inserts the specified element into this queue if it is possible to do so immediately without
* violating capacity restrictions, returning true upon success and throwing an
* IllegalStateException if no space is currently available.
*
* @param element the element to add
* @return true upon success, throwing an IllegalStateException if no space is currently
* available.
*/
public boolean add(T element) {
Node newNode = new Node(element);
Node current = last;
last = newNode;
if (total == 0) {
first = last;
} else {
current.next = last;
}
total++;
return true;
}
/**
* Inserts the specified element into this queue if it is possible to do so immediately without
* violating capacity restrictions.
*
* @param element the element to add
* @return true if the element was added to this queue, else false
*/
public boolean offer(T element) {
try {
return add(element);
} catch (Exception ex) {
//failed to add, return false
return false;
}
}
/**
* Retrieves and removes the head of this queue.
* This method differs from poll only in that it throws an exception if this queue is empty.
*
* @return the head of this queue.
* @throws NoSuchElementException if this queue is empty
*/
public T remove() throws NoSuchElementException {
if (total == 0) {
throw new NoSuchElementException();
}
T element = first.data;
first = first.next;
total--;
if (total == 0) {
last = null;
}
return element;
}
/**
* Retrieves and removes the head of this queue, or returns null if this queue is empty.
* This method differs from peek only in that it throws an exception if this queue is empty.
*
* @return the head of this queue, or returns null if this queue is empty.
*/
public T poll() {
try {
return remove();
} catch (Exception ex) {
//failed to remove, return null
return null;
}
}
/**
* Retrieves, but does not remove, the head of this queue.
*
* @return the head of this queue
* @throws NoSuchElementException if this queue is empty
*/
public T element() throws NoSuchElementException {
if (total == 0) {
throw new NoSuchElementException();
}
return first.data;
}
/**
* Retrieves, but does not remove, the head of this queue, or returns null if this queue is empty.
*
* @return the head of this queue, or returns null if this queue is empty.
*/
public T peek() {
return total == 0 ? null : first.data;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
Node tmp = first;
while (tmp != null) {
sb.append(tmp.data).append(", ");
tmp = tmp.next;
}
return sb.toString();
}
public static void main(String[] args) {
Queue<Integer> queue = new Queue<>();
for (int i = 1; i <= 10; i++) { // Creates a dummy queue which contains integers from 1-10
queue.add(i);
}
System.out.println("Queue :");
while (queue.peek() != null) {
System.out.println(queue.poll());
}
}
}