-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedQueueOfStrings.java
More file actions
57 lines (55 loc) · 1.16 KB
/
LinkedQueueOfStrings.java
File metadata and controls
57 lines (55 loc) · 1.16 KB
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
public class LinkedQueueOfStrings
{
private Node first, last;
private class Node
{
//access modifier doesn't matter
String item;//instance variables have a default value
Node next; //object , a reference to another node
public String toString(){
return item ;
}
}
public boolean isEmpty()
{ return first == null;
}
public void enqueue(String item)
{
Node oldlast = last;
last = new Node();
last.item = item;
last.next = null;
if (isEmpty()) first = last;
else
oldlast.next = last;
}
public String dequeue()
{
String item = first.item;
first
= first.next;
if (isEmpty()) last = null;
return item;
}
public static void main(String[] args ){
LinkedQueueOfStrings l=new LinkedQueueOfStrings();
// System.out.println(l.first);
// System.out.println("====");
String s="hello";
l.enqueue(s);
System.out.println("===");
System.out.println(l.first);
System.out.println("===");
String sa="hello2";
l.enqueue(sa);
System.out.println("===");
System.out.println(l.first);
System.out.println("===");
String sab="hello3";
l.enqueue(sab);
System.out.println("=====");
System.out.println(l.dequeue());
System.out.println(l.first);
System.out.println(l.first.next);
}
}