Advertisement
binibiningtinamoran

LinkedStack

May 18th, 2019
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.61 KB | None | 0 0
  1. // Write a method called priorityPush()
  2. // If the stack does not contain the element to be added, push element to stack THEN return false;
  3. // If the stack contains duplicate/s of the element to be added, delete the first instance of
  4. // the duplicate, push the element to be added to stack THEN return true;
  5.  
  6. import java.util.EmptyStackException;
  7.  
  8. /**
  9.  * A class of stacks whose entries are stored in a chain of nodes.
  10.  *
  11.  * @author Frank M. Carrano
  12.  * @author Timothy M. Henry
  13.  * @version 5.0
  14.  */
  15. public final class LinkedStack<T> implements StackInterface<T> {
  16.  
  17.     private Node topNode; // References the first node in the chain
  18.  
  19.     public LinkedStack() {
  20.         topNode = null;
  21.     } // end default constructor
  22.  
  23.     public void push(T newEntry) {
  24.         Node newNode = new Node(newEntry, topNode);
  25.         topNode = newNode;
  26.         // topNode = new Node(newEntry, topNode); // Alternate code
  27.     } // end push
  28.  
  29.     public T peek() {
  30.         if (isEmpty())
  31.  
  32.             throw new EmptyStackException();
  33.         else
  34.             return topNode.getData();
  35.     } // end peek
  36.  
  37.     public T pop() {
  38.         T top = peek(); // Might throw EmptyStackException
  39.         // Assertion: topNode != null
  40.         topNode = topNode.getNextNode();
  41.  
  42.         return top;
  43.     } // end pop
  44.  
  45.     public boolean isEmpty() {
  46.         return topNode == null;
  47.     } // end isEmpty
  48.  
  49.     public void clear() {
  50.         topNode = null; // Causes deallocation of nodes in the chain
  51.     } // end clear
  52.  
  53.     public T peekNext() {
  54.         if (isEmpty()) {
  55.             throw new EmptyStackException();
  56.         } else {
  57.             return topNode.next.getData();
  58.         }
  59.     }
  60.  
  61.  
  62.     @Override
  63.     public String toString() {
  64.         String s = "";
  65.         Node current = topNode;
  66.         while (current != null) {
  67.             s = current.data + " " + s;
  68.             current = current.next;
  69.         }
  70.         return s;
  71.     }
  72.  
  73.    
  74.     // If the stack does not contain the element to be added, push element to stack THEN return
  75.     // false;
  76.     // If the stack contains duplicate/s of the element to be added, delete the first instance of
  77.     // the duplicate, push the element to be added to stack THEN return true;
  78.     public boolean priorityPush(T element) {
  79.         Node currentNode = topNode;
  80.         if (currentNode == null) { // empty chain
  81.             this.push(element);
  82.             return false;
  83.         }
  84.  
  85.         if (currentNode.data.equals(element)) {
  86.             return true;
  87.         }
  88.  
  89.         while (currentNode != null) {
  90.             if (currentNode.next != null && currentNode.next.data.equals(element)) {
  91.                 currentNode.next = currentNode.next.next;
  92.                 //topNode = new Node(element, topNode); // without calling push()
  93.                 this.push(element);
  94.                 return true;
  95.             }
  96.             currentNode = currentNode.next;
  97.         }
  98.         // Duplicate does not exist...
  99.         this.push(element);
  100.         return false;
  101.     } // end priorityPush()
  102.  
  103.     /*
  104.  
  105.     public boolean priorityPush(T element) {
  106.         if (topNode == null) {
  107.             push(element);
  108.             return false;
  109.         }
  110.         T topData = topNode.getData();
  111.         if (topData.equals(element)) {
  112.             return true;
  113.         } else {
  114.             Node previousNode = topNode;
  115.             Node currentNode = topNode.getNextNode();
  116.             while (currentNode != null) {
  117.                 T data = currentNode.getData();
  118.                 if (data.equals(element)) {
  119.                     previousNode.setNextNode(currentNode.getNextNode());
  120.                     currentNode.setNextNode(topNode);
  121.                     topNode = currentNode;
  122.                     return true;
  123.                 } else {
  124.                     previousNode = previousNode.getNextNode();
  125.                     currentNode = currentNode.getNextNode();
  126.                 }
  127.             }
  128.             push(element);
  129.             return false;
  130.         }
  131.     }
  132.     */
  133.  
  134.     public void display() {
  135.         Node current = topNode;
  136.         while(current != null) {
  137.             System.out.print(current.data + " ");
  138.             current = current.next;
  139.         }
  140.         System.out.println();
  141.     }
  142.  
  143.     // Count number of occurrences of specified element
  144.     public int numMatches(T target) {
  145.         LinkedStack<T> temp = new LinkedStack <>();
  146.         int count = 0;
  147.         T val;
  148.         while (topNode != null) {
  149.             val = this.pop();
  150.             if (val.equals(target)) {
  151.                 count++;
  152.             }
  153.             temp.push(val);
  154.         }
  155.         while (!temp.isEmpty()) {
  156.             this.push(temp.pop());
  157.         }
  158.         return count;
  159.     }
  160.  
  161.     public boolean isSingleton() {
  162.         return topNode != null && topNode.next == null;
  163.     }
  164.  
  165.     private class Node {
  166.         private T data; // Entry in stack
  167.         private Node next; // Link to next node
  168.  
  169.         private Node(T dataPortion) {
  170.             this(dataPortion, null);
  171.         } // end constructor
  172.  
  173.         private Node(T dataPortion, Node linkPortion) {
  174.             data = dataPortion;
  175.             next = linkPortion;
  176.         } // end constructor
  177.  
  178.         private T getData() {
  179.             return data;
  180.         } // end getData
  181.  
  182.         private void setData(T newData) {
  183.             data = newData;
  184.         } // end setData
  185.  
  186.         private Node getNextNode() {
  187.             return next;
  188.         } // end getNextNode
  189.  
  190.         private void setNextNode(Node nextNode) {
  191.             next = nextNode;
  192.         } // end setNextNode
  193.     } // end Node
  194.  
  195. } // end LinkedStack
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement