Source Code for Stack using array in C++.


Hi, I am Aman Kumar pursuing BCA II. I have decided to write what i learn daily.

I request you to please go through this program and feel free to write me if you have any advice/suggestions for me 

at 2022amankumar@gmail.com 

Source code of Stack using array in C++ :)


#include <iostream>
using namespace std;
#define max 1000
class Stack
{
    int Top;
public:
    int a[max];
    Stack(){Top=-1;}
    bool push(int x);
    int pop();
    int peek();
    bool isEmpty();
};
bool Stack::push(int x)
{
    if(Top>=max-1)
    {
        cout<<"Stack Overflow";
        return false;
    }
    else{
        a[++Top]=x;
        cout<<x<<" pushed into stack\n";
        return true;
    }
}
int Stack::pop()
{
    if(Top<0)
    {
        cout<<"Stack Underflow";
        return 0;
    }
    else
    {
        int x=a[Top--];
        return x;
    }
}
int Stack::peek()
{
    if(Top<0)
    {
        cout<<"Stack underflow";
        return 0;
    }
    else
    {
        int x=a[Top];
        return x;
    }
}
bool Stack::isEmpty()
{
    return(Top<0);
}
int main() {
Stack s;
s.push(20);
s.push(10);
s.pop();
s.peek();
s.isEmpty();
return 0;
}

Output:



Comments

  1. Appreciated by your step...
    But write also a program on a python ...
    Or write some algorithm..so that person who is studying different language can understand.
    Btw nice initiative ������

    ReplyDelete
  2. Appreciated by your step...
    But write also a program on a python ...
    Or write some algorithm..so that person who is studying different language can understand.
    Btw nice initiative ������

    ReplyDelete

Post a Comment