Social Icons

Thursday, 27 February 2014

Program of Stack [Push, Pop and Display]

#include<stdio.h>
#include<conio.h>
#include<process.h>

#define MAX 5
int top=-1,stack[MAX];


void push()
{
    int val;
    if(top==MAX-1)
    {
    printf("\n Stack is full...");
    getch();
    }
    else
    {
    printf("\n Enter element to push : ");
    scanf("%d",&val);
    top=top+1;
    stack[top]=val;
    printf("\n Press any key...");
    getch();
    }
}


void pop()
{
    if(top==-1)
    {
    printf("\n Stack is empty...");
    getch();
    }
    else
    {
    printf("\n Deleted element is : %d",stack[top]);
    printf("\ Press any key...");
    top=top-1;
    getch();
    }
}


void display()
{
    int i;
    if(top==-1)
    {
    printf("\n Stack is empty...");
    getch();
    }
    else
    {
    printf("\n Stack elements are  ");
    for(i=top;i>=0;--i)
        printf(" \n %d ",stack[i]);

    printf("\n Press any key....");
    getch();
    }
}


int main()
{
    int ch;
    while(1)
    {
    clrscr();
    printf("\n\t\t***** Stack Menu *****");
    printf("\n Press 1 for Push");
    printf("\n Press 2 for Pop");
    printf("\n Press 3 for Display");
    printf("\n press 4 for Exit");
    printf("\n\n\t Enter your choice : ");
    scanf("%d",&ch);

    switch(ch)
    {
        case 1:
        push();
        break;

        case 2:
        pop();
        break;

        case 3:
        display();
        break;

        case 4:
        exit(0);

        default:
        printf("\n Wrong Choice...");
        getch();
    }
    }
}


                                                        Output















No comments:

Post a Comment