Java Problem LinkQueue

Associate
Joined
28 May 2008
Posts
346
Last question on my uni assignment and im stuck because i missed the lecture for Z topic, wondering if any1 can help a man in trouble :(

THe question asked to..Complete implementation of the LinkedQueue class presnted in the Appendix that follows, completeing methods...first, isEmpty. size and toString.

package jss2;
import jss2.exceptions.*;
public class LinkedQueue<T> implements QueueADT<T>
{
private int count;
private LinearNode<T> front, rear;
/**
* Creates an empty queue.
*/
public LinkedQueue()
{
count = 0;
front = rear = null;
}
/**
* Adds the specified element to the rear of this queue.
*
* @Param element the element to be added to the rear of this queue
*/
public void enqueue (T element)
{
LinearNode<T> node = new LinearNode<T>(element);
if (isEmpty())
front = node;
else
rear.setNext (node);
rear = node;
count++;
}
/**
* Removes the element at the front of this queue and returns a
* reference to it. Throws an EmptyCollectionException if the
* queue is empty.
*
* @return the element at the front of this queue
* @throws EmptyCollectionException if an empty collection exception
occurs
*/
public T dequeue() throws EmptyCollectionException
{
if (isEmpty())
throw new EmptyCollectionException ("queue");
T result = front.getElement();
front = front.getNext();
count--;
if (isEmpty())
rear = null;
return result;
}


Will keep working on with this myself but any help would be greatly appreciated!! Thanks
 
It's pretty much already done for you.

IsEmpty -> is count 0
Size -> return count
First -> if it isn't empty return front
 
Thanks for the reply, I understand the thoery of it but Im having problems with the code, i had a stab at it and i cant compile without masses of error messages but to be honest I dont think ive done it right correctly atall, would you know how I would go about coding these medthods or any helpful tutorials i could reference, Thanks
 
You're on the last question on this Uni assignment... did you not need to write any code for the previous questions, or are you just lying to get someone to do it for you or something?
 
I imagine there's loads of samples on the net, it's a pretty common degree assignment question. I'll let you fill in the blanks

Code:
return (count == 0);   // IsEmpty -> is count 0

return count;  // Size -> return count

return front.getElement();    // First -> if __________ return front

Although it's not much use to you if I've done your work for you, the comments from my first post were near enough psuedocode. Make sure you understand what it's doing, the generics (T) and the exception. The ToString method is fairly simple, iterate through the queue and format it as you wish.
 
Last edited:
Back
Top Bottom