-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathFixedQueue.java
33 lines (27 loc) · 844 Bytes
/
FixedQueue.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
package com.guide.c8;
// A fixed-size queue class for characters.
public class FixedQueue 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 FixedQueue(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) {
System.out.println(" - Queue is full.");
return;
}
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++];
}
}