C Program to Implement a Stack

Stack

A stack is a basic data structure that can be logically thought as linear structure represented by a real physical stack or pile, a structure where insertion and deletion of items takes place at one end called top of the stack. The basic concept can be illustrated by thinking of your data set as a stack of plates or books where you can only take the top item off the stack in order to remove things from it. This structure is used all throughout programming.

The basic implementation of a stack is also called a LIFO (Last In First Out) to demonstrate the way it accesses data



Here is source code of the C Program to Implement Stack using an Array. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

#include<stdio.h>
#define MAX 5

int stack[MAX], top = -1;

int pop();
void push(int num);
void display();

void main()
{
    int choice, num;
    char ch = 'y';

    while (ch == 'y')
    {
        printf("\n------------------------------\n");
        printf("\tStack\n");
        printf("------------------------------\n");

        printf("1. Pop\n");
        printf("2. Push\n");
        printf("3. Display\n");
        printf("Enter your choice:\n");
        scanf("%d", &choice);

        switch(choice)
        {
            case 1:
                   pop();
                   break;
            case 2:
                    printf("\nEnter the element to be pushed: ");
                    scanf("%d", &num);
                    push(num);
                    break;
            case 3:
                    display();
                    break;
            default:
                    printf("\nInvalid choice!!!\n");
        }

        printf("\nDo you want to continue(y/n)?" );
        scanf(" %c", &ch);
    }
}

int pop()
{
    int num;
    if (top == -1)
    {
        printf("\nStack Underflow!!!\n");
        return 0;
    }
    else
    {
        num = stack[top--];
        printf("\nPopped element is %d\n", num);
    }
    return num;
}

void push(int num)
{
    if (top == MAX)
    {
        printf("\nStack Overflow!!!\n");
    }
    else
    {
        stack[++top] = num;
    }
}

void display()
{
    int i;
    if (top == -1)
    {
        printf("\nStack is empty!!!\n");
    }
    else
    {
        printf("Contents of stack are as follows\n");
        for (i=0; i<=top; i++)
        {
            printf("%d  ", stack[i]);
        }
    }
}
Output:

------------------------------
    Stack
------------------------------
1. Pop
2. Push
3. Display
Enter your choice:
1

Stack Underflow!!!

Do you want to continue(y/n)?y

------------------------------
    Stack
------------------------------
1. Pop
2. Push
3. Display
Enter your choice:
2

Enter the element to be pushed: 5

Do you want to continue(y/n)?y

------------------------------
    Stack
------------------------------
1. Pop
2. Push
3. Display
Enter your choice:
2

Enter the element to be pushed: 10

Do you want to continue(y/n)?y

------------------------------
    Stack
------------------------------
1. Pop
2. Push
3. Display
Enter your choice:
2

Enter the element to be pushed: 15

Do you want to continue(y/n)?y

------------------------------
    Stack
------------------------------
1. Pop
2. Push
3. Display
Enter your choice:
3
Contents of stack are as follows
5  10  15
Do you want to continue(y/n)?y

------------------------------
    Stack
------------------------------
1. Pop
2. Push
3. Display
Enter your choice:
1

Popped element is 15

Do you want to continue(y/n)?y

------------------------------
    Stack
------------------------------
1. Pop
2. Push
3. Display
Enter your choice:
3
Contents of stack are as follows
5  10
Do you want to continue(y/n)?

Share on Google Plus

About AlgoStream

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment