6. Stack (LIFO)

Conceptual View

Stack Interface

public interface StackInterface<AnyType> { void push(AnyType item); // O(1) AnyType pop(); // O(1) AnyType peek(); // O(1) boolean isEmpty(); // O(1) }

Stack Implementation using Array

public class ArrayStack<AnyType> implements StackInterface<AnyType> { private static final int DEFAULT_CAPACITY = 10; // Constant private int top; private Object[] elements; public ArrayStack(int initialCapacity) { if (initialCapacity <= 0) { elements = new Object[DEFAULT_CAPACITY]; } else { elements = new Object[initialCapacity]; } // Set the top to be -1, indicating the stack is empty top = -1; } public ArrayStack() { this(DEFAULT_CAPACITY); // Calls the constructor with default capacity } // TODO: implement all the methods here ... }

isEmpty

/** * Checks if the stack is empty in O(1). * @return true if it is empty, false otherwise */ @Override public boolean isEmpty() { return top == -1; }

Push

/** * Pushes a new item onto the top of the stack in O(1). * @param item, a new item to add * @throws StackException if stack is full */ @Override public void push(AnyType item) { if (top == (elements.length - 1)) { throw new StackException("Stack is full"); } top = top + 1; elements[top] = item; }

Pop

/** * Gets and removes the top item in O(1). * @return top, item on the top * @throws StackException indicating the stack is empty */ @Override public AnyType pop() { if (isEmpty()) { throw new StackException("Stack is empty"); } @SuppressWarnings("unchecked") AnyType item = (AnyType) elements[top]; elements[top] = null; top = top - 1; return item; }

Peek

/** * Gets (peeks) the top element but does not delete it in O(1). * @return top, item on the top * @throws StackException indicating the stack is empty */ @SuppressWarnings("unchecked") @Override public AnyType peek() { if (isEmpty()) { throw new StackException("Stack is empty"); } return (AnyType) elements[top]; }

Stack Implementation using LinkedList

Treating the first element of the linked list as the top of the stack

LinkedList<Integer> theStack = new LinkedList<Integer>(); // Push onto the stack theStack.addFirst(0); // Pop from the stack theStack.removeFirst();

Also, addLast()/removeLast() methods can be used to implement a stack that treats the last element of the linked list as the top of the stack.

Stack Usage: Reverse a String

/** * Simple Application to reverse a String using Stack. */ public class Reverser { /** * Input string. */ private String input; /** * Constructor with a specific input string. * @param str string to reverse */ public Reverser(String str) { input = str; } /** * Performs reversal operation and returns a new string. * @return reversed string */ public String doReverse() { ArrayStack<Character> stack = new ArrayStack<Character>(input.length()); for (int i = 0; i < input.length(); i++) { stack.push(input.charAt(i)); } StringBuilder sb = new StringBuilder(); while (!stack.isEmpty()) { sb.append(stack.pop()); } return sb.toString(); } }

Back to Home Next Lecture