-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSimpleQueue.java
70 lines (61 loc) · 1.59 KB
/
SimpleQueue.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
class Queue {
int front, rear, size, capacity;
int array[];
public Queue(int capacity) {
this.capacity = capacity;
front = this.size = 0;
rear = capacity - 1;
array = new int[this.capacity];
}
boolean isFull(Queue queue) {
return (queue.size == queue.capacity);
}
boolean isEmpty(Queue queue) {
return (queue.size == 0);
}
void enqueue(int item) {
if (isFull(this))
return;
this.rear = (this.rear + 1) % this.capacity;
this.array[this.rear] = item;
this.size = this.size + 1;
}
int dequeue() {
if (isEmpty(this))
return Integer.MIN_VALUE;
int item = this.array[this.front];
this.front = (this.front + 1) % this.capacity;
this.size = this.size - 1;
return item;
}
int front() {
if (isEmpty(this))
return Integer.MIN_VALUE;
return this.array[this.front];
}
int rear() {
if (isEmpty(this))
return Integer.MIN_VALUE;
return this.array[this.rear];
}
void printQ(Queue queue) {
for (int i = this.front; i <= this.rear; i++) {
System.out.print(this.array[i] + " ");
}
// System.out.println("Sahil");
}
}
public class SimpleQueue {
public static void main(String[] args) {
Queue q = new Queue(100);
q.enqueue(1);
q.enqueue(2);
q.enqueue(3);
q.enqueue(4);
q.enqueue(5);
q.printQ(q);
System.out.println();
q.dequeue();
q.printQ(q);
}
}