Data Structure in Java: Stack

luthfisauqi17 - Nov 10 '21 - - Dev Community
public class Stack {
    private int size;
    private int[] arr;
    private int top;

    public Stack(int size) {
        this.size = size;
        this.arr = new int[this.size];
        this.top = -1;
    }

    public void push(int i) {
        this.arr[++this.top] = i;
    }

    public int pop() {
        return this.arr[this.top--];
    }

    public int peek() {
        return this.arr[this.top];
    }

    public boolean isEmpty() {
        return (this.top == -1);
    }

    public boolean isFull() {
        return (this.top == this.size - 1);
    }
}
Enter fullscreen mode Exit fullscreen mode

Sources and Images:

. . . . . . . . . . . . . . . . . . . . . . . . . . . .