fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. // Node structure
  5. struct Node {
  6. int data;
  7. struct Node* next;
  8. };
  9.  
  10. // Linked list structure
  11. struct LinkedList {
  12. struct Node* head;
  13. };
  14.  
  15. // Function to create a new node
  16. struct Node* createNode(int data) {
  17. struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
  18. if (newNode == NULL) {
  19. printf("Memory allocation failed\n");
  20. exit(1);
  21. }
  22. newNode->data = data;
  23. newNode->next = NULL;
  24. return newNode;
  25. }
  26.  
  27. // Function to insert a node at the end of the circular linked list
  28. void insertAtEnd(struct LinkedList* list, int data) {
  29. struct Node* newNode = createNode(data);
  30. if (list->head == NULL) {
  31. list->head = newNode;
  32. newNode->next = newNode; // Point to itself to make it circular
  33. return;
  34. }
  35. struct Node* temp = list->head;
  36. while (temp->next != list->head) {
  37. temp = temp->next;
  38. }
  39. temp->next = newNode;
  40. newNode->next = list->head;
  41. }
  42.  
  43. // Function to delete a node with given value from the circular linked list
  44. void deleteNode(struct LinkedList* list, int key) {
  45. if (list->head == NULL) return;
  46.  
  47. struct Node *current = list->head, *prev = NULL;
  48. while (current->data != key) {
  49. if (current->next == list->head) {
  50. printf("Node with value %d not found\n", key);
  51. return;
  52. }
  53. prev = current;
  54. current = current->next;
  55. }
  56.  
  57. // If the node to be deleted is the only node in the list
  58. if (current->next == list->head && prev == NULL) {
  59. free(current);
  60. list->head = NULL;
  61. return;
  62. }
  63.  
  64. // If the list has more than one node
  65. if (current == list->head) {
  66. prev = list->head;
  67. while (prev->next != list->head)
  68. prev = prev->next;
  69. list->head = current->next;
  70. prev->next = list->head;
  71. free(current);
  72. } else if (current->next == list->head) {
  73. prev->next = list->head;
  74. free(current);
  75. } else {
  76. prev->next = current->next;
  77. free(current);
  78. }
  79. }
  80.  
  81. // Function to display the circular linked list
  82. void display(struct LinkedList* list) {
  83. if (list->head == NULL) {
  84. printf("List is empty\n");
  85. return;
  86. }
  87. struct Node* temp = list->head;
  88. do {
  89. printf("%d ", temp->data);
  90. temp = temp->next;
  91. } while (temp != list->head);
  92. printf("\n");
  93. }
  94.  
  95. int main() {
  96. struct LinkedList ll;
  97. ll.head = NULL;
  98.  
  99. // Insert nodes
  100. insertAtEnd(&ll, 1);
  101. insertAtEnd(&ll, 2);
  102. insertAtEnd(&ll, 3);
  103. insertAtEnd(&ll, 4);
  104.  
  105. printf("Original List:\n");
  106. display(&ll);
  107.  
  108. // Delete node
  109. deleteNode(&ll, 3);
  110. printf("List after deleting node with value 3:\n");
  111. display(&ll);
  112.  
  113. return 0;
  114. }
  115.  
Success #stdin #stdout 0s 5304KB
stdin
Standard input is empty
stdout
Original List:
1 2 3 4 
List after deleting node with value 3:
1 2 4