Static Structure Diagram:
Collaboration Diagram:
Stack is fixed size array, which only holds integers
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--]);
}
}