Skip to content

Commit f786f3f

Browse files
authored
Merge pull request TheAlgorithms#1109 from shellhub/dev
LinkedQueue
2 parents 6e401a5 + fce3d83 commit f786f3f

File tree

1 file changed

+169
-0
lines changed

1 file changed

+169
-0
lines changed
+169
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
package DataStructures;
2+
3+
import java.util.NoSuchElementException;
4+
5+
public class LinkedQueue {
6+
class Node {
7+
int data;
8+
Node next;
9+
10+
public Node() {
11+
this(0);
12+
}
13+
14+
public Node(int data) {
15+
this(data, null);
16+
}
17+
18+
public Node(int data, Node next) {
19+
this.data = data;
20+
this.next = next;
21+
}
22+
}
23+
24+
/**
25+
* Front of Queue
26+
*/
27+
private Node front;
28+
29+
/**
30+
* Rear of Queue
31+
*/
32+
private Node rear;
33+
34+
/**
35+
* Size of Queue
36+
*/
37+
private int size;
38+
39+
/**
40+
* Init LinkedQueue
41+
*/
42+
public LinkedQueue() {
43+
front = rear = new Node();
44+
}
45+
46+
/**
47+
* Check if queue is empty
48+
*
49+
* @return <tt>true</tt> if queue is empty, otherwise <tt>false</tt>
50+
*/
51+
public boolean isEmpty() {
52+
return size == 0;
53+
}
54+
55+
/**
56+
* Add element to rear of queue
57+
*
58+
* @param data insert value
59+
* @return <tt>true</tt> if add successfully
60+
*/
61+
public boolean enqueue(int data) {
62+
Node newNode = new Node(data);
63+
rear.next = newNode;
64+
rear = newNode; /* make rear point at last node */
65+
size++;
66+
return true;
67+
}
68+
69+
/**
70+
* Remove element at the front of queue
71+
*
72+
* @return element at the front of queue
73+
*/
74+
public int dequeue() {
75+
if (isEmpty()) {
76+
throw new NoSuchElementException("queue is empty");
77+
}
78+
Node destroy = front.next;
79+
int retValue = destroy.data;
80+
front.next = front.next.next;
81+
destroy = null; /* clear let GC do it's work */
82+
size--;
83+
84+
if (isEmpty()) {
85+
front = rear;
86+
}
87+
88+
return retValue;
89+
}
90+
91+
/**
92+
* Peek element at the front of queue without removing
93+
*
94+
* @return element at the front
95+
*/
96+
public int peekFront() {
97+
if (isEmpty()) {
98+
throw new NoSuchElementException("queue is empty");
99+
}
100+
return front.next.data;
101+
}
102+
103+
/**
104+
* Peek element at the rear of queue without removing
105+
*
106+
* @return element at the front
107+
*/
108+
public int peekRear() {
109+
if (isEmpty()) {
110+
throw new NoSuchElementException("queue is empty");
111+
}
112+
return rear.data;
113+
}
114+
115+
/**
116+
* Return size of queue
117+
*
118+
* @return size of queue
119+
*/
120+
public int size() {
121+
return size;
122+
}
123+
124+
/**
125+
* Clear all nodes in queue
126+
*/
127+
public void clear() {
128+
while (!isEmpty()) {
129+
dequeue();
130+
}
131+
}
132+
133+
@Override
134+
public String toString() {
135+
if (isEmpty()) {
136+
return "[]";
137+
}
138+
StringBuilder builder = new StringBuilder();
139+
Node cur = front.next;
140+
builder.append("[");
141+
while (cur != null) {
142+
builder.append(cur.data).append(", ");
143+
cur = cur.next;
144+
}
145+
builder.replace(builder.length() - 2, builder.length(), "]");
146+
return builder.toString();
147+
}
148+
149+
/* Driver Code */
150+
public static void main(String[] args) {
151+
LinkedQueue queue = new LinkedQueue();
152+
assert queue.isEmpty();
153+
154+
queue.enqueue(1); /* 1 */
155+
queue.enqueue(2); /* 1 2 */
156+
queue.enqueue(3); /* 1 2 3 */
157+
System.out.println(queue); /* [1, 2, 3] */
158+
159+
assert queue.size() == 3;
160+
assert queue.dequeue() == 1;
161+
assert queue.peekFront() == 2;
162+
assert queue.peekRear() == 3;
163+
164+
queue.clear();
165+
assert queue.isEmpty();
166+
167+
System.out.println(queue); /* [] */
168+
}
169+
}

0 commit comments

Comments
 (0)