Hi all..could anyone tell me how i should fix the problem that i am struck here? I try to implemented an queue on my own. but i change sth in that queue.. i try not to use isEmpty() to keep track of the element in my queue..(it work fine if i use isEmpty()....) instead of using size() to keep track if it is an empty stack...but i got a little bit problem here..it call the exception automatically..and i am not expecting this result
=========================
javac QueueTest.java
Exit code: 0
java QueueTest
The queue is empty
0 1 2 3 4 5 6 7 8
EmptyQueueException on peek:queue empty <<- error occur here.ithould not appear.
Exit code: 0
=====================
expected result
=========================
javac QueueTest.java
Exit code: 0
java QueueTest
The queue is empty
0 1 2 3 4 5 6 7 8
Exit code: 0
=====================
my code is as following
QueueTest
public class QueueTest
{
public static void main(String[] args)
{
QueueReferenceBased aQueue = new QueueReferenceBased();
if(aQueue.size() == 0)
//if(aQueue.isEmpty())
{
System.out.println("The queue is empty");
}
for(int i=0; i < 9; i++)
{
aQueue.enqueue(new Integer(i));
}
try
{
//while (!aQueue.isEmpty())
//while(aQueue.size() != 0)
while(aQueue.size() != 0)
{
System.out.print(aQueue.peek() + " ");
//System.out.print(aQueue.size() );
aQueue.dequeue();
}
// System.out.print(aQueue.size() );
//aQueue.peek();
}
catch( EmptyQueueException eqe)
{
System.out.println("\n" + eqe.getMessage());
}
}
}
===
QueueReferenceBased
// File: QueueReferenceBased.java
// Author: Chi Lun To (Ivan To)
// Created on: June 5, 2007
public class QueueReferenceBased implements QueueInterface
{
private Node lastNode;
private int numberOfItems = 0;
public QueueReferenceBased()
{
lastNode = null;
}
/*
// queue operations:
public boolean isEmpty()
{
return lastNode == null;
}
*/
public int size()
{
return numberOfItems;
}
public void enqueue(Object newItem)
{ // insert the new node
Node newNode = new Node(newItem);
// if (isEmpty())
if(lastNode == null)
// if(size()==0)
{
// insertion into empty queue
newNode.setNext(newNode);
}
else {
// insertion into nonempty queue
newNode.setNext(lastNode.getNext());
lastNode.setNext(newNode);
}
lastNode = newNode; // new node is at back
numberOfItems++;
}
public Object dequeue() throws EmptyQueueException
{
// if(!isEmpty())
//if (!(size() == 0))
if(lastNode != null)
{
// queue is not empty; remove front
Node firstNode = lastNode.getNext();
if (firstNode == lastNode) // special case?
{
lastNode = null; // yes, one node in queue
}
else
{
lastNode.setNext(firstNode.getNext());
numberOfItems--;
}
return firstNode.getItem();
}
else {
throw new EmptyQueueException("EmptyQueueException on dequeue:" + "queue empty");
}
} // end dequeue
public Object peek() throws EmptyQueueException
{
if(lastNode != null)
//if (size() != -1)
{
// queue is not empty; retrieve front
Node firstNode = lastNode.getNext();
return firstNode.getItem();
}
else {
throw new EmptyQueueException("EmptyQueueException on peek:"+ "queue empty");
}
}
} // end QueueReferenceBased
===
the code u could omit ..but just for better unstanding ..thx
node
public class Node {
private Object item;
private Node next;
public Node(Object newItem) {
item = newItem;
next = null;
} // end constructor
public Node(Object newItem, Node nextNode) {
item = newItem;
next = nextNode;
} // end constructor
public void setItem(Object newItem) {
item = newItem;
} // end setItem
public Object getItem() {
return item;
} // end getItem
public void setNext(Node nextNode) {
next = nextNode;
} // end setNext
public Node getNext() {
return next;
} // end getNext
} // end class Node
====
QueueInterface
public interface QueueInterface
{
//public boolean isEmpty() ;
public int size();
// Description: Determines the number of elements in a queue.
// In other words, the length of a queue.
// Precondition: None.
// Postcondition: Returns the number of elements that are
// currently in the queue.
// Throws: None.
public void enqueue(Object newElement);
// Description: Adds an element at the back of a queue.
// Precondition: None.
// Postcondition: If the operation was successful,
// newElement is at the back of the queue.
// Throws: None.
public Object dequeue() throws EmptyQueueException;
// Description: Retrieves and removes the front of a queue.
// Precondition: Queue is not empty.
// Postcondition: If the queue is not empty, the element
// that was added to the queue earliest
// is returned and the element is removed.
// If the queue is empty, the operation
// is impossible and QueueException is thrown.
// Throws: EmptyQueueException when the queue is empty.
public Object peek() throws EmptyQueueException;
// Description: Retrieves the element at the front of a queue.
// Precondition: Queue is not empty.
// Postcondition: If the queue is not empty, the element
// that was added to the queue earliest
// is returned. If the queue is empty, the
// operation is impossible and QueueException
// is thrown.
// Throws: EmptyQueueException when the queue is empty.
} // end QueueInterface
=====
public class EmptyQueueException extends Exception
{
//Parameterized constructor
public EmptyQueueException( String s )
{
super( s ); //inherits the existing functionality of the Exception class.
}
}