Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // A circular queue.
- class CircularQueue 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 CircularQueue(int size) {
- q = new char[size+1]; // allocate memory for queue
- putloc = getloc = 0;
- }
- // Put a characer into the queue.
- public void put(char ch) {
- /* Queue is full if either putloc is one less than
- getloc, or if putloc is at the end of the array
- and getloc is at the beginning. */
- if(putloc+1==getloc |
- ((putloc==q.length-1) & (getloc==0))) {
- System.out.println(" -- Queue is full.");
- return;
- }
- q[putloc++] = ch;
- if(putloc==q.length) putloc = 0; // loop back
- }
- // Get a character from the queue.
- public char get() {
- if(getloc == putloc) {
- System.out.println(" -- Queue is empty.");
- return (char) 0;
- }
- char ch = q[getloc++];
- if(getloc==q.length) getloc = 0; // loop back
- return ch;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement