Queue Interface using Java

Soldato
Joined
15 Aug 2010
Posts
8,780
Location
N. Ireland
Hi all, have a little practical for one of my modules, Data Structures and Algorithm.

The problem
A queue is a data structure where new items enter at the back or rear, and items leave from the
front.In your textbook and in your lectures different implementations of a Queue have been discussed.The first part of this assessment is to implement a Csc2001Queue in a file Csc2001Queue.java which implements the CscQueueInterface. Download the interface CscQueueInterface.java from Queens online. Implement the eight methods described in the interface. You can use an array, a circular array,a java Linked List or the generic linked list you created in Practical 2 in your implementation.

It asks to implement the following methods:
offer (E item)
E poll
E remove
E peek
E element
size
printValues

I have the following code written, but when using a tester class to print the values, I dont get my expected outputs.

Code:
import java.util.AbstractQueue;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Queue;

public class Csc2001QueueInterface<E> extends AbstractQueue<E> implements Queue<E>{
	
	//Data field
	private LinkedList<E> theQueue = new LinkedList<E>();
	/**
     * Inserts an item at the rear of the queue.
     * @param item The element to add
     * @return true (always successful)
     */
	
	@Override
	public boolean offer(E item){
		theQueue.addLast(item);
		return true;
	}
	
	
	 /**
     * Removes the entry at the front of the queue and returns it.
     * @return The item removed if successful, or null if not
     */
	@Override
	public E poll(){
		if (size() == 0)
			return null;
			else 
				return theQueue.remove(0);
	}
	
	 /**
     * Removes the entry at the front of the queue and returns it.
     * @return The item removed if successful
	   @throws NoSuchElementException if the queue is empty
     */
	@Override
	public E remove(){
		if (size()==0)
			 throw new NoSuchElementException();
		else
			return theQueue.remove(0);
	}
	
	 /**
     * Returns the item at the front of the queue without removing it.
     * @return The item at the front if successful; null if not
     */
	@Override
	public E peek(){
		if (size()==0)
			return null;
		else return theQueue.getFirst();
	}
	
	   /**
     * Retrieves, but does not remove the entry at the front of the queue
     * and returns it.
     * @return The first entry in the queue if successful
     * @throws NoSuchElementException if this queue is empty
     */
	@Override
	public E element(){
		if (size() ==0)
			throw new NoSuchElementException();
		else
			return theQueue.get(0);
	}
	
	/**
     * Returns the number of elements in the queue
     * @return The number of elements in the queue
     */
	@Override
	public int size(){
		return theQueue.size();
	}
	
	/**
     * Returns true if this queue is empty
     * @return true if this queue is empty
     */
	@Override
	public boolean isEmpty() {
		return (theQueue == null);
	}
	
	 /**
     * Returns a String containing a concatenation of all the elements in this queue, leaving the queue unchanged
     * @return a String
     */

    public List<E> printValues(){
    	
    	List<E> result = new ArrayList<E>();
		if (!isEmpty())
		{
			result.add(theQueue.remove());
		}
		return result;
	
    }

Here is the tester class which was supposed to add the five names in the queue and print the values.

Code:
public class QueueTester {
	public static void main(String[] args)
	{
		Csc2001QueueInterface<String> queueList = new Csc2001QueueInterface<String>();
		queueList.offer("Jack");
		queueList.offer("Joe");
		queueList.offer("Jill");
		queueList.offer("Jane");
		queueList.offer("Jasper");
		
		System.out.println(queueList);
	}

}

When the tester class is ran, I get the following output
Csc2001QueueInterface@15db9742

Could someone kindle point me towards the right direction please.

Thanks.
 
Soldato
OP
Joined
15 Aug 2010
Posts
8,780
Location
N. Ireland
When I changed the print method/line to

Code:
queueList.printValues();

It doesn't output anything, so I'm guessing it's how I implemented the offer() method or the printValues() that's the problem.
 
Back
Top Bottom