C Program to push and pop elements using Stack

C programming code for Stack. This  program will push and pop elements from stack.

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

int stk[100], top=-1, n;

void show()         
{
     for(int i=0;i<=top;i++)
     printf(“%d\t”,stk[i]);
     printf(“\n”);
 }
 
void push()
{
     int item;
  
     if(top == n-1)
            printf(“\nStack is full.\n”);
     else
     {
            printf(“\nEnter the item : “);
            scanf(“%d”,&item);
            stk[++top]=item;
     }
     show();
}
     
void pop()
{
         
         if(top==-1)
             printf(“Stack is empty.”);
         else
         {
             
             printf(“%d is popped.”,stk[top]);
             top–;
         }
}
         
int main()
{
          int i,op;
          printf(“Enter the size of the stack : “);
          scanf(“%d”,&n);                    
          do
          {
             printf(“\n1 : Push”);
             printf(“\n2 : Pop”);
             printf(“\n3 : Print”);
             printf(“\n4 : Exit”);
             printf(“\nEnter your choice : “);
             scanf(“%d”,&op);
          
             switch(op)
             {
                    case 1:
                              push();
                              break;
                              
                    case 2:
                              pop();
                              break;
                              
                    case 3:
                              show();
                              break;

              }
        }while(op!=4);
        
        printf(“\n—THE END—“);
                           
        getch();   
}

OUTPUT

You might be also interested in :

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s