← All examples

Array-based stack

Java Classes & methods

push / pop

Flowchart (ISO 5807)

StartInput xdata[top] = xtop++EndFigure 1 — Stack.pushStarttop--Return data[top]EndFigure 2 — Stack.pop

Source code

class Stack {
    int[] data = new int[100];
    int top = 0;

    void push(int x) {
        data[top] = x;
        top++;
    }

    int pop() {
        top--;
        return data[top];
    }
}