C Program for Array Representation of Stack [Push, Pop and Display]

By | December 16, 2013

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.
Below I have written a C program that performs push, pop and
display operations on a stack. It is implemented using 1-D array.
C Program for Array Representation of Stack [Push]

C Program for Array Representation of Stack [Delete]
  C Program for Array Representation of Stack [Display]
#include<stdio.h>
#include<conio.h>
#include<process.h>       //for exit()
#include<stdlib.h>         //for system(“cls”)
#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
    {
        system(“cls”);      //work in windows, for other OS change it
to system(“clear”)
        printf(“*** Stack Menu ***”);
       
printf(“nn1.Pushn2.Popn3.Displayn4.Exit”);
        printf(“nnEnter 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!!Press any key….”);
                     getch();
        }
    }
}
void push()
{
    int val;
    if(top==MAX-1)
    {
        printf(“nStack is full!!Press any
key….”);
        getch();
    }
    else
    {
        printf(“nEnter element to
push:”);
        scanf(“%d”,&val);
        top=top+1;
        stack[top]=val;
        printf(“nPress any
key….”);
        getch();
    }
}
void pop()
{
    if(top==-1)
    {
        printf(“nStack is empty!!Press
any key….”);
        getch();
    }
    else
    {
        printf(“nDeleted element is
%dnPress any key….”,stack[top]);
        top=top-1;
        getch();
    }
}
void display()
{
    int i;
    if(top==-1)
    {
        printf(“nStack is empty!!Press
any key….”);
        getch();
    }
    else
    {
        printf(“nStack is…n”);
        for(i=top;i>=0;–i)
            printf(“%dn”,stack[i]);
    printf(“nPress any key….”);
    getch();
    }
}

Subscribe To Get Latest Tutorials Directly In Your Inbox

 
badge

Category: DSA

2 thoughts on “C Program for Array Representation of Stack [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

Leave a Reply

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