-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathQueueImplementationLinkedList.java
76 lines (66 loc) · 1.24 KB
/
QueueImplementationLinkedList.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
package queue;
class Node{
int data;
Node next;
Node(int data){
this.data = data;
next = null;
}
}
class QueueLL{
Node front,rear;
int size = 0;
void enqueue(int x) {
Node temp = new Node(x);
size++;
if(front == null) {
front = rear = temp;
return;
}
rear.next = temp;
rear = temp;
}
int dequeue() {
if(front == null) return -1;
front = front.next;
size--;
if(front == null) rear = null;
Node temp = front;
return temp.data;
}
int getFront() {
if(front == null) {
System.out.println("Queue is empty");
return -1;
}
return front.data;
}
int getRear() {
if(rear == null) {
System.out.println("Queue is empty");
return -1;
}
return rear.data;
}
int size() {
return size;
}
}
public class QueueImplementationLinkedList {
public static void main(String[] args) {
QueueLL q = new QueueLL();
q.enqueue(1);
q.enqueue(2);
q.enqueue(3);
q.enqueue(4);
q.enqueue(5);
q.enqueue(6);
q.enqueue(7);
// System.out.println(q.dequeue());
//
// System.out.println(q.dequeue());
System.out.println(q.getFront());
System.out.println(q.getRear());
System.out.println(q.size());
}
}