package com.datastructure;

public class Node {
    public int data;
    public Node next;

    public Node (int data) {
        this.data = data;
    }
}

package com.datastructure;

public class LLStack {
    Node topStackNode;

    public LLStack() {
        topStackNode = null;
    }

    public void push(int value) {
        Node formerNodeTop = topStackNode;
        topStackNode = new Node(value);
        topStackNode.next = formerNodeTop;
    }

    public int pop() {
        if (topStackNode == null) {
            System.out.println("Stack is empty and no values available to pop!!");
            return -99;
        }
        int value = topStackNode.data;
        topStackNode = topStackNode.next;
        return value;
    }
}

package com.datastructure;

public class StackLinkedList {
    public static void printTheStack(Node stackList) {
        Node tempStack = stackList;
        while (tempStack != null) {
            System.out.println("Stack Item : " + tempStack.data);
            tempStack = tempStack.next;
        }
    }

    public static void main(String[] args) {
        LLStack linkedls = new LLStack();

        linkedls.push(20);
        linkedls.push(40);
        linkedls.push(60);
        linkedls.push(80);
        linkedls.push(100);
        linkedls.push(120);

        System.out.println("Integer element removed from the Linked List Based Stack : " + linkedls.pop());
        System.out.println("Integer element removed from the Linked List Based Stack : " + linkedls.pop());
        System.out.println("Integer element removed from the Linked List Based Stack : " + linkedls.pop());
        linkedls.push(140);
        System.out.println("Integer element removed from the Linked List Based Stack : " + linkedls.pop());
        linkedls.push(160);
        System.out.println("Integer element removed from the Linked List Based Stack : " + linkedls.pop());

        printTheStack(linkedls.topStackNode);

        System.out.println("Integer element removed from the Linked List Based Stack : " + linkedls.pop());
        System.out.println("Integer element removed from the Linked List Based Stack : " + linkedls.pop());
        System.out.println("Integer element removed from the Linked List Based Stack : " + linkedls.pop());
        System.out.println("Integer element removed from the Linked List Based Stack : " + linkedls.pop());
    }
}
