import java.util.*;
public class Test {
public static void main(String[] args) { ArrayQueue<String> q = new ArrayQueue<String>(6);
System.out.println("A new queue is an empty queue ... " + q.isEmpty()); System.out.println("Adding six strings to the queue ..."); System.out.println("A: " + q.join(new String("A"))); System.out.println("B: " + q.join(new String("B"))); System.out.println("C: " + q.join(new String("C"))); System.out.println("D: " + q.join(new String("D"))); System.out.println("A: " + q.leave(new String("A"))); System.out.println("B: " + q.leave(new String("B"))); System.out.println("C: " + q.leave(new String("C"))); System.out.println("E: " + q.join(new String("E"))); System.out.println("F: " + q.join(new String("F"))); System.out.println("G: " + q.join(new String("G"))); System.out.println("H: " + q.join(new String("H"))); System.out.println("I: " + q.join(new String("I"))); System.out.println("G: " + q.leave(new String("G"))); System.out.println("H: " + q.leave(new String("H"))); System.out.println("I: " + q.leave(new String("I"))); System.out.println("The queue is now full ..." + q.isFull()); System.out.println("Adding a string to a full queue ..."); System.out.println("tagore: " + q.join(new String("tagore"))); System.out.println("Emptying the queue item by item ..."); while (!q.isEmpty()) { System.out.println(q.retrieve()); q.leave();
} } }
The package java.util.* does not contain the class ArrayQueue. It is found in another package. Either you import that package through the jar file or implement the class ArrayQueue.
ArrayQueue.java:
class ArrayQueue implements Queue { static final int defaultsize = 100; Object data[]; int head; int tail; int size; ArrayQueue(int maxsize) { data = new Object[maxsize]; head = 0; tail = 0; size = maxsize; } ArrayQueue() { data = new Object[defaultsize]; head = 0; tail = 0; size = defaultsize; } public void enqueue(Object elem) { Assert.notFalse((tail+1)%size != head, "Queue Full"); data[tail] = elem; tail = (tail + 1) % size; } public Object dequeue() { Object retval; if (head == tail) return null; retval = data[head]; head = (head + 1) % size; return retval; } public boolean empty() { return head == tail; } public String toString() { String result = "["; int tmpHead = head; if (tmpHead != tail) { result = result + data[tmpHead]; tmpHead = (tmpHead + 1) % size; while (tmpHead != tail) { result = result + "," + data[tmpHead]; tmpHead = (tmpHead + 1) % size; } } result = result + "]"; return result; } }
Ads