Untitled

Anonymous
plain_text
10/17/2026 9:11 PM
2.1 KB
6
Indexable
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define PI 3.14
using namespace std;

const int n=5;

class stack_array
{
private:
    int top;
    int st[n];
public:
    stack_array(){
        top=-1;
    }

    bool isEmpty()
    {
        return top==-1;
    }

    bool isFull()
    {
        return top==n-1;
    }

    void push(int value)
    {
        if(isFull()){
            cout<<"OverFlow\n";
            return;
        }
        top++;
        st[top]=value;
        cout<<value<<" Element inserted successfully\n";
    }

    void pop()
    {
        if(isEmpty()){
            cout<<"UnderFlow\n";
            return;
        }
        top--;
        cout<<"Element removed successfully\n";
    }

    int peek()
    {
        if(isEmpty()){
            cout<<"Stack is Empty\n";
            return -1;
        }

        return st[top];
    }

    void display()
    {
        if(isEmpty()){
            cout<<"Stack is Empty\n";
            return;
        }
        cout<<"Stack Elements:\n";
        for(int i=top;i>=0;i--){
            cout<<st[i]<<endl;
        }
    }

};



void testCase()
{
    stack_array st;
    st.push(1);
    st.push(2);
    st.push(3);
    st.push(4);
    st.push(5);
    st.display();
    st.pop();
    st.display();
    cout<<"Last Element: "<<st.peek()<<endl;
}



int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
 
    int t=1;
    //cin>>t;
 
    while(t--){
        testCase();
    }

    
    return 0;
}

Editor is loading...
Leave a Comment