C Program to Implement Stack Operations Using Linked List

C Program to Implement Stack Operations Using Linked List

      Another important program in data structure is here. previously we have posted stack operations using array. Today we are going to implement stack operations using Linked list. In this program there are total 8 Operations i.e Push, Pop, Display, Display Top, Empty, Destroy, Stack Count, and Exit. We have added comments explaining the working after every important statements. At the end you can see the output and video explaining the program.


Also read: Queue Using Array in C


Program:

#include <stdio.h>
#include <stdlib.h>
/*C Program for stack operations using linked list by SlashMyCode.com*/
struct node
{
    int info;
    struct node *ptr;
}*top,*top1,*temp;

int topelement();
void push(int data);
void pop();
void empty();
void display();
void destroy();
void stack_count();
void create();

int count = 0;

void main()
{
    int no, ch, e;

    printf("\n 1 - Push");
    printf("\n 2 - Pop");
    printf("\n 3 - Top");
    printf("\n 4 - Empty");
    printf("\n 5 - Exit");
    printf("\n 6 - Display");
    printf("\n 7 - Stack Count");
    printf("\n 8 - Destroy stack");

    create();

    while (1)
    {
        printf("\n Enter choice : ");
        scanf("%d", &ch);

        switch (ch)
        {
        case 1:
            printf("Enter data : ");
            scanf("%d", &no);
            push(no); //function call
            break;
        case 2:
            pop();
            break;
        case 3:
            if (top == NULL)//condition to check stack is empty or not
                printf("No elements in stack");
            else
            {
                e = topelement();
                printf("\n Top element : %d", e);
            }
            break;
        case 4:
            empty();
            break;
        case 5:
            exit(0);
        case 6:
            display();
            break;
        case 7:
            stack_count();
            break;
        case 8:
            destroy();
            break;
        default :
            printf(" Wrong choice, Please enter correct choice  ");
            break;
        }
    }
}
/*C Program for stack operations using linked list by SlashMyCode.com*/
/* Create empty stack */
void create()
{
    top = NULL;
}

/* Count stack elements */
void stack_count()
{
    printf("\n No. of elements in stack : %d", count);
}

/* Push data into stack */
void push(int data)
{
    if (top == NULL)
    {
        top =(struct node *)malloc(1*sizeof(struct node));
        top->ptr = NULL;
        top->info = data;
    }
    else
    {
        temp =(struct node *)malloc(1*sizeof(struct node));
        temp->ptr = top;
        temp->info = data;
        top = temp;
    }
    count++; //Every time when element is added count will be incremented by 1
}

/* Display stack elements */
void display()
{
    top1 = top;

    if (top1 == NULL)
    {
        printf("Stack is empty");
        return;
    }

    while (top1 != NULL)
    {
        printf("%d ", top1->info);
        top1 = top1->ptr;
    }
 }

/* Pop Operation on stack */
void pop()
{
    top1 = top;

    if (top1 == NULL)
    {
        printf("\n Error : Trying to pop from empty stack");
        return;
    }
    else
        top1 = top1->ptr;
    printf("\n Popped value : %d", top->info);
    free(top);
    top = top1;
    count--; //Every time when element is Removed count will be decremented by 1
}

/* Return top element */
int topelement()
{
    return(top->info);//top most element in stack will be returned 
}

/* Check if stack is empty or not */
void empty()
{
    if (top == NULL)
        printf("\n Stack is empty");
    else
        printf("\n Stack is not empty with %d elements", count);
}

/* Destroy entire stack */
void destroy()
{
    top1 = top;

    while (top1 != NULL)
    {
        top1 = top->ptr;
        free(top);
        top = top1;
        top1 = top1->ptr;
    }
    free(top1);
    top = NULL;

    printf("\n All stack elements destroyed");
    count = 0; //Since there is no element in stack
/*C Program for stack operations using linked list by SlashMyCode.com*/

}



Video Explaining Stack using Linked List by Marxtudor Y.T


Output:


Here is a output of above program where we have performed all 8 stack operations 

Output of Stack Operations Using Linked List





Please do share if it was helpful to you :)

No comments:

Post a Comment

Feel free to ask us any question regarding this post