-
Notifications
You must be signed in to change notification settings - Fork 0
/
ArrayQueue.java
64 lines (54 loc) · 1.08 KB
/
ArrayQueue.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
package assignment3;
import java.util.NoSuchElementException;
public class ArrayQueue {
int Queue[];
int front, rear, size, len;
public ArrayQueue(int n)
{
size = n;
len = 0;
Queue = new int[size];
front = -1;
rear = -1;
}
public boolean isEmpty() {
return front == -1;
}
public int getsize() {
return len;
}
public void enqueue(int i) {
if (rear == -1) {
front = 0;
rear = 0;
Queue[rear] = i;
} else if (rear + 1 >= size)
throw new IndexOutOfBoundsException("Overflow Exception");
else if (rear + 1 < size)
Queue[++rear] = i;
len++;
}
public int dequeue() {
if (isEmpty())
throw new NoSuchElementException("Underflow Exception");
else {
len--;
int ele = Queue[front];
if (front == rear) {
front = -1;
rear = -1;
} else
front++;
return ele;
}
}
public void display() {
System.out.print("\nQueue = ");
if (len == 0) {
System.out.print("Empty\n");
return;
}
for (int i = front; i <= rear; i++)
System.out.print(Queue[i] + " ");
}
}