Simple Stack Program in C Programming Language

Have you just started learning C programming and wondering how to implement stack? Let’s have a look at the simple stack program in the C programming language to understand the concept in a clear way.

Simple Stack Program in C Programming Language

#include<stdio.h>
int stack[50],choice,n,top,x,i;
void push(void);
void pop(void);
void display(void);
int main()
{
    top=-1;
    printf("\n Please enter the size of STACK[MAX=50]:");
    scanf("%d",&n);
    printf("\n\t Select one option");
    printf("\n\t--------------------------------");
    printf("\n\t 1.PUSH\n\t 2.POP\n\t 3.DISPLAY\n\t 4.EXIT");
    do
    {
        printf("\n Enter the Choice:");
        scanf("%d",&choice);
        switch(choice)
        {
            case 1:
            {
                push();
                break;
            }
            case 2:
            {
                pop();
                break;
            }
            case 3:
            {
                display();
                break;
            }
            case 4:
            {
                printf("\n\t EXIT POINT ");
                break;
            }
            default:
            {
                printf ("\n\t Wrong Input, Enter 1, 2, 3, or 4");

            }
               
        }
    }
    while(choice!=4);
    return 0;
}
void push()
{
    if(top>=n-1)
    {
        printf("\n\tOverflow");
        
    }
    else
    {
        printf(" Please enter a value to push onto a stack:");
        scanf("%d",&x);
        top++;
        stack[top]=x;
    }
}
void pop()
{
    if(top<=-1)
    {
        printf("\n\t Underflow");
    }
    else
    {
        printf("\n\t The popped elements is %d",stack[top]);
        top--;
    }
}
void display()
{
    if(top>=0)
    {
        printf("\n The elements in STACK \n");
        for(i=top; i>=0; i--)
            printf("\n%d",stack[i]);
        printf("\n Press Next Choice");
    }
    else
    {
        printf("\n The STACK is empty");
    }  
}

Output:

 Please enter the size of STACK[MAX=50]:2                                                                                     
                                                                                                                              
         Select one option                                                                                                    
        --------------------------------                                                                                      
         1.PUSH                                                                                                               
         2.POP                                                                                                                
         3.DISPLAY                                                                                                            
         4.EXIT                                                                                                               
 Enter the Choice:1                                                                                                           
 Please enter a value to push onto a stack:2                                                                                  
                                                                                                                              
 Enter the Choice:2                                                                                                           
                                                                                                                              
         The popped elements is 2                                                                                             
 Enter the Choice:3                                                                                                           
                                                                                                                              
 The STACK is empty                                                                                                           
 Enter the Choice:1                                                                                                           
 Please enter a value to push onto a stack:1                                                                                  
                                                                                                                              
 Enter the Choice:4                                                                                                           
                                                                                                                              
         EXIT POINT                                                                                                     

Leave a Comment

Your email address will not be published. Required fields are marked *