Generic Stack using linkedlist

PHOTO EMBED

Mon May 27 2024 17:32:38 GMT+0000 (Coordinated Universal Time)

Saved by @Asadullah69

class GenericStackLinkedList<T> {
    private Node<T> top;

    public GenericStackLinkedList() {
        this.top = null;
    }

    public void push(T item) {
        Node<T> newNode = new Node<>(item);
        newNode.next = top;
        top = newNode;
    }

    public T pop() {
        if (top == null) {
            System.out.println("Stack Underflow");
            return null;
        }
        T data = top.data;
        top = top.next;
        return data;
    }

    public boolean isEmpty() {
        return top == null;
    }
}
content_copyCOPY