用链表实现栈

public class Link {
    public long lData;
    public Link next;//reference to next Link

    public Link(long lData){
        this.lData=lData;
    }
    public void displayLink(){
        System.out.print("{"+lData+"}");
    }
}
public class LinkList {
    private Link first;

    public LinkList(){
        first=null;
    }

    public boolean isEmpty(){
        return (first==null);
    }   

    public void insertFirst(long dd){
        Link newLink = new Link(dd);
        newLink.next=first;
        first=newLink;
    }

    public long deleteFirst(){
        Link temp = first;//save reference to link
        first = first.next;//delete it 
        return temp.lData;
    }

    public void displayList(){
        Link current =first;
        while(current!=null){
            current.displayLink();
            current=current.next;
        }
        System.out.println("");
    }
}
public class LinkStack {
    private LinkList theList;

    public LinkStack(){
        theList=new LinkList();
    }

    public void push(long j){
        theList.insertFirst(j);
    }

    public long pop(){
        return theList.deleteFirst();
    }

    public boolean isEmpty(){
        return (theList.isEmpty());
    }

    public void displayStack(){
        theList.displayList();
    }
}
public class LinkStackApp {
    /** * @param args */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        LinkStack theStack = new LinkStack();
        theStack.push(20);
        theStack.push(40);
        theStack.displayStack();
        theStack.pop();
        theStack.pop();
        theStack.displayStack();
    }
}

你可能感兴趣的:(链表,栈)