Program for Stack in C [Push, Pop, Display]

Here you will get the program for stack implementation using array in C language.

What is Stack?

Stack is a LIFO (last in first out) structure. It is an ordered list of the same type of elements. A stack is a linear list where all insertions and deletions are permitted only at one end of the list. When elements are added to the stack it grows at one end. Similarly, when elements are deleted from a stack, it shrinks at the same end.

There are two main operations in the stack push and poop.

  • Push: Insert an element in the stack.
  • Pop: Removes the most recently inserted element from the stack.
Stack in C

Also Read: Applications of Stack

Below I have written a C program that performs push, pop, and display operations on a stack. It is implemented using one-dimensional array.

Stack Implementation Using Array in C

#include<stdio.h>
#include<process.h>
#include<stdlib.h>

#define MAX 5	//Maximum number of elements that can be stored

int top=-1,stack[MAX];
void push();
void pop();
void display();

void main()
{
	int ch;
	
	while(1)	//infinite loop, will end when choice will be 4
	{
		printf("\n*** Stack Menu ***");
		printf("\n\n1.Push\n2.Pop\n3.Display\n4.Exit");
		printf("\n\nEnter your choice(1-4):");
		scanf("%d",&ch);
		
		switch(ch)
		{
			case 1: push();
					break;
			case 2: pop();
					break;
			case 3: display();
					break;
			case 4: exit(0);
			
			default: printf("\nWrong Choice!!");
		}
	}
}

void push()
{
	int val;
	
	if(top==MAX-1)
	{
		printf("\nStack is full!!");
	}
	else
	{
		printf("\nEnter element to push:");
		scanf("%d",&val);
		top=top+1;
		stack[top]=val;
	}
}

void pop()
{
	if(top==-1)
	{
		printf("\nStack is empty!!");
	}
	else
	{
		printf("\nDeleted element is %d",stack[top]);
		top=top-1;
	}
}

void display()
{
	int i;
	
	if(top==-1)
	{
		printf("\nStack is empty!!");
	}
	else
	{
		printf("\nStack is...\n");
		for(i=top;i>=0;--i)
			printf("%d\n",stack[i]);
	}
}

Output:

*** Stack Menu ***

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

Enter your choice(1-4):1

Enter element to push:3

*** Stack Menu ***

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

Enter your choice(1-4):1

Enter element to push:6

*** Stack Menu ***

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

Enter your choice(1-4):3

Stack is…
6
3

*** Stack Menu ***

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

Enter your choice(1-4):2

Deleted element is 6
*** Stack Menu ***

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

Enter your choice(1-4):3

Stack is…
3

*** Stack Menu ***

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

Enter your choice(1-4):2

Deleted element is 3
*** Stack Menu ***

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

Enter your choice(1-4):2

Stack is empty!!

Video Tutorial

Comment below if you have doubts or found anything incorrect in the above program for stack in C.

35 thoughts on “Program for Stack in C [Push, Pop, Display]”

    1. I have question sir
      Write a program in c++ to perform stack function insert 5 number from the user and delete one no. and display output using pop and push function too and same with another no. Is delete and it output

  1. extern int avijeet(const char *format, …);
    #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*/
    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*/
    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”);
    }
    /*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 (ch3)
    printf (“\nInvalid Choice, Please try again”);
    }
    while (ch3);
    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();
    }

  2. Hiii sir
    Sir my progarm is POP & PUSH THIS PROGRAM NOT RUN PLZ SOWH ERRORE
    #include

    #define MAX 5
    main()
    {
    int top, status;

    void push (int stack[], int item)
    { if (top == (MAX-1))
    status = 0;
    else
    { status = 1;
    ++top;
    stack [top] = item;
    }
    }

    int pop (int stack[])
    {
    int ret;
    if (top == -1)
    { ret = 0;
    status = 0;
    }
    else
    { status = 1;
    ret = stack [top];
    –top;
    }
    return ret;
    }
    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”);
    }

    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 (ch3)
    printf (“\nInvalid Choice, Please try again”);
    }while (ch3);
    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();
    }
    }

    1. because condition must be true so instead of takinf some other variables to make i t true and make it lengthy he have taken 1

    1. Here while is used because of just displaying your values. If u did’nt use while, how can u choose your choice multiple times. If you want single shot entry you can remove while. If you want to select multiple choices (Insert, Delete and Display ) multiple times you should include While(1) just for infinite entry purpose. Hope you understand.

  3. my requirement is int top=-1,stack[MAX]; there two global variable are placed inside the main function(local variables). plz write the code for that requirement .

  4. Shongedzia Mazonde

    PLEASE HELP. AM JUST STARTING PROGRAMMING.
    HOW DO YOU WRITE A RECURSIVE C PROGRAM CODE THJAT IMPLEMENTS A STACK AND CLEARLY SHOW THE FOLLOWING FUNCTIONS:
    a)main()
    b)push()
    c)pop()

  5. please tell me the error in headerfile and c file please early as possible as…

    #define max 5
    typedef struct{
    int data[max];
    int top;
    }stack;

    void init(stack *s)
    {
    s->top=-1;
    }

    void push(stack *s,int val)
    {
    if(s->top==max-1)
    {
    printf(“stack overflow”);
    }
    else
    {
    s->top++;
    s->data[s->top]=val;
    }
    }
    int pop(stack *s)
    {
    int val=NULL;
    if(s->top==-1)
    {
    printf(“stack underflow”);
    }
    else
    {
    val=s->data[s->top];
    s->top–;
    }
    return val;
    }

    int peep(stack *s)
    {
    int val=NULL;
    if(s->top==-1)
    {
    printf(“underflow”);
    }
    else
    {
    printf(“value is %d”,s->data[s->top]);
    }
    return val;

    }
    void display(stack *s)
    { int i,top;
    if(s->top==-1)
    {
    printf(“the stack is empty \n”);
    }
    else
    {
    for(i=s->top;i>=0;i–)
    {
    printf(“the value is %d”,s->data[i]);
    }
    }
    return 0;
    }
    void change(stack *s, int val)
    {
    if(s->top==-1)
    {
    printf(“stack underflow”);
    }
    else
    {
    s->data[s->top]=val;
    }
    }

    void isempty(stack *s)
    {
    if(s->top==-1)
    {
    printf(“stack is empty”);
    }
    else{
    printf(“stack is not empty”);

    }
    }
    void isfull(stack *s)
    {
    if(s->top==max-1)
    {
    printf(“stack is full”);
    }
    else
    {
    printf(“stack is not full”);
    }
    }

    now in c file

    #include
    #include
    #include
    #include
    void main()
    {
    int choice,val,value;
    stack s;
    init(&s);
    while(1)
    {
    printf(“\nenter your choice 1 for push 2 for pop 3 for peep 4 for display 5 for change 6 for isempty 7 for isfull 8 for exit:-“);
    scanf(“%d”,&choice);

    switch(choice)
    {
    case 1:
    printf(“enter value”);
    scanf(“%d”,&val);
    push(&s,val);
    break;
    case 2:
    val=pop(&s);
    if(val!=NULL)
    {
    printf(“the value is %d \n”,val);
    }
    break;
    case 3:
    peep(&s);
    break;
    case 4:
    display(&s);
    break;
    case 5:
    printf(“enter change value”);
    scanf(“%d”,&value);
    change(&s,value);
    break;
    case 6:
    isempty(&s);
    break;
    case 7:
    isfull(&s);
    break;
    case 8:exit(1);
    break;
    }
    }
    }

  6. Sandeep Krishna

    Help me to Write a C program to implement push and pop operation on stack and to display the contents of the stack.using the function definitions void push1 (struct twoStacks *p, int item)
    void push2 (struct twoStacks *p,int item)
    int pop1 (struct twoStacks *p)
    int pop2 (struct twoStacks *p)
    void display1 (struct twoStacks p)
    void display2 (struct twoStacks p)
    void initstack (struct twoStacks *p, int n) and have to obtain the output as Enter your choice
    5
    The contents of the stack1 are {}

    Choice 1 : Push1

    Choice 2 : Push2
    Choice 3 : Pop1
    Choice 4 : Pop2
    Choice 5 : Display1
    Choice 6 : Display2
    Any other choice : Exit

  7. Hi sir,,
    Write a program that implement push pop and display change using stack in c++..
    Sir plz solve this problem

  8. Make a class stack with a list as an attribute and three method Push(), PoP() and Display() to perform the relevant operations in addition to the __init__() method. The class should be able to handle the exception if data is tried to be popped from an empty stack

    plz solve this

Leave a Comment

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