All pastes #904648 Raw Edit

k4st

public java v1 · immutable
·published 2008-02-15 03:24 UTC
rendered paste body
package QuestionEight;class EmptyStackException extends Exception {     EmptyStackException(String s) { super(s); }}class UnknownStackException extends Exception {    UnknownStackException(String s) { super(s); }}/** * Create a two-sided stack. One stack starts at 0, the other at length - 1, * and as they grow they approach eachother. Given the constraints of the * question, the FullStackException class was not required. * @author Peter Goodman */class RedBlueStack<E> {        protected int[] height = {0, 0};    protected int[] start = {0, 0};    protected E[] stack;        /* How we will address each stack, enum was overly complex. */    static public final int RED = 0;    static public final int BLUE = 0;        /** Constructor, set the pertinent info so we can work with 2 stacks. */    public RedBlueStack(int N) {        stack = (E[]) new Object[N];        start[BLUE] = N - 1;            }        /** Get the size of a given stack */    public int size(int s) throws UnknownStackException {               if(s < 0 || s > 1)            throw new UnknownStackException("Stack doesn't exist.");                return height[s];    }        /** Is one of the stacks empty? */    public boolean isEmpty(int s) throws UnknownStackException {        return (size(s) == 0);    }        /** Push an element onto one of the stacks */    public void push(int s, E elem) throws UnknownStackException {        int index = Math.abs(start[s] - height[s]);                stack[index] = elem;                height[s]++;    }        /** Pop an element off of one of the stacks */    public E pop(int s) throws EmptyStackException, UnknownStackException {                if(isEmpty(s))            throw new EmptyStackException("Stack is empty.");                int index = Math.abs(start[s] - height[s]) - 1;                E elem = stack[index];        stack[index] = null;                return elem;    }        /** Get the top element from one of the stacks. */    public E top(int s) throws EmptyStackException, UnknownStackException {                if(isEmpty(s))            throw new EmptyStackException("Stack is empty.");                return stack[Math.abs(start[s] - height[s]) - 1];    }        /* Stupid convenience functions */        public void pushRed(E elem) throws UnknownStackException { push(RED, elem); }    public void pushBlue(E elem) throws UnknownStackException { push(BLUE, elem); }        public E popRed() throws EmptyStackException, UnknownStackException { return pop(RED); }    public E popBlue() throws EmptyStackException, UnknownStackException { return pop(BLUE); }        public E topRed() throws EmptyStackException, UnknownStackException { return top(RED); }    public E topBlue() throws EmptyStackException, UnknownStackException { return top(BLUE); }        public int sizeRed() throws UnknownStackException { return size(RED); }    public int sizeBlue() throws UnknownStackException { return size(BLUE); }        public boolean isEmptyRed() throws UnknownStackException { return isEmpty(RED); }    public boolean isEmptyBlue() throws UnknownStackException { return isEmpty(BLUE); }}