-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueue.java
More file actions
67 lines (53 loc) · 1.33 KB
/
Queue.java
File metadata and controls
67 lines (53 loc) · 1.33 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
public class Queue {
int Q[];
int front;
int rear;
int size;
public Queue(int q) {
Q = new int[q];
}
public boolean isEmpty() {
return (getSize() == 0);
}
public boolean isFull() {
return (getSize() == Q.length);
}
public void enQueue(int val) {
if(isFull()) {
System.out.println("Queue is full"); return;
}
Q[rear] = val;
rear = (rear+1) % Q.length;
size++;
}
public void deQueue() {
if(isEmpty()) {
System.out.println("Queue is Empty"); return;
}
front = (front+1) % Q.length;
size--;
}
public int getSize() {
return size;
}
public void printQ() {
for(int i=0; i<size; i++) {
System.out.print(Q[(front+i) % Q.length] + " ");
}
}
public static void main(String[] args) {
Queue q = new Queue(5);
q.enQueue(5);
q.enQueue(6);
q.enQueue(7);
q.enQueue(9);
q.deQueue();
q.deQueue();
q.enQueue(1);
q.enQueue(3);
q.printQ();
System.out.println("size is - " + q.getSize());
System.out.println(q.isEmpty());
System.out.println(q.isFull());
}
}