File tree Expand file tree Collapse file tree 1 file changed +41
-0
lines changed Expand file tree Collapse file tree 1 file changed +41
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments