Sunday, May 9, 2021

Stack using Linked List in C Program

 We will see how to create stack using linked list in C program. Stack is a special kind of linear list. Linear list is an ordered collection of a number of items of the same type. Two operations are performed frequently from linear list – insertion and deletion.

Stack using Linked List in C Program


A stack is a linear list where all insertions and deletions happen at one end of the list. A stack is often called Last In First Out(LIFO) because a first element removed is the last element pushed to the stack.


Here we will see the operations on stack using linked list because the stack is never full as long as the system has enough space for dynamic memory allocation.


Stack using C Program

A stack can be defined by the following structure using pointer in C language:

typedef struct element {
    int item;
    struct element *next;
} element;

typedef struct stack {
    element *top;
} stack;


If stack variable is s then s->top points to the top of the stack s.


Push and pop operations on such a stack will involve manipulation of this top pointer.


The complete source code for Stack using Linked List is given below.


We have given you several options, such as, pushing an element, popping an element, displaying the current elements and exit when you don’t want to perform any operation on stack.

/* 
 * File:   Stack.c
 */

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

typedef struct element {
    int item;
    struct element *next;
} element;

typedef struct stack {
    element *top;
} stack;

void push(int item, stack *s) {
    element *e;
    e = (element*) malloc(sizeof (element));
    e->item = item;
    e->next = s->top;
    s->top = e;
}

int pop(stack *s) {
    int *item;
    element *e;
    if (s->top == NULL) {
        printf("\nStack is empty\n");
        item = NULL;
        return 0;
    }
    *item = s->top->item;
    e = s->top;
    s->top = s->top->next;
    free(item);
    return 1;
}

void display(stack *s) {
    if (s->top == NULL) {
        printf("\nStack is Empty\n");
    } else {
        while (s->top->next != NULL) {
            printf("%d->", s->top->item);
            s->top = s->top -> next;
        }
        printf("%d->NULL", s->top->item);
    }
}

int main() {
    int choice, value;
    stack *s = (stack*) malloc(sizeof (stack));
    printf("\n:: Stack using Linked List ::\n");
    while (1) {
        printf("\nChoose from below Menu\n");
        printf("1. Push\n2. Pop\n3. Display\n4. Exit\n");
        printf("Enter your choice: ");
        scanf("%d", &choice);
        switch (choice) {
            case 1: printf("Enter the value to be inserted: ");
                scanf("%d", &value);
                push(value, s);
                break;
            case 2: pop(s);
                break;
            case 3: display(s);
                break;
            case 4: exit(0);
            default: printf("\nWrong selection!!! Please try again!!!\n");
        }
    }
}


Testing the Program

When you run the above code you will see the below output:

:: Stack using Linked List ::

Choose from below Menu
1. Push
2. Pop
3. Display
4. Exit
Enter your choice: 1
Enter the value to be inserted: 5

Choose from below Menu
1. Push
2. Pop
3. Display
4. Exit
Enter your choice: 1
Enter the value to be inserted: 4

Choose from below Menu
1. Push
2. Pop
3. Display
4. Exit
Enter your choice: 3
4->5->NULL
Choose from below Menu
1. Push
2. Pop
3. Display
4. Exit
Enter your choice: 4



No comments:

Post a Comment