// Arup Guha
// 6/20/06
// 2006 BHCSI Implemented in class: A StackLL class using a linked list.
import java.util.*;

public class StackLL {
	
	// Just need a reference to the top, so I'll use an LL instead of a Node.
	// This will illustrate an object oriented approach.
	private LL top;
	
	// Creates an empty StackLL.
	public StackLL() {
		top = new LL();
	}
	
	// Pushes x onto the StackLL.
	public void push(int x) {
		
		// We just need to insert x at the front of the linked list top.
		top.insertFront(x);
	}
	
	// Returns the top element on the StackLL and pops it.
	// Returns -1 if there's no element to pop.
	public int pop() {
		
		// Default case.
		if (empty())
			return -1;
			
		// Delete the front element of the linked list top.
		else {
			return top.deleteFront();
		}
	}
	
	// Returns the element at the top of the StackLL without popping it.
	// Returns -1 if no such element exists. 
	public int top() {
		if (top == null)
			return -1;
		else
			return top.getFront();
	}
	
	
	// Return true iff the StackLL is empty.
	public boolean empty() {
		return (top==null);
	}
	
	// Mostly for debugging.
	public void print() {
		top.printlist();
	}
	
	public static void main(String[] args) {
		
		// Allows to test a single StackLL object.
		Scanner stdin = new Scanner(System.in);
		System.out.println("Enter choice, 1=push, 2=pop, 3.print");
		int choice = stdin.nextInt();
		
		StackLL test = new StackLL();
		while (choice != 4) {
			if (choice == 1) {
				System.out.println("Elem to push.");
				int item = stdin.nextInt();
				test.push(item);
			}
			else if (choice == 2) {
				int val = test.pop();
				System.out.println("popped "+val);
			}
			else if (choice == 3) {
				test.print();
			}
			
			System.out.println("Enter choice, 1=push, 2=pop, 3.print");
			choice = stdin.nextInt();
		}
		
	}
}