//
//  ListStack.java
//  
//
//  Created by Herbert J. Bernstein on 2/24/11.
//  Copyright 2011 All rights reserved.
//

public class ListStack {
    int value;
    ListStack next;
    boolean hasnext;
    
    ListStack() {
        hasnext = false;
        value = 0;
    }
 
    // A copy constructor, but only for the top level,
    // The rest of the stacks are merged, but because
    // We only work directly with the front, this works.
    ListStack(ListStack l) {
        hasnext = l.hasnext;
        value = l.value;
        next = l.next;
    }
    

    // Insert data in front of the stack
    
    int push ( int value ){
        ListStack cn;
        cn = new ListStack();
        cn.value = value;
        if (hasnext) {
            cn.next = next;
            cn.hasnext = true;
        }
        next = cn;
        hasnext = true;
        return 0;
    }
    
    // pop data from front of the stack
    
    int pop ( ) {
        int retvalue = 0;
        ListStack cn = new ListStack(this);
        if (hasnext) {
            retvalue = next.value;
            hasnext = cn.next.hasnext;
            next = cn.next.next;
        }
        return retvalue;
    }
    
    // test for empty
    
    boolean empty( ) {
        return (!hasnext);
    }
    
    //print list stack
    
    int print( ) {
        ListStack l = this;
        while (l.hasnext) {
            System.out.println(l.next.value);
            l = l.next;
        }
        return 0;
    }
    
}