-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueue.java
More file actions
78 lines (58 loc) · 1.46 KB
/
Copy pathQueue.java
File metadata and controls
78 lines (58 loc) · 1.46 KB
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
//Queue implementataion using Array
//Youtube : https://www.youtube.com/watch?v=M6GnoUDpqEE&list=PLgUwDviBIf0oSO572kQ7KCSvCUh1AdILj&index=2
import java.io.*;
import java.util.*;
public class Queue{
static int[] a ;
static int count;
static int n;
static int rear;
static int front;
public Queue(int n){
a = new int[n];
count = 0;
this.n = n;
rear = 0;
front = 0;
}
public static void push(int val){
if(count < n){
a[rear%n] = val;
rear++;
count++;
}else{
System.out.println("The queue is full");
}
}
public static int poll(){
int val = 0;
if(count == 0){
System.out.println("The queue is empty");
}else{
val = a[front % n];
a[front % n] = -1;
front++;
count--;
}
return val;
}
public static void printQueue(){
for(int i=front;i<rear;i++){
System.out.print(a[i%n]+" ");
}
}
public static void main(String[] args){
Queue q = new Queue(5);
q.push(3);
q.push(5);
q.push(6);
q.push(1);
q.push(5);
q.poll();
q.push(6);
q.poll();
q.push(7);
q.printQueue();
}
//o/p : 6 1 5 6 7
}