forked from examplehub/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueue.java
More file actions
142 lines (123 loc) · 2.94 KB
/
Queue.java
File metadata and controls
142 lines (123 loc) · 2.94 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
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
132
133
134
135
136
137
138
139
140
141
142
package com.examplehub.datastructures.queue;
import java.util.StringJoiner;
public class Queue<E> {
/** Queue which store elements. */
private final E[] queue;
/** The front index of this queue. */
private int front;
/** The rear index of this queue. */
private int rear;
/** Default initial capacity. */
private static final int DEFAULT_CAPACITY = 10;
/** Capacity of queue. */
private final int capacity;
/** The number of elements in this queue. */
private int size;
public Queue() {
this(DEFAULT_CAPACITY);
}
public Queue(int capacity) {
@SuppressWarnings("unchecked")
E[] tempQueue = (E[]) new Object[capacity];
this.queue = tempQueue;
this.capacity = capacity;
this.front = 0;
this.rear = -1;
this.size = 0;
}
/**
* Added an item onto this queue.
*
* @param item the item to be added onto this queue.
* @return the {@code item} argument.
*/
public E enqueue(E item) {
if (full()) {
throw new StackOverflowError("Queue is full");
}
rear = (rear + 1) % capacity;
queue[rear] = item;
size++;
return item;
}
/**
* Remove an item from this queue.
*
* @return an item at the front of queue.
*/
public E dequeue() {
if (empty()) {
throw new IllegalStateException("Queue is empty");
}
E item = queue[front];
front = (front + 1) % capacity;
size--;
return item;
}
/**
* Get element at rear of this queue.
*
* @return element at rear of this queue.
*/
public E rear() {
if (empty()) {
throw new IllegalStateException("Queue is empty");
}
return queue[rear];
}
/**
* Get element at front of this queue.
*
* @return element at front of this queue.
*/
public E front() {
if (empty()) {
throw new IllegalStateException("Queue is empty");
}
return queue[front];
}
/** Clear all elements in the stack. */
public void clear() {
for (int i = 0; i < capacity; ++i) {
queue[i] = null; /* let GC do its work */
}
front = 0;
rear = -1;
size = 0;
}
/**
* Returns {@code true} if this queue contains no elements.
*
* @return {@code true} if this queue contains no elements, otherwise {@code false}.
*/
public boolean empty() {
return size == 0;
}
/**
* Returns {@code true} if this queue is full.
*
* @return {@code true} if this queue is full, otherwise {@code false}.
*/
public boolean full() {
return size == capacity;
}
/**
* Returns the number of elements at this queue.
*
* @return the number of elements in this stack.
*/
public int size() {
return size;
}
@Override
public String toString() {
StringJoiner joiner = new StringJoiner(", ", "[", "]");
if (!empty()) {
for (int i = front; i != rear; i = (i + 1) % capacity) {
joiner.add(queue[i].toString());
}
joiner.add(queue[rear].toString());
}
return joiner.toString();
}
}