lacedu-computer Science

Circular Linked List

write a c program that uses functions to perform the following operations on a circular linked list.

A Circular Linked List is a type of data structure used in computer science and programming. It is similar to a traditional singly linked list, with the key difference that in a circular linked list, the last node of the list is connected back to the first node, creating a closed loop. This means that you can traverse the entire list starting from any node and keep moving until you reach the starting point again.

i. Creation ii. Insertion iii. Deletion. (iv). Traversal

#include<stdio.h>
#include<conio.h>

// Define the structure of a node in the circular linked list
struct Node {
int data;
struct Node *next;
};

// Create a new node with the given data
struct Node *createNode(int data) 

{
struct Node *newNode = (struct Node*) malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
return newNode;
}

// Insert a node at the beginning of the circular linked list
struct Node *insertBegin(struct Node *head, int data) {
struct Node *newNode = createNode(data);
if (head == NULL) {
head = newNode;
newNode->next = head;
} else {
struct Node *temp = head;
while (temp->next != head) {
temp = temp->next;
}
temp->next = newNode;
newNode->next = head;
head = newNode;
}
return head;
}

// Insert a node at the end of the circular linked list
struct Node *insertEnd(struct Node *head, int data)

 {
struct Node *newNode = createNode(data);
if (head == NULL) {
head = newNode;
newNode->next = head;
} else {
struct Node *temp = head;
while (temp->next != head) {
temp = temp->next;
}
temp->next = newNode;
newNode->next = head;
}
return head;
}

// Delete a node with the given data from the circular linked list
struct Node *deleteNode(struct Node *head, int data) {
if (head == NULL) {
printf(“List is empty\n”);
return head;
}
struct Node *curr = head, *prev = NULL;
while (curr->data != data) {
if (curr->next == head) {
printf(“Data not found in the list\n”);
return head;
}
prev = curr;
curr = curr->next;
}
if (curr->next == head && prev == NULL) {
head = NULL;
free(curr);
return head;
}
if (curr == head) {
prev = head;
while (prev->next != head) {
prev = prev->next;
}
head = curr->next;
prev->next = head;
free(curr);
} else if (curr->next == head) {
prev->next = head;
free(curr);
} else {
prev->next = curr->next;
free(curr);
}
return head;
}

// Traverse the circular linked list and print its elements
void traverseList(struct Node *head) {
if (head == NULL) {
printf(“List is empty\n”);
return;
}
struct Node *temp = head;
do {
printf(“%d “, temp->data);
temp = temp->next;
} while (temp != head);
}

int main() {
struct Node *head = NULL;

// Insert elements at the beginning of the list

head = insertBegin(head, 5);

head = insertBegin(head, 4);

head = insertBegin(head, 3); 

// Insert elements at the end of the list

head = insertEnd(head, 6);

head = insertEnd(head, 7); 

// Traverse the list and print its elements

printf(“List elements: “);

traverseList(head);

head=deleteNode(head,6);

printf (“\n after deleting:  “);

traverseList(head);

}

OUTPUT:

List Elements: 3 4 5 6 7

After Deleting: 3 4 5 7 

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top