Circular Queue using Linked List in C Programming

Implementation of Circular Queue using Linked List

Using Circular Queue is better than normal queue because there is no memory wastage. Linked list provide the facility of dynamic memory allocation so it is easy to create. When we implement circular Queue using linked list it is similar to circular linked list except there is two pointer front and rear in circular Queue where as circular linked list has only one pointer head. Lets see how to implement Circular Queue using Linked list in C Programming.

Circular Queue using Linked List

Steps for Implementing Circular Queue using Linked List in C

enqueue(data)

  • Create a struct node type node.
  • Insert the given data in the new node data section and NULL in address section.
  • If Queue is empty then initialize front and rear from new node.
  • Queue is not empty then initialize rear next and rear from new node.
  • New node next initialize from front

dequeue()

  • Check if queue is empty or not.
  • If queue is empty then dequeue is not possible.
  • Else Initialize temp from front.
  • If front is equal to the rear then initialize front and rear from null.
  • Print data of temp and free temp memory.
  • If there is more than one node in Queue then make front next to front then initialize rear next from front.
  • Print temp and free temp.

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 Circular Queues using Linked List in C

Algorithm for Implementing Circular Queue using linked list in C

enqueue(int d)

  • Create a new node of structure node type.
  • New_node->data = entered data.
  • New_node->Next = NULL.
  • IF(Front == NULL)
  • THEN
  • Front = Rear = New_node
  • Rear->Next = Front
  • END IF
  • Front = New_node.
  • End IF.
  • ELSE
  • Rear->Next = New_node
  • Rear = New_node
  • New_node->Next = Front
  • End ELSE

dequeue()

  • Create a temporary pointer temp  of node structure type
  • IF(Front == NULL)
  • Print  ‘Underflow Condition’
  • End IF
  • ELSE IF(Front == Rear)
  • Front = Rear = NULL
  • Free(temp)
  • End ELSE IF
  • ELSE
  • Front = Front->Next
  • Rear->Next = Front
  • Free(temp)

print()

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

C Program to Implement Circular Queue using Linked list

#include<stdio.h>
#include<stdlib.h>
struct node
{
	int data;
	struct node* next;
};
struct node *f = NULL;
struct node *r = NULL;
void enqueue(int d) //Insert elements in Queue
{
	struct node* n;
	n = (struct node*)malloc(sizeof(struct node));
	n->data = d;
	n->next = NULL;
	if((r==NULL)&&(f==NULL))
	{
		f = r = n;
		r->next = f;
	}
	else
	{
		r->next = n;
		r = n;
		n->next = f;
	}
} 
void dequeue() // Delete an element from Queue
{
	struct node* t;
	t = f;
	if((f==NULL)&&(r==NULL))
		printf("\nQueue is Empty");
	else if(f == r){
		f = r = NULL;
		free(t);
	}
	else{
		f = f->next;
		r->next = f;
		free(t);
	}
	
	
}
void print(){ // Print the elements of Queue
	struct node* t;
	t = f;
	if((f==NULL)&&(r==NULL))
		printf("\nQueue is Empty");
	else{
		do{
			printf("\n%d",t->data);
			t = t->next;
		}while(t != f);
	}
}
int main()
{
	int opt,n,i,data;
	printf("Enter Your Choice:-");
	do{
		printf("\n\n1 for Insert the Data in Queue\n2 for show the Data in Queue \n3 for Delete the data from the Queue\n0 for Exit");
		scanf("%d",&opt);
		switch(opt){
			case 1:
				printf("\nEnter the number of data");
				scanf("%d",&n);
				printf("\nEnter your data");
				i=0;
				while(i<n){
					scanf("%d",&data);
					enqueue(data);
					i++;
				}
				break;
			case 2:
				print();
				break;
			case 3:
				 dequeue();
				break;
			case 0:
				break;
			default:
				printf("\nIncorrect Choice");
			
		}
	}while(opt!=0);
return 0;
}
Enter the number of data 5

Enter your data
34
33
22
76
32


1 for Insert the Data in Queue
2 for show the Data in Queue
3 for Delete the data from the Queue
0 for Exit 3


1 for Insert the Data in Queue
2 for show the Data in Queue
3 for Delete the data from the Queue
0 for Exit 2

33
22
76
32

1 for Insert the Data in Queue
2 for show the Data in Queue
3 for Delete the data from the Queue
0 for Exit

Queue using Linked List

Here we have already learned implementation of Circular Queue using Linked List in C , click the button below to learn the implementation of queue using linked list in C