Stack Push Pop Program In C Using Arrays With Example

Stack push pop program in c using arrays with an example. If you are looking for a stack push pop program in c, this C programming tutorial will help you to learn how to code stack program in c using arrays. Just go through this C programming example, you will be able to write a C program to push and pop.

Stack Push Pop Program in C

Table of Contents

Stack Push Pop Program in C using Arrays

Learn how to write a  stack program in C using arrays. Writing a stack push pop program in C can be done using various techniques but here in this program, we show how to write a C program to push and pop using arrays in a proper way.

C Programming Tutorials

  1. Factorial Program in C Using Pointers
  2. Factorial Program in C Using While Loop
  3. C Program For Factorial Using For Loop
  4. Factorial Program in C Using Recursion Function
  5. C Program For Palindrome String
  6. C Program For Palindrome Numbers
  7. C Program To Reverse a String with Using Function
  8. C Program To Reverse a String without Using Function
  9. C Program To Reverse a String Using Recursion

Push Function Creation

/*STACK PUSH() AND POP() IMPLEMENTATION USING ARRAYS*/

#include <stdio.h>

#include<conio.h>

#define MAX 5

int top, status;

 

/*PUSH FUNCTION*/

void push (int stack[], int item)

{   if (top == (MAX-1))

status = 0;

    else

    {   status = 1;

++top;

stack [top] = item;

    }

}

Pop Function Creation

/*POP FUNCTION*/

int pop (int stack[])

{  

int ret;

    if (top == -1)

    {   ret = 0;

status = 0;

    }

    else

    {   status = 1;

ret = stack [top];

--top;

    }

return ret;

}

Function To Display Stack 

/*FUNCTION TO DISPLAY STACK*/

void display (int stack[])

{   int i;

    printf ("\nThe Stack is: ");

    if (top == -1)

printf ("empty");

    else

    {   for (i=top; i>=0; --i)

  printf ("\n--------\n|%3d   |\n--------",stack[i]);

    }

    printf ("\n");

}

Stack Push Pop Program in C Source Code

/*MAIN PROGRAM*/

void main()

{  

int stack [MAX], item;

    int ch;

    clrscr ();

    top = -1;

 

    do

    {  do

       {   printf ("\NMAIN MENU");

  printf ("\n1.PUSH (Insert) in the Stack");

  printf ("\n2.POP  (Delete) from the Stack");

  printf ("\n3.Exit (End the Execution)");

  printf ("\nEnter Your Choice: ");

  scanf  ("%d", &ch);

  if (ch<1 || ch>3)

      printf ("\nInvalid Choice, Please try again");

}while (ch<1 || ch>3);

       switch (ch)

       {case 1:

printf ("\nEnter the Element to be pushed : ");

scanf  ("%d", &item);

printf (" %d", item);

push (stack, item);

if (status)

{   printf ("\nAfter Pushing ");

   display (stack);

   if (top == (MAX-1))

printf ("\nThe Stack is Full");

}

else

   printf ("\nStack overflow on Push");

break;

       case 2:

item = pop (stack);

if (status)

{    printf ("\nThe Popped item is %d.  After Popping: ");

    display (stack);

}

else

    printf ("\nStack underflow on Pop");

break;

       default:

printf ("\nEND OF EXECUTION");

       }

    }while (ch != 3);

getch();

}

C PROGRAMMING EXAMPLES

Leave a Comment