forked from reeucq/advanced-data-structures
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArrayQueue.java
131 lines (109 loc) · 3.24 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/**
* Queue is a data structure that that follows the First In First Out (FIFO) principle.
* It has restricted insertion and deletion operations, they are allowed at two different ends of the structure, which are called the front and rear.
* The insertion is performed at the rear end and the deletion is performed at the front end.
* This class implements a queue using an array.
*/
public class ArrayQueue<T> {
/**
* Data Members
*/
T[] queue;
int front;
int rear;
int length;
/** Constructor */
@SuppressWarnings("unchecked")
public ArrayQueue(int length) {
if(length < 1)
throw new IllegalArgumentException("Length of the queue should be greater than 0");
this.length = length;
queue = (T[]) new Object[length];
front = -1;
rear = -1;
}
/** @return true iff queue is empty */
public boolean isEmpty() { return rear == -1; }
/** @return the number of elements in the queue */
public int size() {
if(rear == -1)
return 0;
return (rear - front + 1);
}
/**
* inserts an element at the rear end of the queue
* @param element the element to be inserted into the queue
* @throws IllegalStateException if the queue is full
*/
public void insert(T element) {
if(rear == length - 1)
throw new IllegalStateException("Queue is Full");
if(front == -1)
front = 0;
queue[++rear] = element;
}
/**
* removes the front element from the queue and returns it
* @return the front element of the queue
* @throws IllegalStateException if the queue is empty
*/
public T remove() {
if(isEmpty())
throw new IllegalStateException("Queue is Empty");
T element = queue[front];
if(front == rear) {
front = -1;
rear = -1;
} else {
front++;
}
return element;
}
/**
* returns the front element of the queue without removing it
*/
public T getFront() {
if(isEmpty())
throw new IllegalStateException("Queue is Empty");
return queue[front];
}
/**
* returns the front element of the queue without removing it
*/
public T getRear() {
if(isEmpty())
throw new IllegalStateException("Queue is Empty");
return queue[rear];
}
/**
* Displays the elements of the queue
*/
public void display() {
if(isEmpty()) {
System.out.println("Queue is Empty");
return;
}
System.out.print("Queue: ");
for(int i = front; i <= rear; i++)
System.out.print(queue[i] + " ");
System.out.println();
}
// Main method for testing
public static void main(String[] args) {
ArrayQueue<Integer> queue = new ArrayQueue<>(5);
queue.display();
queue.insert(1);
queue.insert(2);
queue.insert(3);
queue.insert(4);
queue.insert(5);
queue.display();
queue.remove();
queue.display();
queue.remove();
queue.remove();
queue.remove();
queue.remove();
queue.display();
}
}