Question

In: Computer Science

Complete the following program:LinkedQueue.zip package jsjf; /** * Represents a node in a linked list. */...

Complete the following program:LinkedQueue.zip

package jsjf;

/**
* Represents a node in a linked list.
*/
public class LinearNode<T>
{
   private LinearNode<T> next;
   private T element;

   /**
   * Creates an empty node.
   */
   public LinearNode()
   {
       next = null;
       element = null;
   }

   /**
   * Creates a node storing the specified element.
   * @param elem element to be stored
   */
   public LinearNode(T elem)
   {
       next = null;
       element = elem;
   }

   /**
   * Returns the node that follows this one.
   * @return reference to next node
   */
   public LinearNode<T> getNext()
   {
       return next;
   }

   /**
   * Sets the node that follows this one.
   * @param node node to follow this one
   */
   public void setNext(LinearNode<T> node)
   {
       next = node;
   }

   /**
   * Returns the element stored in this node.
   * @return element stored at the node
   */
   public T getElement()
   {
       return element;
   }

   /**
   * Sets the element stored in this node.
   * @param elem element to be stored at this node
   */
   public void setElement(T elem)
   {
       element = elem;
   }
}

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

package jsjf;

import jsjf.exceptions.*;

/**
* LinkedQueue represents a linked implementation of a queue.
*/
public class LinkedQueue<T> implements QueueADT<T>
{
   private int count;
   private LinearNode<T> head, tail;

   /**
   * Creates an empty queue.
   */
   public LinkedQueue()
   {
       count = 0;
       head = tail = null;
   }

   /**
   * Adds the specified element to the tail of this queue.
   * @param element the element to be added to the tail of the queue
   */
   public void enqueue(T element)
   {
       LinearNode<T> node = new LinearNode<T>(element);

       if (isEmpty())
           head = node;
       else
           tail.setNext(node);

       tail = node;
       count++;
   }

   /**
   * Removes the element at the head of this queue and returns a
   * reference to it.
   * @return the element at the head of this queue
   * @throws EmptyCollectionException if the queue is empty
   */
   public T dequeue() throws EmptyCollectionException
   {
       if (isEmpty())
           throw new EmptyCollectionException("queue");

       T result = head.getElement();
       head = head.getNext();
       count--;

       if (isEmpty())
           tail = null;

       return result;
   }

   /**
   * Returns a reference to the element at the head of this queue.
   * The element is not removed from the queue.
   * @return a reference to the first element in this queue
   * @throws EmptyCollectionsException if the queue is empty
   */
   public T first() throws EmptyCollectionException
   {
       // To be completed as a Programming Project
      
       return null; // temp
   }

   /**
   * Returns true if this queue is empty and false otherwise.
   * @return true if this queue is empty
   */
   public boolean isEmpty()
   {
       // To be completed as a Programming Project
      
       return true; // temp
   }

   /**
   * Returns the number of elements currently in this queue.
   * @return the number of elements in the queue
   */
   public int size()
   {
       // To be completed as a Programming Project
      
       return 0; // temp
   }

   /**
   * Returns a string representation of this queue.
   * @return the string representation of the queue
   */
   public String toString()
   {
       // To be completed as a Programming Project
      
       return ""; // temp
   }
}

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

package jsjf;

/**
* QueueADT defines the interface to a queue collection.
*/
public interface QueueADT<T>
{
   /**
   * Adds one element to the rear of this queue.
   * @param element the element to be added to the rear of the queue
   */
   public void enqueue(T element);

   /**
   * Removes and returns the element at the front of this queue.
   * @return the element at the front of the queue
   */
   public T dequeue();

   /**
   * Returns without removing the element at the front of this queue.
   * @return the first element in the queue
   */
   public T first();

   /**
   * Returns true if this queue contains no elements.
   * @return true if this queue is empty
   */
   public boolean isEmpty();

   /**
   * Returns the number of elements in this queue.
   * @return the integer representation of the size of the queue
   */
   public int size();

   /**
   * Returns a string representation of this queue.
   * @return the string representation of the queue
   */
   public String toString();
}

Solutions

Expert Solution

/**
   * Returns a reference to the element at the head of this queue.
   * The element is not removed from the queue.
   * @return a reference to the first element in this queue
   * @throws EmptyCollectionsException if the queue is empty
   */
   public T first() throws EmptyCollectionException
   {

if (isEmpty()) // check Queue is empty or not.
           throw new EmptyCollectionException("queue");   


return head.getElement(); // return reference of head
   }

