Skip to content

Commit c63dbad

Browse files
LinkedQueue
1 parent a60cb58 commit c63dbad

File tree

1 file changed

+154
-0
lines changed

1 file changed

+154
-0
lines changed
+154
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
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+
return retValue;
84+
}
85+
86+
/**
87+
* Peek element at the front of queue without removing
88+
*
89+
* @return element at the front
90+
*/
91+
public int peekFront() {
92+
if (isEmpty()) {
93+
throw new NoSuchElementException("queue is empty");
94+
}
95+
return front.next.data;
96+
}
97+
98+
/**
99+
* Peek element at the rear of queue without removing
100+
*
101+
* @return element at the front
102+
*/
103+
public int peekRear() {
104+
if (isEmpty()) {
105+
throw new NoSuchElementException("queue is empty");
106+
}
107+
return rear.data;
108+
}
109+
110+
/**
111+
* Return size of queue
112+
*
113+
* @return size of queue
114+
*/
115+
public int size() {
116+
return size;
117+
}
118+
119+
/**
120+
* Clear all nodes in queue
121+
*/
122+
public void clear() {
123+
//TODO
124+
}
125+
126+
@Override
127+
public String toString() {
128+
StringBuilder builder = new StringBuilder();
129+
Node cur = front.next;
130+
builder.append("[");
131+
while (cur != null) {
132+
builder.append(cur.data).append(", ");
133+
cur = cur.next;
134+
}
135+
builder.replace(builder.length() - 2, builder.length(), "]");
136+
return builder.toString();
137+
}
138+
139+
/* Driver Code */
140+
public static void main(String[] args) {
141+
LinkedQueue queue = new LinkedQueue();
142+
assert queue.isEmpty();
143+
144+
queue.enqueue(1); /* 1 */
145+
queue.enqueue(2); /* 1 2 */
146+
queue.enqueue(3); /* 1 2 3 */
147+
System.out.println(queue);
148+
149+
assert queue.size() == 3;
150+
assert queue.dequeue() == 1;
151+
assert queue.peekFront() == 2;
152+
assert queue.peekRear() == 3;
153+
}
154+
}

0 commit comments

Comments
 (0)