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;
}
Shares
facebook sharing button Share
twitter sharing button Tweet
whatsapp sharing button Share
telegram sharing button Share
pinterest sharing button Pin

Leave a Comment