Implement Stack using linked list |Data Structure in C|

Introduction

     In the realm of data structures, stacks play a pivotal role in managing data with a Last-In-First-Out (LIFO) approach. This blog post will unravel a C program that dynamically implements a stack, allowing users to interactively PUSH elements onto the stack and observe the POP order. We'll explore the intricacies of dynamic memory allocation and linked structures.  


Code

    
     
    #include<stdio.h>
    #include<conio.h>
    
    void main() {
        struct node {
            int data;
            struct node *next;
        } *top = NULL;
    
        struct node *newnode;
        char ch;
        clrscr();
    
        do {
            newnode = (struct node*)malloc(sizeof(struct node));
            printf("Enter element to PUSH: ");
            scanf("%d", &newnode->data);
    
            if (top == NULL) {
                top = newnode;
                top->next = NULL;
            } else {
                newnode->next = top;
                top = newnode;
            }
    
            printf("PUSH more (y/n): ");
            ch = getch();
        } while ((ch == 'y') || (ch == 'Y'));
    
        printf("\nThe POP order is");
        printf("\n(TOP)");
    
        for (; top != NULL; top = top->next)
            printf("\n %d", top->data);
    
        printf("\n(BOTTOM)");
        getch();
    }
       
     


Output


        Enter element to PUSH: 5
        PUSH more (y/n): y
        Enter element to PUSH: 8
        PUSH more (y/n): y
        Enter element to PUSH: 3
        PUSH more (y/n): n
        
        The POP order is
        (TOP)
        3
        8
        5
        (BOTTOM)   
       



Explanation

      1. Header Files:
The program includes the standard input/output header <stdio.h> and the console I/O header <conio.h>.
2. Structure Definition:
The node structure is defined to represent each element in the stack. It contains an integer data and a pointer next to the next node.
3. Main Function:
The main function serves as the program's entry point.
It dynamically allocates memory for a new node and allows users to input elements to be PUSHed onto the stack.
4. Dynamic Memory Allocation:
malloc is used to dynamically allocate memory for each new node.
5. Linked List Implementation:
If the stack is empty (top == NULL), the new node becomes the top.
Otherwise, the new node is linked to the current top, and the top is updated.
6. User Interaction:
Users can continue to PUSH elements onto the stack until they choose to stop.
7. Display POP Order:
The program displays the POP order by traversing the stack from top to bottom.


Conclusion

     This C program provides a dynamic implementation of a stack using linked structures. Users can interactively PUSH elements onto the stack and observe the POP order, gaining insights into dynamic memory allocation and linked list operations. Feel free to run the program, PUSH different elements onto the stack, and observe the resulting POP order. This example serves as an engaging introduction to dynamic stack implementation in programming. Happy coding!

Post a Comment

0 Comments