A splice method combines two queues by adding all elements o

A splice method combines two queues by adding all elements of a second queue to the end of a first queue. Write a splice method from the implementation perspective. This method will go inside of the LinkedQueue class. public void splice(LinkedQueue secondQueue) The secondQueue should not be altered when the method completes. For full credit, you must take advantage of being able to directly access the linked node chain of secondQueue. Your solution should not be the same as Q5. In other words, for an efficient solution, do not destroy and rebuild secondQueue. Note that it is okay to invoke the O(1) methods in the current LinkedQueue class!

public class LinkedQueue<T> implements QueueInterface<T>, java.io.Serializable
{
private Node firstNode; // references node at front of queue
private Node lastNode; // references node at back of queue
  
   public LinkedQueue()
   {
       firstNode = null;
       lastNode = null;
   } // end default constructor
  
   public void enqueue(T newEntry)
   {
       Node newNode = new Node(newEntry, null);
      
       if (isEmpty())
           firstNode = newNode;
       else
           lastNode.setNextNode(newNode);

       lastNode = newNode;
   } // end enqueue

   public T getFront()
   {
       T front = null;
      
       if (!isEmpty())
           front = firstNode.getData();
      
       return front;
   } // end getFront

   public T dequeue()
   {
       T front = null;
      
       if (!isEmpty())
       {
           front = firstNode.getData();
           firstNode = firstNode.getNextNode();
          
           if (firstNode == null)
               lastNode = null;
       } // end if
      
       return front;
   } // end dequeue
      
   public boolean isEmpty()
   {
       return (firstNode == null) && (lastNode == null);
   } // end isEmpty
  
   public void clear()
   {
       firstNode = null;  
       lastNode = null;
   } // end clear
  
   public void display() {
       Node current = firstNode;
       while(current!=null) {
           System.out.print(current.data + \" \");
           current = current.next;
       }
       System.out.println();
   }
  
   public void splice(LinkedQueue<T> anotherQueue) {
       // YOUR CODE HERE!

   }


   private class Node implements java.io.Serializable
   {
       private T data; // entry in queue
       private Node next; // link to next node

       private Node(T dataPortion)
       {
           data = dataPortion;
           next = null;  
       } // end constructor
      
       private Node(T dataPortion, Node linkPortion)
       {
           data = dataPortion;
           next = linkPortion;  
       } // end constructor

       private T getData()
       {
           return data;
       } // end getData

       private void setData(T newData)
       {
           data = newData;
       } // end setData

       private Node getNextNode()
       {
           return next;
       } // end getNextNode
      
       private void setNextNode(Node nextNode)
       {
           next = nextNode;
       } // end setNextNode
   } // end Node

} // end Linkedqueue

Solution

Please find the required program along with its output. Please see the comments against each line to understand the step.

---------------------------------------------------------------------

OUTPUT:

QUEUE 1:
1 2 3 4 5
QUEUE 2:
6 7 8 9 10
QUEUE 1 AFTER SPLICE:
1 2 3 4 5 6 7 8 9 10

A splice method combines two queues by adding all elements of a second queue to the end of a first queue. Write a splice method from the implementation perspect
A splice method combines two queues by adding all elements of a second queue to the end of a first queue. Write a splice method from the implementation perspect
A splice method combines two queues by adding all elements of a second queue to the end of a first queue. Write a splice method from the implementation perspect

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site