Tuesday, 16 June 2015

IMPLEMENTATION OF STACK USING ARRAYS(GLOBAL VARIABLES)


#include<stdio.h>
int s[50],item,size;
int top=-1;
void push();
int pop();
void display();
main()
{
   int choice;
    printf("\nENTER THE SIZE OF THE STACK\n");
    scanf("%d",&size);
    int flag;
    do
    {
         printf("\nMENU\n1.PUSH\n2.POP\n3.EXIT\nEnter your choice : ");
         scanf("%d",&choice);
         switch(choice)
         {
              case 1:
                     push();
                     display();
                     break;
              case 2:
                    flag=pop();
                    
                    if(flag==-1)
                    {
                         printf("\n...THE STACK IS EMPTY...\n");
                    }
                    else
                     printf("\n...THE POPPED OUT ELEMENT..%d",flag);
                    printf("\n");
                    display();
                    break;
             case 3:
                    printf("\n...YOU HAVE EXITED..\n");
                    break;
         }  
    }while(choice!=3);
}
void push()
{
   if(top==size-1)
   {
       printf("\n THE STACK IS FULL \n");
       return;
   }
   printf("\n..ENTER THE ELEMENT YOU WANT TO PUSH..\n");
   scanf("%d",&item);
   s[++top]=item;
}
int pop()
{
   if(top==-1)
   {
       return -1;
   }
   printf("%d",top);
   return s[top--];    
}
void display()
{
   int i;
   for(i=top;i>=0;i--)
   {
       if(i==top)
       {
           printf("TOP->%d\n",s[i]);
       }
       else
       printf("     %d\n",s[i]);
   }
}



No comments:

Post a Comment