binibiningtinamoran

LinkedQueue

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