DEV Community

Cover image for Data Structure in Java: Stack
luthfisauqi17
luthfisauqi17

Posted on • Updated on

Data Structure in Java: Stack

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:

Top comments (0)