Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Linked Queue Implementation

  1. #1
    Junior Member
    Join Date
    Jul 2012
    Posts
    9
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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?

    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
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    494
    Thanks
    8
    Thanked 48 Times in 46 Posts

    Default Re: Linked Queue Implementation

    This thread has been cross posted here:

    http://www.java-forums.org/new-java/70938-linked-queue-implementation.html

    Although cross posting is allowed, for everyone's benefit, please read:

    Java Programming Forums Cross Posting Rules

    The Problems With Cross Posting


  3. #3
    Junior Member
    Join Date
    Jul 2012
    Posts
    9
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Linked Queue Implementation

    Ok, sadly that still doesn't help me with my Question?

Similar Threads

  1. [Linked List] Problems deleting items from a linked list
    By KLVTZ in forum What's Wrong With My Code?
    Replies: 7
    Last Post: March 8th, 2013, 09:21 PM
  2. [Linked List] Problems deleting items from a linked list
    By KLVTZ in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 8th, 2013, 07:52 PM
  3. Implementing a Linked List into a Queue class?
    By orbin in forum What's Wrong With My Code?
    Replies: 0
    Last Post: November 9th, 2012, 12:07 AM
  4. Priority Queue help
    By BuhRock in forum What's Wrong With My Code?
    Replies: 7
    Last Post: November 3rd, 2011, 06:37 PM
  5. queue question
    By Herah in forum Object Oriented Programming
    Replies: 2
    Last Post: November 3rd, 2011, 06:04 AM