Results 1 to 3 of 3
  1. #1
    DaMarshN is offline Member
    Join Date
    Mar 2013
    Posts
    2
    Rep Power
    0

    Default Linked Queue Implementation

    I am trying to understand linked queue, my teacher has already written a data structure for implementation but I need help under standing how to use is with multiple data elements? What we are doing is creating a game and I need to store user data inf a Linked Queue, in a queue to keep track of who's turn it is. I need to be able to ad plays when created and remove when dies. I will need the following for each player: Player ID, Type, Lifespan, Location.

    Will this type of data structure work and how can I implement to hold the data needed? Or different approach?

    Java Code:
    package dataStructures;
     
    /**
     *	LinkedQueue class
     *
     *	implementation of a linked queue
     *
     */
    public class LinkedQueue implements Queue
    {
     
    	/*************
    	 *	attributes
    	 ************/
     
    	/** node containing item at front of queue */
    	private QueueNode front;
     
    	/** node containing item at rear of queue */
    	private QueueNode back;
     
    	/** current number of items in queue */
    	private int theSize;
     
     
    	/***************
    	 *	constructors
    	 **************/
     
    	/**
    	 *	return a new, empty LinkedQueue
    	 */
    	public LinkedQueue()
    	{
    		// empty this LinkedQueue
    		clear();
    	}
     
     
    	/**************************************
    	 *	methods inherited from class Object
    	 *************************************/
     
    	/**
    	 *	return a String representation of the LinkedQueue
    	 *
    	 *	items are listed from left to right, in comma-delimited fashion,
    	 *	with the leftmost item being the item at the front of the queue, and the
    	 *	rightmost item being the item at the rear of the queue
    	 */
    	public String toString()
    	{
    		String s = "";
    		QueueNode node = front;
     
    		while (node != null)
    		{
    			if (node == back)
    				s += node.theItem.toString();
    			else
    				s += node.theItem.toString() + ", ";
     
    			node = node.next;
    		}
     
    		return s;
    	}
     
     
    	/*****************************************
    	 *	methods inherited from interface Queue
    	 ****************************************/
     
    	/**
    	 *	return the item at the front of the queue; item is not removed
    	 *
    	 *	throws UnderflowException if queue is empty
    	 */
    	public Object getFront()
    	{
    		// throw exception if this LinkedQueue is empty
    		if(isEmpty())
    			throw new UnderflowException("LinkedQueue getFront");
     
    		// return the item at the front of this LinkedQueue
    		return front.theItem;
    	}
     
     
    	/**
    	 *	remove and return the item at the front of the queue
    	 *
    	 *	throws UnderflowException if queue is empty
    	 */
    	public Object dequeue()
    	{
    		// throw exception if empty queue
    		if(isEmpty())
    			throw new UnderflowException("LinkedQueue dequeue");
     
    		// store object being removed 
    		Object returnValue = front.theItem;
     
    		// make next item new front item
    		front = front.next;
     
    		// subtract 1 from size of this LinkedQueue
    		theSize--;
     
    		// return item that was removed
    		return returnValue;
    	}
     
     
    	/**
    	 *	add the specified item to the rear of the queue
    	 */
    	public boolean enqueue(Object obj)
    	{
    		if(isEmpty())
    		{
    			// if this LinkedQueue was empty, need to make both front and back
    			// references link to new item
    			back = front = new QueueNode(obj);
    		}
    		else
    		{
    			// add new item to rear of this LinkedQueue
    			back = back.next = new QueueNode(obj);
    		}
     
    		// add 1 to size of this LinkedQueue
    		theSize++;
     
    		// enqueue successful
    		return true;
    	}
     
     
    	/**********************************************
    	 *	methods inherited from interface Collection
    	 *********************************************/
     
    	/**
    	 *	add the specified item to the rear of the queue
    	 */
    	public boolean add(Object obj)
    	{
    		// invoke enqueue alias method
    		return enqueue(obj);
    	}
     
     
    	/**
    	 *	remove the item at the front of the queue
    	 *	return true if operation is successful
    	 *
    	 *	throws UnderflowException if queue is empty
    	 */
    	public boolean remove()
    	{
    		// invoke dequeue alias method
    		dequeue();
     
    		// remove successful
    		return true;
    	}
     
     
    	/**
    	 *	empty the LinkedQueue
    	 *
    	 *	size will be set to zero
    	 */
    	public void clear()
    	{
    		// reset links to front and rear
    		front = back = null;
     
    		// reset size to 0
    		theSize = 0;
    	}
     
     
    	/**
    	 *	return the number of items in the ArrayQueue
    	 */
    	public int size()
    	{
    		return theSize;
    	}
     
     
    	/**
    	 *	return true if the ArrayQueue is empty
    	 */
    	public boolean isEmpty()
    	{
    		return theSize == 0;
    	}
     
     
    	/**
    	 *	return the item at the front of the queue; item is not removed
    	 *
    	 *	throws UnderflowException if queue is empty
    	 */
    	public Object get()
    	{
    		return getFront();
    	}
     
     
    	/****************
    	 *	inner classes
    	 ***************/
     
    	/**
    	 *	nested class QueueNode
    	 *	encapsulates the fundamental building block of a LinkedQueue
    	 *	contains a data item, and a reference to the next node in the queue
    	 */
    	private static class QueueNode
    	{
     
    		/*************
    		 *	attributes
    		 ************/
     
    		/** the data item */
    		Object theItem;
     
    		/** reference to the next node in the list */
    		QueueNode next;
     
     
    		/***************
    		 *	constructors
    		 **************/
     
    		/**
    		 *	create a new QueueNode containing the specified item
    		 *
    		 *	reference to the new node's next node is null
    		 */
    		public QueueNode(Object obj)
    		{
    			this(obj, null);
    		}
     
     
    		/**
    		 *	create a new QueueNode containing the specified item, and reference
    		 *	to the new node's next node
    		 */
    		public QueueNode(Object obj, QueueNode node)
    		{
    			theItem = obj;
    			next = node;
    		}
     
    	}
     
    }

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,193
    Rep Power
    19

    Default Re: Linked Queue Implementation

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    DaMarshN is offline Member
    Join Date
    Mar 2013
    Posts
    2
    Rep Power
    0

    Default Re: Linked Queue Implementation

    But still no answer to my question.

Similar Threads

  1. Priority Queue Implementation
    By cylus99 in forum New To Java
    Replies: 1
    Last Post: 12-20-2011, 10:48 PM
  2. Queue Implementation
    By Ryanmc56 in forum New To Java
    Replies: 4
    Last Post: 08-01-2011, 06:12 PM
  3. Queue implementation
    By thc in forum New To Java
    Replies: 3
    Last Post: 03-29-2011, 04:14 PM
  4. Linked Lists Queue
    By bdario1 in forum New To Java
    Replies: 0
    Last Post: 04-28-2010, 04:40 AM
  5. Queue implementation - Enqueue problem
    By jefrazie in forum New To Java
    Replies: 5
    Last Post: 11-05-2009, 01:10 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •