How to Implement a Stack in Java

If you are a computer science major when you take Data Structures and Algorithms you are going to have to learn about a Stack.  Java has a built in Stack class that you can use so you would probably never have to implement your own. In this example we will start with the Stack interface to formally define how our stack will be implemented.

Stack.java


/**
*
* @author Copypasteearth
*interface for the Stack implementation
* @param <T>
* generic parameter T
*/
public interface Stack<T>
{
/*
* push element onto the top of the stack
* @element
* the item to be pushed onto the top of the stack
*
*/
public void push(T element);
/**
* removes the top element from the stack and returns it
* @throws
* throws stackunderflowexception if the stack is empty
* @return
* returns T element
*/
public T pop();
/**
* lets you see the current top element of the stack without removing it
* @throws
* throws stackunderflowexception if the stack is empty
* @return
* returns T element (the top of the stack)
*/
public T top();
/**
* checks to see if the stack is empty
* @return
* returns true if the stack is empty, false otherwise
*/
public boolean isEmpty();
/**
* gets the size of the stack
* @return
* returns an integer representing how many items are on the stack
*/
public int getSize();

}

Next we will need a class that holds our information so we define the Node class to hold the data and a reference to the next Node in the stack.

Node.java


/**
*
* @author Copypasteearth
* Node class used to dynamically keep track of
* elements on the stack
*/
public class Node<T> {

/** holds the reference to Object of the node*/
public T data;
/** holds the reference to next node of the stack*/
public Node<T> next;
/**
*
* @param element
* element to be placed in data variable of Node
* @param next
* Node to be placed in next variable of Node
*/
public Node(T element,Node<T> next) {

this.data = element;
this.next = next;

}

}

Now we will define an Exception just incase someone trys to access the top of the stack and it is empty.

StackUnderflowException.java


/**
*
* @author Copypasteearth
* StackUnderflowException to be thrown when the
* stack tries to perform a getter method on an empty stack
*/
public class StackUnderflowException extends RuntimeException {

public StackUnderflowException() {
super();
// TODO Auto-generated constructor stub
}

public StackUnderflowException(String arg0) {
super(arg0);
// TODO Auto-generated constructor stub
}

public StackUnderflowException(Throwable arg0) {
super(arg0);
// TODO Auto-generated constructor stub
}

public StackUnderflowException(String arg0, Throwable arg1) {
super(arg0, arg1);
// TODO Auto-generated constructor stub
}

public StackUnderflowException(String arg0, Throwable arg1, boolean arg2,
boolean arg3) {
super(arg0, arg1, arg2, arg3);
// TODO Auto-generated constructor stub
}

}

And finally here is the implementation of the Stack interface, in a class called UnboundedStackImplementation.

UnboundedStackImplementation.java


/**
* @author Copypasteearth
*
* UnboundedStackImplementation is the version of a Stack
* created with the Stack interface dynamically creating
* instances of Node class to keep references to elements
* in the stack
*/
public class UnboundedStackImplementation<T> implements Stack<T> {

/** the count of how many elements are in the stack */
private int count;
/** the reference to the element on the top of the stack */
private Node<T> top;

/**
* constructor sets values of top and count and returns an
* instance of UnboundedStackImplementation
*/
public UnboundedStackImplementation()
{
this.count = 0;
this.top = null;
}

/*
* push element onto the top of the stack
* @element
* the item to be pushed onto the top of the stack
*
*/
@Override
public void push(T element) {

//create new node with element as data and top as next
Node<T> currentNode = new Node<T>(element,top);
//set top to the new node
top = currentNode;
//increment count
count++;


}

/**
* removes the top element from the stack and returns it
* @throws
* throws stackunderflowexception if the stack is empty
* @return
* returns T element
*/
@Override
public T pop() {
//get T with top method, may throw StackUnderflowException
T result = top();
//set top data to null
top.data = null;
//set top to the top next node
top = top.next;
//decrement count
count--;
return result;

}

/**
* lets you see the current top element of the stack without removing it
* @throws
* throws stackunderflowexception if the stack is empty
* @return
* returns T element (the top of the stack)
*/
@Override
public T top() {

//prepare result T to be returned by top
T result = null;
// if stack is not empty
if(!isEmpty()){
//set result to top variable data
result = top.data;
}else{
// if stack is empty there is no data to be returned
throw new StackUnderflowException("StackUnderflowException -- The Stack is empty!!!");
}

return result;
}

/**
* checks to see if the stack is empty
* @return
* returns true if the stack is empty, false otherwise
*/
@Override
public boolean isEmpty() {
if(top == null)
return true;
else
return false;
}

/**
* gets the size of the stack
* @return
* returns an integer representing how many items are on the stack
*/
@Override
public int getSize() {

return count;
}

}

And that’s it, I hope you appreciate this little demonstration about the data structure that is very commonly used in practice.  There are a ton of different data structures to choose from and some are better in certain situations than others.

Author: John Rowan

I am a Senior Android Engineer and I love everything to do with computers. My specialty is Android programming but I actually love to code in any language specifically learning new things.

Author: John Rowan

I am a Senior Android Engineer and I love everything to do with computers. My specialty is Android programming but I actually love to code in any language specifically learning new things.

%d bloggers like this: