0% found this document useful (0 votes)
13 views

Write A Program To Create Circular Linked List, Delete at The Begining, Display Circular Link List

Uploaded by

omkarkera2006
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Write A Program To Create Circular Linked List, Delete at The Begining, Display Circular Link List

Uploaded by

omkarkera2006
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Circular Linked List Programs

Circular Linked List: Delete at Beginning


Aim: To write a program to create a Circular Linked List, delete a node at the beginning, and display the Cir

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

and then traverse the list to display it.

Code:

#include<stdio.h>

#include<stdlib.h>

struct Node {

int data;

struct Node* next;

};

void deleteAtBeginning(struct Node** head) {

struct Node *temp = *head, *last = *head;

if (*head == NULL) return;

while (last->next != *head) last = last->next;

if (*head == (*head)->next) {

free(*head);

*head = NULL;

} else {
last->next = (*head)->next;

temp = *head;

*head = (*head)->next;

free(temp);

void display(struct Node* head) {

if (head == NULL) return;

struct Node* temp = head;

do {

printf("%d -> ", temp->data);

temp = temp->next;

} while (temp != head);

printf("

");

void createCircularLinkedList(struct Node** head, int data) {

struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));

struct Node* last = *head;

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() {

struct Node* head = NULL;

createCircularLinkedList(&head, 1);

createCircularLinkedList(&head, 2);

createCircularLinkedList(&head, 3);

createCircularLinkedList(&head, 4);

printf("Original List:

");

display(head);

deleteAtBeginning(&head);

printf("After Deleting at Beginning:

");

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;

struct Node* next;

};

void insertAtEnd(struct Node** head, int data) {

struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));

struct Node* temp = *head;

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;
}

void display(struct Node* head) {

if (head == NULL) return;

struct Node* temp = head;

do {

printf("%d -> ", temp->data);

temp = temp->next;

} while (temp != head);

printf("

");

int main() {

struct Node* head = NULL;

insertAtEnd(&head, 1);

insertAtEnd(&head, 2);

insertAtEnd(&head, 3);

printf("Circular Linked List:

");

display(head);

return 0;
}

Circular Linked List: Delete at End


Aim: To write a program to create a Circular Linked List, delete a node at the end, and display the Circular

Theory:

In Circular Linked List, deleting the last node requires traversing the list to find the second last node

and updating its pointer to point back to the head.

Code:

#include<stdio.h>

#include<stdlib.h>

struct Node {

int data;

struct Node* next;

};

void deleteAtEnd(struct Node** head) {

if (*head == NULL) return;

struct Node* temp = *head, *prev;

if ((*head)->next == *head) {

free(*head);

*head = NULL;

return;

while (temp->next != *head) {


prev = temp;

temp = temp->next;

prev->next = *head;

free(temp);

void display(struct Node* head) {

if (head == NULL) return;

struct Node* temp = head;

do {

printf("%d -> ", temp->data);

temp = temp->next;

} while (temp != head);

printf("

");

void createCircularLinkedList(struct Node** head, int data) {

struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));

struct Node* temp = *head;

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() {

struct Node* head = NULL;

createCircularLinkedList(&head, 1);

createCircularLinkedList(&head, 2);

createCircularLinkedList(&head, 3);

printf("Original List:

");

display(head);

deleteAtEnd(&head);

printf("After Deleting at End:

");

display(head);

return 0;

You might also like