public class LinkedList<T> {
    private T data;
    private LinkedList<T> prevNode, nextNode;

    /**
     *  Constructs a new element
     *
     * @param  data, data of object
     * @param  node, previous node
     */
    public LinkedList(T data, LinkedList<T> node)
    {
        this.setData(data);
        this.setPrevNode(node);
        this.setNextNode(null);
    }

    /**
     *  Clone an object,
     *
     * @param  node  object to clone
     */
    public LinkedList(LinkedList<T> node)
    {
        this.setData(node.data);
        this.setPrevNode(node.prevNode);
        this.setNextNode(node.nextNode);
    }

    /**
     *  Setter for T data in DoubleLinkedNode object
     *
     * @param  data, update data of object
     */
    public void setData(T data)
    {
        this.data = data;
    }

    /**
     *  Returns T data for this element
     *
     * @return  data associated with object
     */
    public T getData()
    {
        return this.data;
    }

    /**
     *  Setter for prevNode in DoubleLinkedNode object
     *
     * @param node, prevNode to current Object
     */
    public void setPrevNode(LinkedList<T> node)
    {
        this.prevNode = node;
    }

    /**
     *  Setter for nextNode in DoubleLinkedNode object
     *
     * @param node, nextNode to current Object
     */
    public void setNextNode(LinkedList<T> node)
    {
        this.nextNode = node;
    }


    /**
     *  Returns reference to previous object in list
     *
     * @return  the previous object in the list
     */
    public LinkedList<T> getPrevious()
    {
        return this.prevNode;
    }

    /**
     *  Returns reference to next object in list
     *
     * @return  the next object in the list
     */
    public LinkedList<T> getNext()
    {
        return this.nextNode;
    }

}

public class Stack<T> {

    private LinkedList<T> upper;
    private int size;

    // constructor initiates null LinkedList<T> object + set size to 0
    public Stack() {
        this.upper = null;
        this.size = 0;
    }

    // push method for a new element to the upper value
    public void push(T data) {
        LinkedList<T> newNode = new LinkedList<T>(data, this.upper);
        this.upper = newNode;
        this.size++;
    }

    // peek method, return upper
    public T peek() {
        // try/catch to either return upper or print message if upper doesn't exist
        try {
            return this.upper.getData();
        } catch (NullPointerException e) {
            System.out.println("No upper element, empty stack!");
            return null;
        }
    }

    // pop method, return upper and remove 
    public T pop() {
        // try/catch to either return + pop upper or print message if upper doesn't exist
        try {
            T data = this.upper.getData();
            this.upper = this.upper.getPrevious();
            this.size--;
            return data;
        } catch (NullPointerException e) {
            System.out.println("No upper element, empty stack!");
            return null;
        }
    }

    // get size method
    public int size() {
        return this.size;
    }

    // isEmpty method, compare size to 0
    public boolean isEmpty() {
        return this.size == 0;
    }

    // toString method, from top to bottom
    public String toString() {
        String s = "[ ";
        LinkedList<T> currentNode = upper;
        // gets upper node, then keeps going down to previous until previous is null
        while (currentNode != null) {
            s += currentNode.getData();
            currentNode = currentNode.getPrevious();
            if (currentNode != null) {
                s += ", ";
            }
        }
        s += " ]";
        return s;
    }

    
}

public class Tester {
    public static void main(String[] args) {
        // test stack with Integer wrapper class
        Stack<Integer> s1 = new Stack<Integer>();
        s1.push(1);
        s1.push(2);
        s1.push(3);
        s1.push(4);
        s1.push(5);
        System.out.println(s1.toString());
        System.out.println(s1.peek());
        System.out.println(s1.pop());
        System.out.println(s1.peek());
        System.out.println(s1.size());
        System.out.println(s1.isEmpty());

        // test stack with String class
        Stack<String> s2 = new Stack<String>();
        s2.push("Hello");
        s2.push("my");
        s2.push("name");
        s2.push("is");
        s2.push("Daniel");
        System.out.println(s2.toString());
        System.out.println(s2.peek());
        System.out.println(s2.pop());
        System.out.println(s2.peek());
        System.out.println(s2.size());
        System.out.println(s2.isEmpty());

    }
}

Tester.main(null);
[ 5, 4, 3, 2, 1 ]
5
5
4
4
false
[ Daniel, is, name, my, Hello ]
Daniel
Daniel
is
4
false

Stack T Hack

Hack: Merge two stacks by pushing both until one is empty and then pushing the rest of the other

public class StackMerger<T> {
    private final Stack<T> s1;
    private final Stack<T> s2;
    private Stack<T> s3;
    
    // constructor for Stackmerger
    public StackMerger(Stack<T> s1, Stack<T> s2) {
        this.s1 = s1;
        this.s2 = s2;
        this.merge(s1, s2);
    }
    
    public void merge(Stack<T> s1, Stack<T> s2) {
        Stack<T> mergedStack = new Stack<T>();
        // if both stacks are not empty, 
        while (!s1.isEmpty() && !s2.isEmpty()) {
            mergedStack.push(s1.pop());
            mergedStack.push(s2.pop());
        }
        // if s1 is empty, pop from s2 and push to mergedStack
        while (!s2.isEmpty()) {
            mergedStack.push(s2.pop());
        }
        // if s2 is empty, pop from s1 and push to mergedStack
        while (!s1.isEmpty()) {
            mergedStack.push(s1.pop());
        }
        s3 = mergedStack;
    }

    // toString method using Stack<T> toString
    public String toString() {
        return s3.toString();
    }
}

public class TesterTwo {
    public static void main(String[] args) {
        // test stack with Integer wrapper class
        Stack<Integer> s1 = new Stack<Integer>();
        s1.push(1);
        s1.push(2);
        s1.push(3);
        s1.push(4);
        s1.push(5);
        System.out.println(s1.toString());

        Stack<Integer> s2 = new Stack<Integer>();
        s2.push(6);
        s2.push(7);
        s2.push(8);
        s2.push(9);
        s2.push(10);
        System.out.println(s2.toString());

        StackMerger<Integer> sm1 = new StackMerger<Integer>(s1, s2);
        System.out.println(sm1.toString());

        // test stack with String class
        Stack<String> s3 = new Stack<String>();
        s3.push("Hello");
        s3.push("name");
        s3.push("Daniel");
        System.out.println(s3.toString());
        
        Stack<String> s4 = new Stack<String>();
        s4.push("my");
        s4.push("is");
        System.out.println(s4.toString());

        StackMerger<String> sm2 = new StackMerger<String>(s3, s4);
        System.out.println(sm2.toString());
    }
}

TesterTwo.main(null);
[ 5, 4, 3, 2, 1 ]
[ 10, 9, 8, 7, 6 ]
[ 6, 1, 7, 2, 8, 3, 9, 4, 10, 5 ]
[ Daniel, name, Hello ]
[ is, my ]
[ Hello, my, name, is, Daniel ]