Implement Queue using linked list |Data Structure in C|

Introduction

     Queues, a fundamental data structure, follow the First-In-First-Out (FIFO) principle. In this blog post, we explore a C program that dynamically implements a queue using linked structures. Through the use of pointers and memory allocation, we'll uncover the intricacies of enqueue and dequeue operations, offering a deeper understanding of dynamic queue implementation.  


Code

    
     
    #include<stdio.h>
    #include<conio.h>
    
    void main() {
        struct node {
            int data;
            struct node *next;
        } *front = NULL, *rear = NULL;
    
        struct node *newnode;
        char ch;
        clrscr();
    
        do {
            newnode = (struct node*)malloc(sizeof(struct node));
            printf("\nEnter the element to push: ");
            scanf("%d", &newnode->data);
    
            if (rear == NULL) {
                front = newnode;
                rear = newnode;
                newnode->next = NULL;
            } else {
                newnode->next = rear;
                rear = newnode;
            }
    
            printf("Add more (y/n): ");
            ch = getch();
        } while ((ch == 'y') || (ch == 'Y'));
    
        printf("\nThe deletion order is");
        printf("\n(Front)");
    
        for (; front != NULL; front = front->next) {
            printf("\n%d", front->data);
        }
    
        printf("\n(Rear)");
        getch();
    }
       
     


Output


    Enter the element to push: 5
    Add more (y/n): y
    
    Enter the element to push: 8
    Add more (y/n): y
    
    Enter the element to push: 3
    Add more (y/n): n
    
    The deletion order is
    (Front)
    3
    8
    5
    (Rear)
       




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 queue. 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.
Users input elements to be enqueued into the queue.
4. Dynamic Memory Allocation:
malloc is used to dynamically allocate memory for each new node.
5. Queue Implementation:
If the queue is empty (rear == NULL), the new node becomes both the front and rear.
Otherwise, the new node is linked to the rear, and the rear is updated.
6. User Interaction:
Users can continue to enqueue elements into the queue until they choose to stop.
7. Display Deletion Order:
The program displays the deletion order by traversing the queue from front to rear.


Conclusion

     This C program provides a dynamic implementation of a queue using linked structures. Users can interactively enqueue elements into the queue and observe the deletion order, gaining insights into dynamic memory allocation and linked list operations. Feel free to run the program, enqueue different elements, and observe how the queue dynamically adjusts and displays the deletion order accordingly. This example serves as an engaging introduction to dynamic queue implementation in programming. Happy coding!

Post a Comment

0 Comments