Monday, September 5, 2016

PROGRAM TO PERFORM PUSH AND POP OPERATIONS ON STACK:

PROGRAM TO PERFORM PUSH AND POP OPERATIONS ON STACK IMPLEMENTED AS AN ARRAY.

#include <iostream>
using namespace std;
void push(int stack[],int max,int i,int top);
void pop(int stack[],int max,int i,int top);
int main()
{
    int choice,stack[30],max,i;
start:
    int top=-1;
    cout<<"\n\n***Operations on STACK***\n\n";
    cout<<"Enter Size of Stack: ";
    cin>>max;
    cout<<"\nEnter "<<max<<" Elements of Stack: ";

    for(i=0; i<max; i++)
    {
        cin>>stack[i];
        top++;
    }
    cout<<"\n1: Push\n2: Pop\n";
    cout<<"Enter your Choice(Press F6 to Exit): ";

    while(cin>>choice)
    {

        switch(choice)
        {
        case 1:
            push(stack,max,i,top);
            cout<<"\nEnd of Program";
            break;
        case 2:
            pop(stack,max,i,top);
            cout<<"\nEnd of Program";
            break;
        default:
            cout<<"\nInvalid Input";
        }
        goto start;
    }
    return 0;
}

void push(int stack[],int max,int i,int top)
{
    int item;
    if(top==29)
        cout<<"\nStack Overflow!!";
    else
    {
        cout<<"\nEnter Element to be Inserted(Pushed): ";
        cin>>item;
        top++;
        stack[top]=item;
        cout<<"\nNew Stack is:\n";
        for(i=0; i<=top; i++)
            cout<<stack[i]<<" ";
    }
}


void pop(int stack[],int max,int i,int top)
{
    if(top==-1)
        cout<<"\nStack UnderFlow!!";
    else
    {
        top--;
        cout<<"\nNew Stack is:\n";
        for(i=0; i<=top; i++)
            cout<<stack[i]<<" ";
    }
}