Queues using Linked List in C

Implementation of Queues using Linked List in C

How to implement Queue using linked list?

Implementation of Queues using Linked List in C solves the problem of Queue implementation with arrays as using linked list for implementing queue we need not to define the size of the queue and it can work on the infinite number of values.

Implementing queue using linked list will not change its behavior i.e. the queue will continue to work with FIFO architecture.

Queue Using Linked List in C

enqueue(data)

  • Build a new node with given data.
  • Check if the queue is empty or not.
  • If queue is empty then assign new node to front and rear.
  • Else make next of rear as new node and rear as new node.

dequeue()

  • Check if queue is empty or not.
  • If queue is empty then dequeue is not possible.
  • Else store front in temp
  • And make next of front as front.

print()

  • Check if there is some data in the queue or not.
  • If the queue is empty print “No data in the queue.”
  • Else define a node pointer and initialize it with front.
  • Print data of node pointer until the next of node pointer becomes NULL.
Implementation of Queues using Linked List in C

Algorithm for enqueue(int d)

  • STRUCT NODE* NEW_N
  • NEW_N->DATA = D
  • NEW_N->NEXT = NULL
  • IF((FRONT == NULL)&&(REAR == NULL))
  • FRONT = REAR = NEW_N
  • ELSE
  • REAR->NEXT = NEW_N
  • REAR = NEW_N

Algorithm for dequeue()

  • STRUCT NODE *TEMP
  • TEMP = FRONT
  • IF((FRONT == NULL)&&(REAR == NULL))
  • RETURN
  • ELSE
  • FRONT = FRONT->NEXT
  • FREE(TEMP)

print()

  • STRUCT NODE* TEMP
  • IF((FRONT == NULL)&&(REAR == NULL))
  • RETURN
  • ELSE
  • TEMP = FRONT
  • WHILE(TEMP)
  • RETURN TEMP->DATA
  • TEMP = TEMP->NEXT

Code for implementing queue using linked list in C

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

struct node
{
	int data;
	struct node* next;
};//defining linked list to implement queue

struct node *front = NULL;
struct node *rear = NULL;

void enqueue(int d)//function to insert a node in queue
{
	struct node* new_n;
	new_n = (struct node*)malloc(sizeof(struct node));
	new_n->data = d;
	new_n->next = NULL;
	if((front == NULL)&&(rear == NULL)){
		front = rear = new_n;
	}
	else{
		rear->next = new_n;
		rear = new_n;
	}
	
}

void display()//function to display the queue
{
	struct node* temp;
	if((front == NULL)&&(rear == NULL)){
		printf("\nQueue is Empty");
	}
	else{
		temp = front;
		while(temp){
			printf(" %d ",temp->data);
			temp = temp->next;
		}
	}
}

void dequeue()//function to delete an element from a queue
{
	struct node *temp;
	temp = front;
	if((front == NULL)&&(rear == NULL)){
		printf("\nQueue is Empty");
	}
	else{
		front = front->next;
		free(temp);
	}
}
int main()//main function to use all our declared function
{
  enqueue(5);
  enqueue(10);
  enqueue(15);
  enqueue(20);
  enqueue(25);
  printf("Queue:");
  display();
  printf("\nQueue After Dequeue:");
  dequeue();
  display();
  
}

Output

Queue: 5  10  15  20  25 
Queue After Dequeue: 10  15  20  25 

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription

Stacks

Queues

Circular Queues

Priority Queue

  • Application of Priority Queue
  • Priority Queue Example
  • Priority Queue Introduction – C | C++ | Java
  • Priority Queue Implementation using Array – C | C++ | Java
  • Priority Queue using Linked List – C | C++ | Java
  • Priority Queue Insertion and Deletion- C | C++ | Java

Stacks

  • Introduction to Stack in Data Structure
    Click Here
  • Operations on a Stack
    Click Here
  • Stack: Infix, Prefix and Postfix conversions
    Click Here
  • Stack Representation in –
    C | C++ | Java
  • Representation of a Stack as an Array. –
    C | C++ | Java
  • Representation of a Stack as a Linked List. –
    C | C++ | Java
  • Infix to Postfix Conversion –
    C | C++ | Java
  • Infix to prefix conversion in –
    C | C++ | Java
  • Postfix to Prefix Conversion in –
    C | C++ | Java

Queues

  • Queues in Data Structures (Introduction)
    Click Here
  • Queues Program in C and implementation
    Click Here
  • Implementation of Queues using Arrays | C Program
    Click Here
  • Types of Queues in Data Structure
    Click Here
  • Application of Queue Data Structure
    Click Here
  • Insertion in Queues Program (Enqueuing) –
    C | C++ | Java
  • Deletion (Removal) in Queues Program(Dequeuing) –
    C | C++ | Java
  • Reverse a Queue –
    C | C++ | Java
  • Queues using Linked Lists –
    C | C++ | Java
  • Implement Queue using Stack –
    C | C++ | Java
  • Implement Queue using two Stacks –
    C | C++ | Java

Circular Queues

Priority Queue

  • Application of Priority Queue
  • Priority Queue Example
  • Priority Queue Introduction –
    C | C++ | Java
  • Priority Queue Implementation using Array –
    C | C++ | Java
  • Priority Queue using Linked List –
    C | C++ | Java
  • Priority Queue Insertion and Deletion-
    C | C++ | Java

One comment on “Queues using Linked List in C”


  • Shubham Das

    int main()//main function to use all our declared function
    {
    int opt=0,n,i,data;
    while(opt!=4){
    printf(“\n\n1 for Insert the Data in Queue\n2 for show the Data in Queue \n3 for Delete the data from the Queue\n4 for Exit\n\nEnter Your Choice:-“);
    scanf(“%d”,&opt);
    switch(opt){
    case 1:
    printf(“\nEnter the size: “);
    scanf(“%d”,&n);
    printf(“\nEnter your data\n”);
    i=0;
    while(i<n){
    scanf("%d",&data);
    enqueue(data);
    i++;
    }
    break;
    case 2:
    print();
    break;
    case 3:
    dequeue();
    break;
    case 4:
    break;
    default:
    printf("\nIncorrect Choice");

    }
    }
    return 0;
    }