Q:

Write a Menu Driven Program to implement stack operations in C

0

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.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

  • 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

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Write a C Program to perform Binary Search... >>
<< Write a Menu Driven C Program for Student Details ...