-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQueueArray.java
75 lines (52 loc) · 1.12 KB
/
QueueArray.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
package com.topcoder.easy;
import java.util.Arrays;
public class QueueArray {
int front = 0;
int rear = 0;
int maxsize = 5;
int arr[] = new int[maxsize];
public void Enqueue(int data) {
if ((rear + 1) % maxsize == front) {
throw new IllegalStateException("Queue is full");
}
else{
arr[rear] = data;
rear = (rear+1)% maxsize;
}
}
public void Dequeue() {
if (front == rear) {
System.out.println("Queue is empty");
return;
} else {
System.out.println("Dequeued " + arr[front]);
front = (front+1)%maxsize;
}
}
public void display() {
for(int i =0; i<rear;i++ )
{
System.out.println(arr[i]);
}
}
public String toString() {
return Arrays.toString(arr) ;
}
public static void main(String[] args) {
QueueArray qa = new QueueArray();
qa.Enqueue(1);
qa.Enqueue(2);
qa.Enqueue(3);
qa.Enqueue(4);
qa.display();
qa.Dequeue();
qa.Dequeue();
System.out.println("After inserting again");
qa.Enqueue(3);
qa.Enqueue(4);
System.out.println(qa.toString());
qa.Dequeue();
qa.Dequeue();
qa.Dequeue();
}
}