-
Notifications
You must be signed in to change notification settings - Fork 13
/
DynQueue.java
39 lines (31 loc) · 966 Bytes
/
DynQueue.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
package com.guide.c8;
// A dynamic queue.
public class DynQueue implements ICharQ {
private char[] q; // this array holds the queue
private int putloc, getloc; // the put and get indices
// Construct an empty queue given its size.
public DynQueue(int size) {
q = new char[size]; // allocate memory for queue
putloc = getloc = 0;
}
// Put a character into the queue.
public void put(char ch) {
if (putloc == q.length) {
// increase queue size
char[] t = new char[q.length * 2];
// copy elements into new queue
for (int i = 0; i < q.length; i++)
t[i] = q[i];
q = t;
}
q[putloc++] = ch;
}
// Get a character from the queue.
public char get() {
if (getloc == putloc) {
System.out.println(" - Queue is empty.");
return (char) 0;
}
return q[getloc++];
}
}