-
Notifications
You must be signed in to change notification settings - Fork 0
/
LinearQueueUsingArray.java
175 lines (160 loc) · 5.42 KB
/
LinearQueueUsingArray.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
public class LinearQueueUsingArray {
// Array to implement Linear queue
int[] arr;
// variables to maintain indexes of start and end of queue
int startOFQueue;
int endOfQueue;
// constructor
// Creating a queue - Time Complexity - O(1), Space Complexity - O(n) , where n = size
LinearQueueUsingArray(int size){
this.arr=new int[size];
this.startOFQueue=0;
this.endOfQueue=-1;
}
// function to initialize the queue array
//Time Complexity - O(n), Space Complexity - O(1)
public void initializeArray(){
if(this.startOFQueue==0 && this.endOfQueue==-1){
for(int i=0; i<arr.length; i++){
this.arr[i]=Integer.MIN_VALUE;
}
}
}
// function to enqueue an element 'x' at the end of queue
//Time Complexity - O(1), Space Complexity - O(1)
public void enQueue(int x){
// if there is no queue present in memory
if(this.arr==null){
System.out.println("No Queue found, Please create one first");
return;
}
// if queue is full
if(this.endOfQueue==(this.arr.length-1)){
System.out.println("Linear queue is full, can't enqueue more");
return;
}
// if queue is present and not full
System.out.println("Enqueuing "+x+" ...");
this.endOfQueue++;
this.arr[this.endOfQueue]=x;
}
// function to dequeue an element from the start of queue
//Time Complexity - O(1), Space Complexity - O(1)
public void deQueue(){
// if there is no queue present in memory
if(this.arr==null){
System.out.println("No Queue found, Please create one first");
return;
}
// if queue is empty
if(this.endOfQueue==-1 || this.startOFQueue>this.endOfQueue){
System.out.println("Linear queue is empty, can't dequeue");
return;
}
// if queue is present and not empty
System.out.println("Dequeing "+this.arr[this.startOFQueue]+" ...");
this.arr[this.startOFQueue]=Integer.MIN_VALUE;
this.startOFQueue++;
}
// function to traverse the queue
//Time Complexity - O(n), Space Complexity - O(1)
public void traverseQueue(){
// if there is no queue present in memory
if(this.arr==null){
System.out.println("No Queue found, Please create one first");
return;
}
// If queue is empty
// System.out.println("Start: "+this.startOFQueue+" End: "+this.endOfQueue);
if(this.endOfQueue==-1 || this.startOFQueue>this.endOfQueue){
System.out.println("Linear queue is empty, can't traverse");
return;
}
// else if queue is present and not empty
System.out.println("Printing queue ...");
for(int i=this.startOFQueue;i<=this.endOfQueue;i++){
System.out.print(this.arr[i]+" ");
}
System.out.println();
}
// function to delete a queue
//Time Complexity - O(1), Space Complexity - O(1)
public void deleteQueue(){
// if there is no queue present in memory
if(this.arr==null){
System.out.println("No Queue Found, Please create one first");
return;
}
// else if queue is present in memory
this.arr=null;
this.startOFQueue=0;
this.endOfQueue=-1;
System.out.println("Queue Deleted Successfully");
}
// function to peek the start OF Queue
//Time Complexity - O(1), Space Complexity - O(1)
public void peek(){
// if there is no queue present in memory
if(this.arr==null){
System.out.println("No Queue found, Please create one first");
return;
}
// If queue is empty
// System.out.println("Start: "+this.startOFQueue+" End: "+this.endOfQueue);
if(this.endOfQueue==-1 || this.startOFQueue>this.endOfQueue){
System.out.println("Linear queue is empty, can't peek");
return;
}
// else if queue is present in memory and is not empty
System.out.println("Top of the queue is: "+this.arr[this.startOFQueue]);
}
public static void main(String[] args) throws Exception {
LinearQueueUsingArray queue = new LinearQueueUsingArray(10);
queue.initializeArray();
queue.peek();
queue.enQueue(1);
queue.enQueue(2);
queue.enQueue(3);
queue.traverseQueue();
queue.deQueue();
queue.traverseQueue();
queue.deQueue();
queue.traverseQueue();
queue.enQueue(100);
queue.traverseQueue();
queue.deQueue();
queue.traverseQueue();
queue.peek();
queue.deQueue();
queue.traverseQueue();
queue.deQueue();
queue.deleteQueue();
queue.traverseQueue();
}
}
/* ========================= OUTPUT ============================
Linear queue is empty, can't peek
Enqueuing 1 ...
Enqueuing 2 ...
Enqueuing 3 ...
Printing queue ...
1 2 3
Dequeing 1 ...
Printing queue ...
2 3
Dequeing 2 ...
Printing queue ...
3
Enqueuing 100 ...
Printing queue ...
3 100
Dequeing 3 ...
Printing queue ...
100
Top of the queue is: 100
Dequeing 100 ...
Linear queue is empty, can't traverse
Linear queue is empty, can't dequeue
Queue Deleted Successfully
No Queue found, Please create one first
====================================================================*/