-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinearQueueUsingLinkedList.java
137 lines (123 loc) · 3.76 KB
/
LinearQueueUsingLinkedList.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// Linked list node to maintain the queue
class Node{
int data;
Node next;
// Cretaion of a queue node
// Time Complexity - O(1) , Space Complexity - O(1)
Node(int x){
this.data = x;
this.next = null;
}
}
public class LinearQueueUsingLinkedList {
// head and tail of the linked list
Node head;
Node tail;
// function to enqueue an element 'x' to the end of queue
// Time Complexity - O(1) , Space Complexity - O(1)
public void enQueue(int x){
// create a new linked list node
Node newNode = new Node(x);
// if linked list / queue is empty
if(head==null){
head=newNode;
tail=newNode;
return;
}
// else if linked list / queue is not empty
tail.next = newNode;
tail = newNode;
}
// function to deQueue an element from the start of queue
// Time Complexity - O(1) , Space Complexity - O(1)
public void deQueue(){
// if queue is empty
if(head==null){
System.out.println("Queue is empty, can't dequeue");
return;
}
// else if queue is not empty, we just update the head node to its next
System.out.println("Dequeuing "+head.data+" ...");
head=head.next;
}
// function to get the element at the start of queue
// Time Complexity - O(1) , Space Complexity - O(1)
public void peek(){
// if queue is empty
if(head==null){
System.out.println("Queue is empty, can't peek");
return;
}
// else if queue is not empty
System.out.println("Element at Start of queue is: "+head.data);
}
// function to traverse the queue from start (as left) to end (as right)
// Time Complexity - O(n) , Space Complexity - O(1)
public void traverseQueue(){
// if linked list is empty
if(head==null){
System.out.println("Queue is empty, can't traverse");
return;
}
// else if linked list is not empty
System.out.println("Traversing Queue...");
Node temp = head;
while(temp!=null){
System.out.print(temp.data+" ");
temp = temp.next;
}
System.out.println();
}
// function to delete the entire queue
// Time Complexity - O(1) , Space Complexity - O(1)
public void deleteQueue(){
// if queue is already empty
if(head==null){
System.out.println("Queue is empty, can't delete");
return;
}
// else if linked list is not empty;
head=null;
tail=null;
System.out.println("Queue successfully deleted");
}
public static void main(String[] args) throws Exception {
LinearQueueUsingLinkedList queue = new LinearQueueUsingLinkedList();
queue.enQueue(1);
queue.enQueue(2);
queue.enQueue(3);
queue.enQueue(4);
queue.enQueue(5);
queue.traverseQueue();
queue.deQueue();
queue.deQueue();
queue.peek();
queue.deQueue();
queue.deQueue();
queue.deQueue();
queue.deQueue();
queue.traverseQueue();
queue.deleteQueue();
queue.enQueue(1);
queue.traverseQueue();
queue.deleteQueue();
queue.traverseQueue();
}
}
/* ======================= OUTPUT =========================
Traversing Queue...
1 2 3 4 5
Dequeuing 1 ...
Dequeuing 2 ...
Element at Start of queue is: 3
Dequeuing 3 ...
Dequeuing 4 ...
Dequeuing 5 ...
Queue is empty, can't dequeue
Queue is empty, can't traverse
Queue is empty, can't delete
Traversing Queue...
1
Queue successfully deleted
Queue is empty, can't traverse
======================================================*/