/**
   * Returns true if this queue is empty and false otherwise.
   * @return true if this queue is empty
   */
   public boolean isEmpty()
   {

return (count == 0); // return true or false.
   }

/**
   * Returns the number of elements currently in this queue.
   * @return the number of elements in the queue
   */
   public int size()
   {   

   return count;                                           // return number of elements 
  }

/**
   * Returns a string representation of this queue.
   * @return the string representation of the queue
   */
   public String toString()
   {
String result = "";

LinearNode<T> temp1 = head;

while (temp1 != null) // terminate when queue is at tail

{

result = result + (temp1.getElement()).toString() + "\n"; // store queue elements in string

temp1 = temp1.getNext();

}

return result; // return string
   }


Related Solutions

Complete the following program: LinkedStack.zip package jsjf; /** * Represents a node in a linked list....
Complete the following program: LinkedStack.zip package jsjf; /** * Represents a node in a linked list. */ public class LinearNode<T> {    private LinearNode<T> next;    private T element;    /**    * Creates an empty node.    */    public LinearNode()    {        next = null;        element = null;    }    /**    * Creates a node storing the specified element.    * @param elem element to be stored    */    public LinearNode(T...
Assume that a singly linked list is implemented with a header node, but no tail node,...
Assume that a singly linked list is implemented with a header node, but no tail node, and that it maintains only a pointer to the header node. Write a class in C++ that includes methods to a. return the size of the linked list b. print the linked list c. test if a value x is contained in the linked list d. add a value x if it is not already contained in the linked list e. remove a value...
Assume that a singly linked list is implemented with a header node, but no tail node,...
Assume that a singly linked list is implemented with a header node, but no tail node, and that it maintains only a pointer to the header node. Write a class that includes methods to a. return the size of the linked list b. print the linked list c. test if a value x is contained in the linked list d. add a value x if it is not already contained in the linked list e. remove a value x if...
Data Structures on Java Basic Linked List exercises a. Suppose x is a linked-list node and...
Data Structures on Java Basic Linked List exercises a. Suppose x is a linked-list node and not the last node on the list. What is the effect of the following code fragment? x.next = x.next.next b. Singly Linked List has two private instance variables first and last as that point to the first and the last nodes in the list, respectively. Write a fragment of code that removes the last node in a linked list whose first node is first....
I've provided a Node class that implements a node of a simple singly-linked list (with .value...
I've provided a Node class that implements a node of a simple singly-linked list (with .value and .next fields), and an empty LinkedList class. Your task is to implement LinkedList.sort(l), where given the node l as the head of a singly-linked list, LinkedList.sort(l) sorts the nodes in the list into ascending order according to the values in the .value field of each node. Your implementation should do an in-place update of the list. It is ok to use a simple...
The following is how we define a linear linked list node: (USE IT IN QUESTIONS 1...
The following is how we define a linear linked list node: (USE IT IN QUESTIONS 1 & 2)     struct node    { int item; node* next;            node(int x, node* t)          { item = x; next = t; }     };     typedef node* link; Write a C++ function void moveMaxToLast(link h) that takes as a parameter a link to the head of a linked list L. After this function performs, L still contains the same item values with...
The following is how we define a linear linked list node: (USE IT IN QUESTIONS 1...
The following is how we define a linear linked list node: (USE IT IN QUESTIONS 1 & 2) struct node { int item; node* next; node(int x, node* t) { item = x; next = t; } }; typedef node* link; Write a C++ function void moveMaxToLast(link h) that takes as a parameter a link to the head of a linked list L. After this function performs, L still contains the same item values with the following change in order...
Python 3 Function which takes the head Node of a linked list and sorts the list...
Python 3 Function which takes the head Node of a linked list and sorts the list into non-descending order. PARAM: head_node The head of the linked list RETURNS: The node at the head of the sorted linked list. ''' def sort(head_node): #Code goes here ''' Test code goes here '' ' if __name__ == '__main__':
You are given a reference to the head node of a linked list that stores integers....
You are given a reference to the head node of a linked list that stores integers. Please print the minimum element in this linked list. The class ListNode.java contains the description of a single node in the linked list. It has a num field to store the integer number and a reference next that points to the next element in the list. The file MyList.class is a pre-defined java code, that creates a linked list. The file ListSmallest.java creates an...
You are given a reference to the head node of a linked list that stores integers....
You are given a reference to the head node of a linked list that stores integers. Please print the minimum element in this linked list. The class ListNode.java contains the description of a single node in the linked list. It has a num field to store the integer number and a reference next that points to the next element in the list. The file MyList.class is a pre-defined java code, that creates a linked list. The file ListSmallest.java creates an...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT