Write a program to implement stack using array in C++.
In this we are going to see  a program to  implement stack using array in C++ Programming Language.



The Code given below can be used in TURBO C++ Compilers: -

#include <iostream.h>
#include <conio.h>
#include <stdlib.h>
#define max 5

int stack[max], top = -1;
void push(int stack[], int val);
int pop(int stack[]);
void display(int stack[]);

void main()
{
    clrscr();
    int choice, n;
    cout << "MENU:\n1. Push Element\n2. Pop Element\n3. Display\n4. Exit\n";
    do
    {
        cout << "\nEnter your choice: ";
        cin >> choice;
        switch (choice)
        {
        case 1:
        {
            cout << "Enter a value: ";
            cin >> n;
            push(stack, n);
            break;
        }
        case 2:
        {
            cout << pop(stack);
            break;
        }
        case 3:
        {
            display(stack);
            break;
        }
        case 4:
        {
            exit(1);
            break;
        }
        default:
        {
            cout << "Invalid Choice!" << endl;
        }
        }
    } while (choice != 4);
    getch();
}

void push(int stack[], int val)
{
    if (top == max - 1)
    {
        cout << "Stack Overflow" << endl;
    }
    else
    {
        top++;
        stack[top] = val;
    }
}

int pop(int stack[])
{
    int val;
    if (top <= -1)
    {
        cout << "Stack Empty" << endl;
        return -1;
    }
    else
    {
        val = stack[top];
        top--;
        return val;
    }
}
void display(int stack[])
{

    if (top >= 0)
    {
        cout << "Stack elements are:";
        for (int i = top; i >= 0; i--)
        {
            cout << stack[i] << " ";
        }
        cout << endl;
    }
    else
    {
        cout << "Stack is empty";
    }
}


The Code given below can be used in gcc/g++ Compilers: -

#include <iostream>
#define max 5
using namespace std;

int stack[max], top = -1;
void push(int stack[], int val);
int pop(int stack[]);
void display(int stack[]);

int main()
{
    int choice, n;
    cout  << "MENU:\n1. Push Element\n2. Pop Element\n3. Display\n4. Exit\n";
    do
    {
        cout << "\nEnter your choice: ";
        cin >> choice;
        switch (choice)
        {
        case 1:
        {
            cout << "Enter a value: ";
            cin >> n;
            push(stack, n);
            break;
        }
        case 2:
        {
            cout << pop(stack);
            break;
        }
        case 3:
        {
            display(stack);
            break;
        }
        case 4:
        {
            exit(0);
            break;
        }
        default:
        {
            cout << "Invalid Choice!" << endl;
        }
        }
    } while (choice != 4);
    return 0;
}

void push(int stack[], int val)
{
    if (top == max - 1)
    {
        cout << "Stack Overflow" << endl;
    }
    else
    {
        top++;
        stack[top] = val;
    }
}

int pop(int stack[])
{
    int val;
    if (top <= -1)
    {
        cout << "Stack Empty" << endl;
        return -1;
    }
    else
    {
        val = stack[top];
        top--;
        return val;
    }
}
void display(int stack[])
{

    if (top >= 0)
    {
        cout << "Stack elements are:";
        for (int i = top; i >= 0; i--)
        {
            cout << stack[i] << " ";
        }
        cout << endl;
    }
    else
    {
        cout << "Stack is empty";
    }
}



#ENJOY CODING

Post a Comment

FOR ANY DOUBTS AND ERRORS FEEL FREE TO ASK. YOUR DOUBTS WILL BE ADDRESSED ASAP

Previous Post Next Post