Stack

public class Stack <T>{

    private Node<T> head;

   

    public Stack(){

        this.head = null;

    }

    public void push(T x){

        Node <T> tmp = new Node<T>(x);

        tmp.setNext(head);

        head = tmp;

    }

    public T top(){

        return head.getValue();

    }

    public T pop(){

        T x = head.getValue();

        head = head.getNext();

        return x;

    }

    public boolean isEmpty(){

        return (head == null);

    }

   

    public void print(){

        Node <T> tmp = head;

        System.out.print("head –> ");

        while (tmp != null){

            System.out.print(tmp.getValue()+" –> ");

            tmp = tmp.getNext();

        }

        System.out.println("null");

    }

}

כתיבת תגובה