0% found this document useful (0 votes)
10 views3 pages

Circular

Uploaded by

bishtvanshika642
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)
10 views3 pages

Circular

Uploaded by

bishtvanshika642
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
You are on page 1/ 3

Write a C program to implement the operation on (Circular Linked list):

i) Delete a node after given node.


ii) Find node with smallest data value
iii) Display the list
iv) Insert a node at the end of the list

answer:-

// i) Delete a node after a given node


void deleteAfterNode(Node** tail, int key) {
if (*tail == NULL) {
printf("List is empty. Nothing to delete.\n");
return;
}

Node* current = (*tail)->next; // Start from the first node


do {
if (current->data == key) {
Node* temp = current->next;
if (temp == current) { // Only one node in the list
free(temp);
*tail = NULL;
return;
}
current->next = temp->next;
if (temp == *tail) { // Update tail if we're deleting the tail node
*tail = current;
}
free(temp);
printf("Node after %d deleted.\n", key);
return;
}
current = current->next;
} while (current != (*tail)->next);

printf("Node with value %d not found.\n", key);


}

// ii) Find node with smallest data value


int findSmallest(Node* tail) {
if (tail == NULL) {
printf("List is empty.\n");
return -1; // Indicate empty list
}

Node* current = tail->next; // Start from the first node


int smallest = current->data;

do {
if (current->data < smallest) {
smallest = current->data;
}
current = current->next;
} while (current != tail->next);

return smallest;
}
// iii) Display the list
void displayList(Node* tail) {
if (tail == NULL) {
printf("List is empty.\n");
return;
}

Node* current = tail->next; // Start from the first node


do {
printf("%d -> ", current->data);
current = current->next;
} while (current != tail->next);

printf("(circular)\n");
}

// iv) Insert a node at the end of the list


void insertAtEnd(Node** tail, int data) {
Node* newNode = createNode(data);
if (*tail == NULL) {
*tail = newNode;
return;
}
newNode->next = (*tail)->next; // Link to the first node
(*tail)->next = newNode;
*tail = newNode; // Update the tail pointer
}

You might also like