Online-Academy
Look, Read, Understand, Apply

Data Structure

Stack

Stack

Stack is a data structure in which last element inserted is the first to come out, Last In First Out (LIFO).

Static Stack is implemented with array, and pointer (end) for inserting and removing element from the array. Stack has push, pop, display methods.

class stk{
    int data[];
    int top;
    public stk(){
        data = new int[10];
        top = -1;
    }
    public void push(int x){
        if(top<10){
            ++top;
            data[top] = x;
        }
    }
    public void pop(){
        if(top>-1){
            System.out.println("Popped: "+stk[top]);
            --top 
        }
    }
}