-
Notifications
You must be signed in to change notification settings - Fork 19.9k
/
Copy pathLinkedQueue.java
201 lines (175 loc) · 4.71 KB
/
LinkedQueue.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
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
package com.thealgorithms.datastructures.queues;
import java.util.Iterator;
import java.util.NoSuchElementException;
public class LinkedQueue<T> implements Iterable<T> {
/**
* Node class representing each element in the queue.
*/
private static class Node<T> {
T data;
Node<T> next;
Node(T data) {
this.data = data;
this.next = null;
}
}
private Node<T> front; // Front of the queue
private Node<T> rear; // Rear of the queue
private int size; // Size of the queue
/**
* Initializes an empty LinkedQueue.
*/
public LinkedQueue() {
front = null;
rear = null;
size = 0;
}
/**
* Checks if the queue is empty.
*
* @return true if the queue is empty, otherwise false.
*/
public boolean isEmpty() {
return size == 0;
}
/**
* Adds an element to the rear of the queue.
*
* @param data the element to insert.
* @throws IllegalArgumentException if data is null.
*/
public void enqueue(T data) {
if (data == null) {
throw new IllegalArgumentException("Cannot enqueue null data");
}
Node<T> newNode = new Node<>(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.
*/
public T dequeue() {
if (isEmpty()) {
throw new NoSuchElementException("Queue is empty");
}
T retValue = front.data;
front = front.next;
size--;
if (isEmpty()) {
rear = null;
}
return retValue;
}
/**
* 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.
*/
public T peekFront() {
if (isEmpty()) {
throw new NoSuchElementException("Queue is empty");
}
return front.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.
*/
public T peekRear() {
if (isEmpty()) {
throw new NoSuchElementException("Queue is empty");
}
return rear.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.
*/
public T peek(int pos) {
if (pos < 1 || pos > size) {
throw new IndexOutOfBoundsException("Position " + pos + " out of range!");
}
Node<T> node = front;
for (int i = 1; i < pos; i++) {
node = node.next;
}
return node.data;
}
/**
* Returns an iterator over the elements in the queue.
*
* @return an iterator over the elements in the queue.
*/
@Override
public Iterator<T> iterator() {
return new Iterator<>() {
private Node<T> current = front;
@Override
public boolean hasNext() {
return current != null;
}
@Override
public T next() {
if (!hasNext()) {
throw new NoSuchElementException();
}
T data = current.data;
current = current.next;
return data;
}
};
}
/**
* Returns the size of the queue.
*
* @return the size of the queue.
*/
public int size() {
return size;
}
/**
* Clears all elements from the queue.
*/
public void clear() {
front = null;
rear = null;
size = 0;
}
/**
* Returns a string representation of the queue.
*
* @return a string representation of the queue.
*/
@Override
public String toString() {
if (isEmpty()) {
return "[]";
}
StringBuilder sb = new StringBuilder("[");
Node<T> current = front;
while (current != null) {
sb.append(current.data);
if (current.next != null) {
sb.append(", ");
}
current = current.next;
}
sb.append(']');
return sb.toString();
}
}