Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 21.3k
Expand file tree
/
Copy pathLinkedQueue.java
More file actions
Latest commit
201 lines (175 loc) · 4.71 KB
/
Copy pathLinkedQueue.java
File metadata and controls
201 lines (175 loc) · 4.71 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
packagecom.thealgorithms.datastructures.queues;
importjava.util.Iterator;
importjava.util.NoSuchElementException;
publicclassLinkedQueue<T> implementsIterable<T> {
/**
* Node class representing each element in the queue.
*/
privatestaticclassNode<T> {
Tdata;
Node<T> next;
Node(Tdata) {
this.data = data;
this.next = null;
}
}
privateNode<T> front; // Front of the queue
privateNode<T> rear; // Rear of the queue
privateintsize; // Size of the queue
/**
* Initializes an empty LinkedQueue.
*/
publicLinkedQueue() {
front = null;
rear = null;
size = 0;
}
/**
* Checks if the queue is empty.
*
* @return true if the queue is empty, otherwise false.
*/
publicbooleanisEmpty() {
returnsize == 0;
}
/**
* Adds an element to the rear of the queue.
*
* @param data the element to insert.
* @throws IllegalArgumentException if data is null.
*/
publicvoidenqueue(Tdata) {
if (data == null) {
thrownewIllegalArgumentException("Cannot enqueue null data");
}
Node<T> newNode = newNode<>(data);
if (isEmpty()) {
front = newNode;
} else {
rear.next = newNode;
}
rear = newNode;
size++;
}
/**
* Removes and returns the element at the front of the queue.
*
* @return the element at the front of the queue.
* @throws NoSuchElementException if the queue is empty.
*/
publicTdequeue() {
if (isEmpty()) {
thrownewNoSuchElementException("Queue is empty");
}
TretValue = front.data;
front = front.next;
size--;
if (isEmpty()) {
rear = null;
}
returnretValue;
}
/**
* Returns the element at the front of the queue without removing it.
*
* @return the element at the front of the queue.
* @throws NoSuchElementException if the queue is empty.
*/
publicTpeekFront() {
if (isEmpty()) {
thrownewNoSuchElementException("Queue is empty");
}
returnfront.data;
}
/**
* Returns the element at the rear of the queue without removing it.
*
* @return the element at the rear of the queue.
* @throws NoSuchElementException if the queue is empty.
*/
publicTpeekRear() {
if (isEmpty()) {
thrownewNoSuchElementException("Queue is empty");
}
returnrear.data;
}
/**
* Returns the element at the specified position (1-based index).
*
* @param pos the position to peek at (1-based index).
* @return the element at the specified position.
* @throws IndexOutOfBoundsException if the position is out of range.
*/
publicTpeek(intpos) {
if (pos < 1 || pos > size) {
thrownewIndexOutOfBoundsException("Position " + pos + " out of range!");
}
Node<T> node = front;
for (inti = 1; i < pos; i++) {
node = node.next;
}
returnnode.data;
}
/**
* Returns an iterator over the elements in the queue.
*
* @return an iterator over the elements in the queue.
*/
@Override
publicIterator<T> iterator() {
returnnewIterator<>() {
privateNode<T> current = front;
@Override
publicbooleanhasNext() {
returncurrent != null;
}
@Override
publicTnext() {
if (!hasNext()) {
thrownewNoSuchElementException();
}
Tdata = current.data;
current = current.next;
returndata;
}
};
}
/**
* Returns the size of the queue.
*
* @return the size of the queue.
*/
publicintsize() {
returnsize;
}
/**
* Clears all elements from the queue.
*/
publicvoidclear() {
front = null;
rear = null;
size = 0;
}
/**
* Returns a string representation of the queue.
*
* @return a string representation of the queue.
*/
@Override
publicStringtoString() {
if (isEmpty()) {
return"[]";
}
StringBuildersb = newStringBuilder("[");
Node<T> current = front;
while (current != null) {
sb.append(current.data);
if (current.next != null) {
sb.append(", ");
}
current = current.next;
}
sb.append(']');
returnsb.toString();
}
}