Skip to content

Commit 5c7f447

Browse files
committed
added queue in linkedlist implementation in Java
1 parent 0474200 commit 5c7f447

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
public class LinkedQueueOfStrings {
2+
private Node first, last;
3+
4+
private class Node {
5+
String item;
6+
Node next;
7+
}
8+
9+
public boolean isEmpty() {
10+
return first != null;
11+
}
12+
13+
public void enqueue(String item) {
14+
Node oldlast = last;
15+
last = new Node();
16+
last.item = item;
17+
last.next = null;
18+
if (isEmpty()) {
19+
first = last;
20+
} else {
21+
oldlast.next = last;
22+
}
23+
}
24+
25+
public String dequeue() {
26+
String item = first.item;
27+
first = first.next;
28+
if (isEmpty()) {
29+
last = null;
30+
}
31+
return item;
32+
}
33+
34+
public static void main(String[] args) {
35+
LinkedQueueOfStrings<String> queue = new LinkedQueueOfStrings<String>();
36+
queue.enqueue("a");
37+
queue.enqueue("b");
38+
queue.enqueue("c");
39+
40+
}
41+
}

0 commit comments

Comments
 (0)