Click here to load reader
Upload
dinhlien
View
212
Download
0
Embed Size (px)
Citation preview
Listing 4.4 shows the queue.java program.
LISTING 4.4 The queue.java Program
// queue.java
// demonstrates queue
// to run this program: C>java QueueApp
////////////////////////////////////////////////////////////////
class Queue
{
private int maxSize;
private long[] queArray;
private int front;
private int rear;
private int nItems;
//--------------------------------------------------------------
public Queue(int s) // constructor
{
maxSize = s;
queArray = new long[maxSize];
front = 0;
rear = -1;
nItems = 0;
}
//--------------------------------------------------------------
public void insert(long j) // put item at rear of queue
{
if(rear == maxSize-1) // deal with wraparound
rear = -1;
queArray[++rear] = j; // increment rear and insert
nItems++; // one more item
}
//--------------------------------------------------------------
public long remove() // take item from front of queue
{
long temp = queArray[front++]; // get value and incr front
if(front == maxSize) // deal with wraparound
front = 0;
nItems--; // one less item
return temp;
}
//--------------------------------------------------------------
public long peekFront() // peek at front of queue
{
CHAPTER 4 Stacks and Queues138
return queArray[front];
}
//--------------------------------------------------------------
public boolean isEmpty() // true if queue is empty
{
return (nItems==0);
}
//--------------------------------------------------------------
public boolean isFull() // true if queue is full
{
return (nItems==maxSize);
}
//--------------------------------------------------------------
public int size() // number of items in queue
{
return nItems;
}
//--------------------------------------------------------------
} // end class Queue
////////////////////////////////////////////////////////////////
class QueueApp
{
public static void main(String[] args)
{
Queue theQueue = new Queue(5); // queue holds 5 items
theQueue.insert(10); // insert 4 items
theQueue.insert(20);
theQueue.insert(30);
theQueue.insert(40);
theQueue.remove(); // remove 3 items
theQueue.remove(); // (10, 20, 30)
theQueue.remove();
theQueue.insert(50); // insert 4 more items
theQueue.insert(60); // (wraps around)
theQueue.insert(70);
theQueue.insert(80);
while( !theQueue.isEmpty() ) // remove and display
{ // all items
Queues 139
LISTING 4.4 Continued
long n = theQueue.remove(); // (40, 50, 60, 70, 80)
System.out.print(n);
System.out.print(“ “);
}
System.out.println(“”);
} // end main()
} // end class QueueApp
We’ve chosen an approach in which Queue class fields include not only front andrear, but also the number of items currently in the queue: nItems. Some queue implementations don’t use this field; we’ll show this alternative later.
The insert() MethodThe insert() method assumes that the queue is not full. We don’t show it in main(),but normally you should call insert() only after calling isFull() and getting a returnvalue of false. (It’s usually preferable to place the check for fullness in the insert()routine and cause an exception to be thrown if an attempt was made to insert into afull queue.)
Normally, insertion involves incrementing rear and inserting at the cell rear nowpoints to. However, if rear is at the top of the array, at maxSize-1, then it must wraparound to the bottom of the array before the insertion takes place. This is done bysetting rear to –1, so when the increment occurs, rear will become 0, the bottom ofthe array. Finally, nItems is incremented.
The remove() MethodThe remove() method assumes that the queue is not empty. You should call isEmpty()to ensure this is true before calling remove(), or build this error-checking intoremove().
Removal always starts by obtaining the value at front and then incrementing front.However, if this puts front beyond the end of the array, it must then be wrappedaround to 0. The return value is stored temporarily while this possibility is checked.Finally, nItems is decremented.
The peek() MethodThe peek() method is straightforward: It returns the value at front. Some implemen-tations allow peeking at the rear of the array as well; such routines are called some-thing like peekFront() and peekRear() or just front() and rear().
The isEmpty(), isFull(), and size() MethodsThe isEmpty(), isFull(), and size() methods all rely on the nItems field, respectivelychecking if it’s 0, if it’s maxSize, or returning its value.
CHAPTER 4 Stacks and Queues140
LISTING 4.4 Continued