need help with Queue program

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • satan
    New Member
    • Oct 2006
    • 28

    #1

    need help with Queue program

    I need the definitions of the method copyQueue, the default constructor, and the copy constructor folr the class LinkedQueueClas s.


    Code:
    This is my default constructor method
    
    //default constructor
        public LinkedQueueClass()
        {
            queueFront = null;
            queueRear = null;
       }
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by satan
    I need the definitions of the method copyQueue, the default constructor, and the copy constructor folr the class LinkedQueueClas s.


    Code:
    This is my default constructor method
     
    //default constructor
    public LinkedQueueClass()
    {
    queueFront = null;
    queueRear = null;
    }
    Depends on the structure of the rest of your LinkedQueueClas s. eg are you using a vector, array, ArrayList
    eg
    Code:
     public LinkedQueueClass(LinkedQueueClass list) { 
      queueFront = list.getFront();
      queueRear = list.getRear();
      list = list.getList(); 
    }

    Comment

    • satan
      New Member
      • Oct 2006
      • 28

      #3
      This is the entire class

      Code:
      public class LinkedQueueClass1
      {
                //Definition of the node
          protected class QueueNode
          {
              DataElement info;
              QueueNode link;
          }
      
          private QueueNode queueFront; //reference variable to the
                                        //first element of the queue
          private QueueNode queueRear;  //reference variable to the
                                        //last element of the queue
              //default constructor
          public LinkedQueueClass()
          {
              queueFront = null;
              queueRear = null;
          }
      
      
              //copy constructor
          public LinkedQueueClass(LinkedQueueClass otherQueue)
          {
             NEED HELP HERE
          }
      //end copy constructor
      
              //Method to initialize the queue to an empty state.
              //Postcondition: queueFront = null; queueRear = null
           public void initializeQueue()
           {
                queueFront = null;
                queueRear = null;
           }
      
              //Method to determine whether the queue is empty.
              //Postcondition: Returns true if the queue is empty;
              //               otherwise, returns false.
           public boolean isEmptyQueue()
           {
                return (queueFront == null);
           }
      
      
              //Method to determine whether the queue is full.
              //Postcondition: Returns true if the queue is full;
              //               otherwise, returns false.
           public boolean isFullQueue()
           {
                return false;
           }
      
              //Method to return the first element of the queue.
              //Precondition: The queue exists and is not empty.
              //Postcondition: If the queue is empty, the method throws
              //               QueueUnderflowException; otherwise, a
              //               reference to a copy of the first element
              //               of the queue is returned.
           public DataElement front() throws QueueUnderflowException
           {
                if(isEmptyQueue())
                   throw new QueueUnderflowException();
      
                DataElement temp = queueFront.info.getCopy();
                return temp;
           }
      
              //Method to return the last element of the queue.
              //Precondition: The queue exists and is not empty.
              //Postcondition: If the queue is empty, the method throws
              //               QueueUnderflowException; otherwise, a
              //               reference to a copy of the last element
              //               of the queue is returned.
           public DataElement back() throws QueueUnderflowException
           {
                if(isEmptyQueue())
                   throw new QueueUnderflowException();
      
                DataElement temp = queueRear.info.getCopy();
                return temp;
           }
      
      
              //Method to add queueElement to the queue.
              //Precondition: The queue exists.
              //Postcondition: The queue is changed and queueElement
              //               is added to the queue.
           public void addQueue(DataElement newElement)
           {
                QueueNode newNode;
      
                newNode = new QueueNode();  //create the node
      
                newNode.info = newElement.getCopy();  //store the info
                newNode.link = null;   //initialize the link field to null
      
                if(queueFront == null) //if initially the queue is empty
                {
                   queueFront = newNode;
                   queueRear = newNode;
                }
                else   //add newNode at the end
                {
                   queueRear.link = newNode;
                   queueRear = queueRear.link;
                }
           }//end addQueue
      
      
              //Method to remove the first element of the queue.
              //Precondition: The queue exists and is not empty.
              //Postcondition: The queue is changed and the first
              //               element is removed from the queue.
           public void deleteQueue() throws QueueUnderflowException
           {
                if(isEmptyQueue())
                   throw new QueueUnderflowException();
      
                queueFront = queueFront.link; //advance queueFront
      
                if(queueFront == null)  //if after deletion the queue is
                   queueRear = null;  //empty, set queueRear to null
           } //end deleteQueue
      
      
              //Method to make a copy of otherQueue.
              //Postcondition: A copy of otherQueue is created and
              //               assigned to this queue.
          public void copyQueue(LinkedQueueClass otherQueue)
          {
            NEED HELP HERE
          }
      }

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Code:
         
         //Your queue has only 2 elements so just copy them
        	public LinkedQueueClass(LinkedQueueClass otherQueue)
        	{
        	   queueFront = otherQueue.queueFront;
        	   queueRear = otherQueue.queueRear;
        	} 
         
         
        public void copyQueue(LinkedQueueClass otherQueue)
        	{
        	  //same as copy constructor
        	  queueFront = otherQueue.queueFront;
        	  queueRear = otherQueue.queueRear;
        	}

        Comment

        • r035198x
          MVP
          • Sep 2006
          • 13225

          #5
          Comments on your Linked Queue

          1) Maybe include the size for easier reference
          Code:
           
          public class LinkedQueueClass {
            private QueueNode queueFront; //reference variable to the
            private QueueNode queueRear;  
            private int size;
          
            public LinkedQueue() {
          	size = 0;
          	queueFront = null;
          	queueRear= null;
            }
          ....
          2) Consider implementing Queue interface from java library

          Comment

          • satan
            New Member
            • Oct 2006
            • 28

            #6
            Originally posted by r035198x
            Comments on your Linked Queue

            1) Maybe include the size for easier reference
            Code:
             
            public class LinkedQueueClass {
              private QueueNode queueFront; //reference variable to the
              private QueueNode queueRear;  
              private int size;
            
              public LinkedQueue() {
            	size = 0;
            	queueFront = null;
            	queueRear= null;
              }
            ....
            2) Consider implementing Queue interface from java library



            I'am on it, i will let u know if it's compiling. Thanks

            Comment

            Working...