Advertisement
binibiningtinamoran

LinkedQueue

May 27th, 2019
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.76 KB | None | 0 0
  1. // Write a splice() method
  2.  
  3. /**
  4.  * A class that implements a queue of objects by using a chain of linked nodes
  5.  * that has both head and tail references.
  6.  *
  7.  * @author Frank M. Carrano
  8.  * @author Timothy M. Henry
  9.  * @version 5.0
  10.  */
  11. public final class LinkedQueue<T> implements QueueInterface<T> {
  12.     private Node firstNode; // References node at front of queue
  13.     private Node lastNode; // References node at back of queue
  14.  
  15.     public LinkedQueue() {
  16.         firstNode = null;
  17.         lastNode = null;
  18.     } // end default constructor
  19.  
  20.     public void enqueue(T newEntry) {
  21.         Node newNode = new Node(newEntry, null);
  22.  
  23.         if (isEmpty())
  24.             firstNode = newNode;
  25.         else
  26.             lastNode.setNextNode(newNode);
  27.  
  28.         lastNode = newNode;
  29.     } // end enqueue
  30.  
  31.     public T getFront() {
  32.         if (isEmpty())
  33.             throw new EmptyQueueException();
  34.         else
  35.             return firstNode.getData();
  36.     } // end getFront
  37.  
  38.     public T dequeue() {
  39.         T front = getFront(); // Might throw EmptyQueueException
  40.         // Assertion: firstNode != null
  41.         firstNode.setData(null);
  42.         firstNode = firstNode.getNextNode();
  43.  
  44.         if (firstNode == null)
  45.             lastNode = null;
  46.  
  47.         return front;
  48.     } // end dequeue
  49.  
  50.     public boolean isEmpty() {
  51.         return (firstNode == null) && (lastNode == null);
  52.     } // end isEmpty
  53.  
  54.     public void clear() {
  55.         firstNode = null;
  56.         lastNode = null;
  57.     } // end clear
  58.  
  59.     public void display() {
  60.         Node current = firstNode;
  61.         while(current!=null) {
  62.             System.out.print(current.data + " ");
  63.             current = current.next;
  64.         }
  65.         System.out.println();
  66.     }
  67.  
  68.     public void splice(LinkedQueue<T> secondQueue) {
  69.         Node currentNode = secondQueue.firstNode;
  70.         if (secondQueue.isEmpty()) {
  71.             System.out.println(String.valueOf(secondQueue) + " is empty.");
  72.         }
  73.         while (currentNode != null) {
  74.             this.enqueue(currentNode.getData());
  75.             //lastNode = currentNode;
  76.             currentNode = currentNode.next;
  77.         }
  78.     }
  79.  
  80.     public T printLastNode() {
  81.         if (lastNode != null) {
  82.             return this.lastNode.getData();
  83.         } else {
  84.             return null;
  85.         }
  86.     }
  87.  
  88.     public T printFirstNode() {
  89.         if (firstNode != null) {
  90.             return this.firstNode.getData();
  91.         } else {
  92.             return null;
  93.         }
  94.     }
  95.  
  96.     private class Node {
  97.         private T data; // Entry in queue
  98.         private Node next; // Link to next node
  99.  
  100.         private Node(T dataPortion) {
  101.             data = dataPortion;
  102.             next = null;
  103.         } // end constructor
  104.  
  105.         private Node(T dataPortion, Node linkPortion) {
  106.             data = dataPortion;
  107.             next = linkPortion;
  108.         } // end constructor
  109.  
  110.         private T getData() {
  111.             return data;
  112.         } // end getData
  113.  
  114.         private void setData(T newData) {
  115.             data = newData;
  116.         } // end setData
  117.  
  118.         private Node getNextNode() {
  119.             return next;
  120.         } // end getNextNode
  121.  
  122.         private void setNextNode(Node nextNode) {
  123.             next = nextNode;
  124.         } // end setNextNode
  125.     } // end Node
  126. } // end Linkedqueue
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement