Write a Menu Driven Program to implement stack operations in C

By | 03.12.2016

C Program to implement stack operations


Write a C Program to implement stack operations. Here’s a Simple Program to implement stack operations like push, pop, display in C Programming Language.


  • This C Program implements stack. Stack is an area of memory that holds all local variables and parameters used by any function, and remembers the order in which functions are called so that function returns occur correctly.
  • Each time a function is called, its local variables and parameters are “”pushed onto”” the stack. When the function returns, these locals and parameters are “”popped.””
  • Because of this, the size of a program’s stack fluctuates constantly as the program is running, but it has some maximum size. This program has to perform push and pop operation of stack.

Below is the source code for C Program to implement stack operations which is successfully compiled and run on Windows System to produce desired output as shown below :


SOURCE CODE : :


/*
 * C program to implement stack operations.
 * Stack operations: PUSH(insert operation), POP(Delete operation)
 * and Display stack.
 */

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
#define max 20

int top=-1,s[max];
void push(int n)
{
    if(top==max-1)
    {
        puts("stack is over flown");
        return;
    }
    else
    {
        top=top+1;
        s[top]=n;
    }
}
void pop()
{
    int del;
    if(top==-1)
    {
        puts("stack is underflown");
        return;
    }
    else
    {
        del=s[top];
        printf("\n poped element is %d",del);
        top=top-1;
    }
}
void display()
{
    int i;
    if(top==-1)
        puts("stack is empty");
    else
    {
        for(i=top;i>=0;i--)
            printf("\t%d",s[i]);
    }
}
int main()
{
    int opt,n;
    do
    {
        printf("\n 1.Push");
        printf("\n 2.Pop");
        printf("\n 3.Display");
        printf("\n 4.Exit ");
        printf("\n\nEnter your choice :: ");
        scanf("%d",&opt);
        switch(opt)
        {
        case 1:
            printf("\n Enter any element to push :: ");
            scanf("%d",&n);
            push(n);
            break;
        case 2:
            pop();
            break;
        case 3:
            display();
            break;
        case 4:
            exit(0);
            break;
        }
    }
    while(1);

    return 0;
}

OUTPUT : :


1.Push
 2.Pop
 3.Display
 4.Exit

Enter your choice :: 1

 Enter any element to push :: 4

 1.Push
 2.Pop
 3.Display
 4.Exit

Enter your choice :: 1

 Enter any element to push :: 3

 1.Push
 2.Pop
 3.Display
 4.Exit

Enter your choice :: 1

 Enter any element to push :: 8

 1.Push
 2.Pop
 3.Display
 4.Exit

Enter your choice :: 1

 Enter any element to push :: 0

 1.Push
 2.Pop
 3.Display
 4.Exit

Enter your choice :: 1

 Enter any element to push :: 7

 1.Push
 2.Pop
 3.Display
 4.Exit

Enter your choice :: 3
        7       0       8       3       4
 1.Push
 2.Pop
 3.Display
 4.Exit

Enter your choice :: 2

 poped element is 7
 1.Push
 2.Pop
 3.Display
 4.Exit

Enter your choice :: 2

 poped element is 0
 1.Push
 2.Pop
 3.Display
 4.Exit

Enter your choice :: 2

 poped element is 8
 1.Push
 2.Pop
 3.Display
 4.Exit

Enter your choice :: 2

 poped element is 3
 1.Push
 2.Pop
 3.Display
 4.Exit

Enter your choice :: 2

 poped element is 4
 1.Push
 2.Pop
 3.Display
 4.Exit

Enter your choice :: 2
stack is underflown

 1.Push
 2.Pop
 3.Display
 4.Exit

Enter your choice :: 3
stack is empty

 1.Push
 2.Pop
 3.Display
 4.Exit

Enter your choice :: 4

Process returned 0

If you found any error or any queries related to the above program or any questions or reviews , you wanna to ask from us ,you may Contact Us through our contact Page or you can also comment below in the comment section.We will try our best to reach upto you in the short interval.


Thanks for reading the post.

4.5 11 votes
Article Rating
Category: Advance Programs C Programming Tags:

About Tunde A

My name is Tunde Ajetomobi, a Tech Enthusiast and Growth Hacker. I enjoy creating helpful content that solves problem across different topics. Codezclub is my way of helping young aspiring programmers and students to hone their skills and find solutions on fundamental programming languages.

Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments