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

Here you will get program for array representation of stack in C.

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 stack it grow at one end. Similarly, when elements are deleted from a stack, it shrinks at the same end.

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 1-D array.

Program for Stack in C

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!!

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

Category: DSA

3 thoughts on “Program for Stack in C [Push, Pop and Display]

  1. Avijeet Paul

    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();
    }

    Reply
  2. Waqar

    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();
    }
    }

    Reply

Leave a Reply

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