//Stack implementation using arrays

public class Stack
{
	int DEFAULT_MAX_SIZE = 100;
	int [] stack;
	int topIndex = -1;	//the index of the top data element
	
	//default constructor
	public Stack()
	{
	    stack =  = new int[DEFAULT_MAX_SIZE];
	}
	
	//specific constructor that sets the max size of the stack to size
	public Stack(int size)
	{
		stack = new int[size];
	}
	
	//push() takes data and pushes it onto the top of the stack
	public void push(int data)
	{
		if (!isFull())	//check if the stack is full
		{
			topIndex++;	//topIndex must be incremented to the next empty spot
			stack[topIndex] = data;	//set the data (place the tray)
		}
	}
	
	public int pop()
	{
		if (!isEmpty())	//check if stack is empty (cannot pop an empty stack)
		{
			topIndex--;	//decrement topIndex first to update the new topIndex
			return stack[topIndex+1];	//return the data
		}
		
		return Integer.MIN_VALUE;  //return error code if nothing to pop
	}
	
	//top() returns the data on top (no popping)
	public int top()
	{
		if (!isEmpty())
			return stack[topIndex];
		return Integer.MIN_VALUE;  //return error code if empty
	}
	
	//print() outputs all the elements in the stack
	public void print()
	{
		//start at topIndex and go down (print the stack from top to bottom)
		for (int i = topIndex; i >= 0; i--)
			System.out.println("Stack Index[" + i + "] => " + stack[i]);
	}
	
	//isEmpty() returns true if the stack is empty and false otherwise
	public boolean isEmpty()
	{
		return topIndex == -1;

		// equivalent to above
	        // if (topIndex == -1)
		// {
		// 	return true;
		// }
		// else
		// {
		// 	return false;
		// }
	}
	
	//isFull() returns true if the stack is full and false otherwise
	public boolean isFull()
	{
        	return topIndex == stack.length-1

        	// equivalent to above
		// if (topIndex == stack.length-1)
		// {
		// 	return true;
		// }
		// else
		// {
		// 	return false;
		// }
	}
}
