CS-PROJECTS / towerofhanoi / LinkedStack.java
LinkedStack.java
Raw
package towerofhanoi;

import java.util.EmptyStackException;

// Virginia Tech Honor Code Pledge:
//
// As a Hokie, I will conduct myself with honor and integrity at all times.
// I will not lie, cheat, or steal, nor will I accept the actions of those who
// do.
// -- Jordan Harrington (jordanha23)

/**
 * @author Jordan Harrington
 * @version <3/25/2020>
 * @param <T>
 *            - Type T
 */

public class LinkedStack<T> implements StackInterface<T> {

    /**
     * Private Node class meant for use in LinkedStack
     * 
     * @author Jordan Harrington
     * @version <3/25/2020>
     * @param <T>
     */
    private class Node<T> {

        private Node<T> next;
        private T data;

        /**
         * Constructor for node class
         * 
         * @param data
         *            - the data being stored in the node
         */
        public Node(T data) {
            this.data = data;
            next = null;
        }


        /**
         * Getter for the next node in the stack
         * 
         * @return the next node in the stack
         */
        public Node<T> getNextNode() {
            return next;
        }


        /**
         * Getter for the data in the node
         * 
         * @return the data being stored in the Node
         */
        public T getData() {
            return data;
        }


        /**
         * Setter for next
         * 
         * @param nextNode
         *            - the Node you would like to set as the next Node in the
         *            stack
         */
        public void setNextNode(Node<T> nextNode) {
            this.next = nextNode;
        }
    }

    private Node<T> topNode;
    private int size;

    /**
     * Constructor for LinkedStack
     */
    public LinkedStack() {
        topNode = null;
        size = 0;
    }


    /**
     * @return the size of the stack
     */
    public int size() {
        return size;
    }


    /**
     * Pushes an object onto the stack.
     * 
     * @param anEntry
     *            - disk to be pushed
     *            onto the stack.
     */
    public void push(T anEntry) {
        Node<T> newNode = new Node<T>(anEntry);
        newNode.setNextNode(topNode);
        topNode = newNode;
        size++;
    }


    /**
     * Removes the disk at the top of
     * the stack.
     * 
     * @return The disk that was removed.
     */
    public T pop() {
        if (this.isEmpty()) {
            throw new EmptyStackException();
        }

        Node<T> poppedNode = topNode;
        topNode = topNode.next;
        size--;
        return poppedNode.data;
    }


    /**
     * Checks if the stack is empty.
     * 
     * @return Returns true if the stack is empty.
     */
    public boolean isEmpty() {
        return (size == 0);
    }


    /**
     * Checks the disk at the top of the
     * stack without removing it.
     * 
     * @return disk at the top of the stack.
     */
    public T peek() {
        if (this.isEmpty()) {
            throw new EmptyStackException();
        }
        return topNode.data;
    }


    /**
     * Clears the stack (removes all of
     * the disks from the stack).
     */
    public void clear() {
        topNode = null;
        size = 0;
    }


    /**
     * toString method which displays each item in the stack within brackets
     * 
     * @return a string signifying each push on a stack
     */
    public String toString() {
        String toString = "[";
        Node<T> current = topNode;
        while (current != null) {
            toString = toString + (current.getData().toString());
            if (current.getNextNode() != null) {
                toString = toString + ", ";
            }
            current = current.getNextNode();
        }

        toString = toString + "]";
        return toString;
    }

}