-
Notifications
You must be signed in to change notification settings - Fork 1
/
QueueUsingLL.java
114 lines (80 loc) · 2.02 KB
/
QueueUsingLL.java
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
public class QueueUsingLL {
static class Node{
int data;
Node next;
Node(int d) {
data = d;
next = null;
}
}
static class Queue {
Node head = null;
Node tail = null;
public boolean isEmpty(){
if(head == null && tail == null){
return true;
}
return false;
}
// Enqueue
public void enqueue(int data){
Node newNode = new Node(data);
if(tail == null){
tail = head = newNode;
return;
}
tail.next = newNode;
tail = newNode;
}
// Dequeue
public void dequeue(){
if(isEmpty()) {
System.out.println("Queue is Empty");
return;
}
if(head == tail){
tail = null;
}
// int front = head.data;
head = head.next;
// return front;
}
public void peek(){
if(isEmpty()) {
System.out.println("Queue is Empty");
return;
}
System.out.println(head.data);
}
public void print(){
if(head == null){
System.out.println("Queue is Empty");
return;
}
Node currNode = head;
while(currNode != null){
System.out.print(currNode.data + " ");
currNode = currNode.next;
}
System.out.println();
}
}
public static void main(String[] args){
Queue q = new Queue();
q.peek();
q.print();
q.dequeue();
q.enqueue(1);
q.enqueue(2);
q.enqueue(3);
q.enqueue(4);
q.enqueue(5);
q.print();
q.dequeue();
q.peek();
q.print();
q.enqueue(55);
q.print();
q.enqueue(35);
}
}