Contents    Page-10    Prev    Next    Page+10    Index   

Linked List Stack

We could just as easily use a linked list to implement the stack. The testBalance program is the same for either version. The stack abstract data type uses the same interface for both implementations.


public class charStack {
    charStack link;
    char contents;
    public void push(char c) {
        charStack newitem = new charStack();
        newitem.link = link;
        newitem.contents = c;
        link = newitem; }
    public char pop() {
        char c = link.contents;
        link = link.link;
        return c; }
    public boolean empty() {
        return ( link == null ); } }

This data structure is essentially the same as the one for Cons.