Write A Program To Create Circular Linked List, Delete at The Begining, Display Circular Link List
Write A Program To Create Circular Linked List, Delete at The Begining, Display Circular Link List
Theory:
A Circular Linked List is a variation of Linked List where the last element points back to the first
element, forming a circular chain. In this operation, we delete the node at the beginning of the list
Code:
#include<stdio.h>
#include<stdlib.h>
struct Node {
int data;
};
if (*head == (*head)->next) {
free(*head);
*head = NULL;
} else {
last->next = (*head)->next;
temp = *head;
*head = (*head)->next;
free(temp);
do {
temp = temp->next;
printf("
");
newNode->data = data;
newNode->next = *head;
if (*head != NULL) {
while (last->next != *head) last = last->next;
last->next = newNode;
} else {
newNode->next = newNode;
*head = newNode;
int main() {
createCircularLinkedList(&head, 1);
createCircularLinkedList(&head, 2);
createCircularLinkedList(&head, 3);
createCircularLinkedList(&head, 4);
printf("Original List:
");
display(head);
deleteAtBeginning(&head);
");
display(head);
return 0;
}
Circular Linked List: Insert at End
Aim: To write a program to create a Circular Linked List, insert a node at the end, and display the Circular L
Theory:
Inserting a node at the end of a Circular Linked List involves adding the new node just before the
node that points back to the head, and updating pointers accordingly.
Code:
#include<stdio.h>
#include<stdlib.h>
struct Node {
int data;
};
newNode->data = data;
newNode->next = *head;
if (*head != NULL) {
temp->next = newNode;
} else {
*head = newNode;
newNode->next = newNode;
}
do {
temp = temp->next;
printf("
");
int main() {
insertAtEnd(&head, 1);
insertAtEnd(&head, 2);
insertAtEnd(&head, 3);
");
display(head);
return 0;
}
Theory:
In Circular Linked List, deleting the last node requires traversing the list to find the second last node
Code:
#include<stdio.h>
#include<stdlib.h>
struct Node {
int data;
};
if ((*head)->next == *head) {
free(*head);
*head = NULL;
return;
temp = temp->next;
prev->next = *head;
free(temp);
do {
temp = temp->next;
printf("
");
newNode->data = data;
newNode->next = *head;
if (*head != NULL) {
while (temp->next != *head) temp = temp->next;
temp->next = newNode;
} else {
*head = newNode;
newNode->next = newNode;
int main() {
createCircularLinkedList(&head, 1);
createCircularLinkedList(&head, 2);
createCircularLinkedList(&head, 3);
printf("Original List:
");
display(head);
deleteAtEnd(&head);
");
display(head);
return 0;