DEV Community

Cover image for Data Structure in Java: Stack
luthfisauqi17
luthfisauqi17

Posted on • Edited on

4 2

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)

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

Rather than just generating snippets, our agents understand your entire project context, can make decisions, use tools, and carry out tasks autonomously.

Read full post

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay