Saturday, May 29, 2010

Stack - PUSH Operation

#include< stdio.h >
#include< conio.h >
//global declaration
int a[20],n,top,x;

void main()
{ clrscr();
printf("Enter Total Number of Elements = ");
scanf("%d",&n);
//initialise TOP
top=0;

//insert element
while(top < n)
{       //check for Overflow
    if(top > n)
    {
        printf("\nStack Full");
        exit();
    }
    else
    {
        //enter element
        printf("\nEnter Element a[%d] = ",top);
        scanf("%d",&x);
        a[top]=x;
        top=top+1;
    }
}
top=top-1;
//Traverse Stack
printf("\n***** Elements of Stack Are ***** \n");
while(top >= 0)
{
    printf("_____\n");
    printf("| %d |\n",a[top]);
    top=top-1;
}
printf("_____\n");
getch();
}

No comments:

Post a Comment