0% found this document useful (0 votes)
7 views2 pages

Circular Queue Using Linked List

The document provides a C implementation of a circular queue using a linked list. It includes functions for checking if the queue is empty, adding elements (enQueue), removing elements (deQueue), and displaying the queue. The main function demonstrates the usage of these operations with sample data.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views2 pages

Circular Queue Using Linked List

The document provides a C implementation of a circular queue using a linked list. It includes functions for checking if the queue is empty, adding elements (enQueue), removing elements (deQueue), and displaying the queue. The main function demonstrates the usage of these operations with sample data.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

9/23/25, 9:17 PM circular_queue_using_linked_list

1 #include <stdio.h>
2 #include <stdlib.h>
3
4 struct Node {
5 int data;
6 struct Node* link;
7 };
8
9 struct Node* front = NULL;
10 struct Node* rear = NULL;
11
12 // Check if the queue is empty
13 int isEmpty() {
14 return front == NULL;
15 }
16
17 // Adding an element
18 void enQueue(int value) {
19 struct Node* newNode = (struct Node*) malloc(sizeof(struct Node));
20 newNode->data = value;
21 newNode->link = NULL;
22 if(isEmpty()) {
23 front = newNode;
24 } else {
25 rear->link = newNode;
26 }
27 rear = newNode;
28 rear->link = front;
29 printf("\n Inserted -> %d", value);
30 }
31
32 // Deleting an element
33 int deQueue() {
34 int value;
35 if(isEmpty()) {
36 printf("\n Queue is Empty!!");
37 return -1;
38 } else {
39 struct Node* temp = front;
40 value = temp->data;
41 if(front == rear) {
42 front = rear = NULL;
43 } else {
44 front = front->link;
45 rear->link = front;
46 }
47 free(temp);
48 return value;
49 }

[Link] 1/2
9/23/25, 9:17 PM circular_queue_using_linked_list

50 }
51
52 // Display the queue
53 void display() {
54 struct Node* temp = front;
55 if(isEmpty()) {
56 printf("\n Queue is Empty!!\n");
57 } else {
58 printf("\n Front -> ");
59 do {
60 printf("%d ", temp->data);
61 temp = temp->link;
62 } while(temp != front);
63 printf("\n");
64 }
65 }
66
67 int main() {
68 enQueue(14);
69 enQueue(22);
70 enQueue(6);
71 // Display elements present in the queue
72 printf("Initial Queue ");
73 display();
74 // Deleting elements from queue
75 printf("\nElement Removed = %d", deQueue());
76 // Display elements present in the queue
77 printf("\nQueue after deletion an element: ");
78 display();
79 // Inserting elements to queue
80 enQueue(9);
81 //Showing the rear of the queue
82 printf("\nRear Element = %d", rear->data);
83 enQueue(20);
84 //Showing the front of the queue
85 printf("\nFront Element = %d", front->data);
86 printf("\nFinal Queue ");
87 display();
88 return 0;
89 }

[Link] 2/2

You might also like