next up previous contents
Next: Queue (FIFO) Up: Data Structures in UML Previous: Data Structures in UML   Contents

Stack (LIFO) [26]



Static Structure Diagram:


\begin{picture}(225,230)(35,590)\thicklines
\put( 35,590){\framebox (225,230)...
...0){\makebox(0,0)[lb]{\raisebox{0pt}[0pt][0pt]{\twlrm +pop():int}}}
\end{picture}



Collaboration Diagram:


\begin{picture}(560,195)(5,625)\thicklines
\put( 55,760){\framebox (105,60){}...
...[lb]{\raisebox{0pt}[0pt][0pt]{\twlrm 5a*: [!isEmpty()] i:=pop()}}}
\end{picture}

$\bullet$ Stack is fixed size array, which only holds integers

$\bullet$ Stack maintains Last-In First-Out (LIFO) by incrementing/decrementing top



Stack: Java [27]

class Stack {

  public static void main(String args[]) {
    Stack s = new Stack();
    s.push(5); 
    s.push(1); 
    s.push(7); 
    while (!s.isEmpty()) 
      System.out.println(s.pop());
  }

  private static final int STACK_SIZE  = 100;
  private static final int EMPTY_TOS   = -1;
  private int top = EMPTY_TOS;
  int stack_array[] = new int[STACK_SIZE];

  public Stack () { }

  public boolean isEmpty() {
    return(top == EMPTY_TOS);
  }

  public boolean isFull() { 
    return(top == STACK_SIZE-1);
  }

  public void push(int i) {
    if (!isFull()) 
      stack_array[++top]= i;
  }

  public int pop() {
    if (isEmpty()) 
      return 0;
    else
      return(stack_array[top--]);
  }
}



Ted Billard 2006-09-26