-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSection_16_1_Dynamic_Array_Implementations_Queue.java
More file actions
163 lines (138 loc) · 4.51 KB
/
Section_16_1_Dynamic_Array_Implementations_Queue.java
File metadata and controls
163 lines (138 loc) · 4.51 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*
Array-based Queue...!
Note:
a = 5; b = ++a; // a = 6, b = 6
a = 5; b = a++; // a = 6, b = 5
*/
public class Section_16_1_Dynamic_Array_Implementations_Queue {
public static void main(String[] arguments){
ArrayQueue<String> papersQueue = new ArrayQueue();
papersQueue.enqueue("Washington Post");
papersQueue.enqueue("Wall Street Journal");
papersQueue.enqueue("The Guardian");
papersQueue.enqueue("The Citizen");
papersQueue.enqueue("New Yorck Times");
papersQueue.enqueue("The Economist");
papersQueue.enqueue("Nature");
papersQueue.enqueue("National Geographic");
papersQueue.enqueue("Rolling Stones");
papersQueue.enqueue("The New Herald");
papersQueue.enqueue("The Oxford Gazette");
papersQueue.enqueue("Times");
papersQueue.enqueue("Forbes");
System.out.println("ArrayQueue...!\n");
System.out.println(papersQueue.toString());
System.out.println("-------------------------------------------------");
System.out.println("'" + papersQueue.dequeue() + "' has been removed of the queue...!");
System.out.println("'" + papersQueue.dequeue() + "' has been removed of the queue...!");
System.out.println("'" + papersQueue.dequeue() + "' has been removed of the queue...!");
System.out.println("-------------------------------------------------");
System.out.println(papersQueue.toString());
}
}
class ArrayQueue<AnyType>{
private AnyType[] theArray;
private int currentSize;
private int front;
private int back;
private static final int DEFAULT_CAPACITY = 10;
/**
* Construct the queue.
*/
public ArrayQueue(){
theArray = (AnyType []) new Object[DEFAULT_CAPACITY];
makeEmpty();
}
/**
* Test if the queue is logically empty.
* @return true if empty, false otherwise.
*/
public boolean isEmpty(){
return(currentSize == 0);
}
/**
* Make the queue logically empty.
*/
public void makeEmpty(){
currentSize = 0;
front = 0;
back = -1;
}
/**
* Insert a new item into the queue.
* @param x the item to insert.
*/
public void enqueue(AnyType x){
if(currentSize == theArray.length){
doubleQueue();
}
back = increment(back);
theArray[back] = x;
currentSize++;
}
/**
* Return and remove the least recently inserted item from the queue.
* @return the least recently inserted item in the queue.
* @throws RuntimeException if the queue is empty.
*/
public AnyType dequeue(){
if(isEmpty()){
throw new RuntimeException("ArrayQueue dequeue");
}
currentSize--;
AnyType returnValue = theArray[front];
front = increment(front);
return returnValue;
}
/**
* Get the least recently inserted item in the queue, does not alter the queue.
* @return the least recently inserted item in the queue.
* @throws UnderflowException if the queue is empty.
*/
public AnyType getFront(){
if(isEmpty()){
throw new RuntimeException("ArrayQueue getFront");
}
return theArray[front];
}
/**
* @return a string whit all elements of the queue
*/
@Override
public String toString(){
StringBuilder sb = new StringBuilder();
sb.append("[\n");
for(int i = front; i < back; i++){
if(theArray[i] != null){
sb.append("\t").append(theArray[i]).append("\n");
}
}
sb.append("]\n");
return(new String(sb));
}
/**
* Internal method to expand theArray.
*/
private void doubleQueue(){
AnyType[] newArray;
newArray = (AnyType []) new Object[theArray.length * 2];
// Copy elements that are logically in the queue
for(int i = 0; i < currentSize; i++, front = increment(front)){
newArray[i] = theArray[front];
}
theArray = newArray;
front = 0;
back = currentSize - 1;
}
/**
* Internal method to increment with wraparound.
* @param x any index in theArray's range.
* @return x + 1, or 0 if x is at the end of theArray.
*/
private int increment( int x ){
if(++x == theArray.length){
x = 0;
}
return x;
}
}