-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQueue.java
52 lines (44 loc) · 1 KB
/
Queue.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
public class Queue {
private int maxSize; // Maximum Capacity for the queue
private String[] queArray; // The array that holds the items
private int front;
private int rear;
private int nItems;
// constructor
public Queue(int s) {
maxSize = s; // set array size
queArray = new String[maxSize]; // create array
front = 0;
rear = -1;
nItems = 0; // no items yet
} // end of constructor
public void enqueue(String i) {
if (rear == maxSize - 1)
rear = -1;
queArray[++rear] = i; // increment rear, insert item
nItems++;
}
public String dequeue() {
String result = queArray[front]; // get value
front++; // increment front
if (front == maxSize) // deal with wraparound
front = 0;
nItems--;
return result;
}
public String peek() {
return queArray[front];
}
public boolean isEmpty() {
return (nItems == 0);
}
public boolean isFull() {
return (nItems == maxSize);
}
public int size() {
return nItems;
}
public String get(int index) {
return queArray[index];
}
}