C Program to implement stack using array

/*************** BY Marxtudor 
www.marxtudor.com/ ***************/

#include
#define MAX 5

void push(int);
int pop();
void display();

int top = -1;
int stk[MAX];

main()
{
      int choice;
      int num;
      while(1)
      {
printf("nEnter your choicen");
printf("1. Pushn");
printf("2. Popn");  
printf("3. Displayn");
scanf("%d",&choice);
  switch(choice)
  {
               case 1:
                    scanf("%d",&num);
                    push(num);        /*function call*/
                    break;
               case 2:
                    pop();            /*function call*/
                    break;
               case 3:
                    display();        /*function call*/
                    break;
               default:
                    printf("nInvalid Inputn");
                    
       }   /*end of switch*/
  }        /*end of while*/
}          /*end of main*/


void push(int element)
{
     if(top==MAX-1)
     {
               printf("nOverflown");
               return;
     }

     top = top+1;
     stk[top]=element;
}


int pop()
{
    int element;
    if(top==-1)
    {
               printf("nUnderflown");
               return;
    }
    
    element = stk[top];
    top = top-1;
    printf("%d  deleted ", element);
    return element;
}


void display()
{
          if(top==-1)
     {
               printf("nUnderflown");
               return;
     }
     
     int i;
     printf("nn");
     for(i=top; i>=0; i--)
     printf("%d n", stk[i]);
}



Previous Post
Stack using array Program blueprint
Next Post
Queue using array

Related Posts

1 Comment. Leave new

Leave a Reply

Your email address will not be published.

Fill out this field
Fill out this field
Please enter a valid email address.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Menu