-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueue using linkedlist.java
More file actions
72 lines (71 loc) · 1.23 KB
/
Queue using linkedlist.java
File metadata and controls
72 lines (71 loc) · 1.23 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
public class Main {
public static void main(String[] args) {
Queue q = new Queue();
q.enque("Faster saas");
q.enque("Faster digital services");
q.enque("Faster AI&MI");
q.enque("Faster finacial services");
q.enque("Faster Anlytics");
q.display();
q.deque();
q.deque();
System.out.println();
q.display();
System.out.println();
System.out.println(q.peekFirst());
System.out.println();
System.out.println(q.peekLast());
}
}
class Node {
String data;
Node next;
public Node(String data) {
this.data = data;
this.next = null;
}
}
class Queue {
Node head;
Node tail;
public Queue() {
this.head = null;
this.tail = null;
}
public void enque(String data) {
Node newNode = new Node(data);
if (head == null) {
head = newNode;
tail = head;
return;
} else {
tail.next = newNode;
tail = tail.next;
}
}
public void deque() {
if (head == null) {
return;
}
head = head.next;
}
public String peekFirst() {
if (head == null) {
return "";
}
return head.data;
}
public String peekLast() {
if (tail == null) {
return "";
}
return tail.data;
}
public void display() {
Node temp = head;
while (temp != null) {
System.out.println(temp.data);
temp = temp.next;
}
}
}