Implementation of Queue Using Linked List In C

Queue: It is a linear data structure. Collection with access only to the first element inserted.

  • First In First Out (FIFO)
  • Enqueue/Dequeue
  • Remove/Pop
  • front, rear
  • Make empty
Implementation of Queue Using Linked List In C

Code For Implementation of Queue Using Linked List In C

A queue can be implemented by both an array and a linked list.

Code here 👇

#include<stdio.h>
#include<stdlib.h>
struct node *front = NULL, *rear = NULL;

struct node{
    int data;
    struct node *next;
};

void enqueue(int a){
    struct node *new = malloc(sizeof(struct node));
    new->data = a;
    new->next = NULL;

    if (front==NULL && rear==NULL)
    {
        front=rear=new;
    }
    else{
        rear->next = new;
        rear = new;
    }
    
}

void dequeue(){
    struct node *temp;

    if (front==NULL)
    {
        printf("Queue is Empty\n");
    }
    else{
        printf("Dequed element is %d\n",front->data);
        temp = front;
        front = front->next;
        if(front==NULL){
            rear = NULL;
        }

        free(temp);
    }
    
}

int main(){
    dequeue();
    enqueue(1);
    enqueue(2);
    enqueue(3);
    enqueue(4);
    dequeue();
    dequeue();
    dequeue();
    return 0;
}

If You Like This Page Then Make Sure To Follow Us on Facebook, G News and Subscribe Our YouTube Channel. We will provide you updates daily.
Share on:

NK Coderz is a Computer Science Portal. Here We’re Proving DSA, Free Courses, Leetcode Solutions, Programming Languages, Latest Tech Updates, Blog Posting Etc.

Leave a Comment