forked from LogicFirstTamil/DSA-in-JAVA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQueueUsingLL.java
51 lines (40 loc) · 880 Bytes
/
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
public class QueueUsingLL {
class Node{
int data;
Node next;
Node(int val){
data = val;
next = null;
}
}
Node front,rear;
QueueUsingLL(){
front = null;
rear = null;
}
public void enqueue(int val) { //O(1)
Node newNode = new Node(val);
if(front==null)
front = newNode;
else
rear.next = newNode ;
rear = newNode;
}
public int dequeue() {
if(front==null) //no node in Q
throw new IndexOutOfBoundsException("Queue is Empty");
int temp = front.data;
front = front.next;
if(front==null) //deleted last node and now Q is empty
rear=null;
return temp;
}
public boolean isEmpty() {
return front==null;
}
public int elementAtFront() {
if(front==null) //no node in Q
throw new IndexOutOfBoundsException("Queue is Empty");
return front.data;
}
}