fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. // Define the structure of a node
  5. typedef struct Node {
  6. int data;
  7. struct Node *next;
  8. } Node;
  9.  
  10. // Function to create a new list node
  11. Node* createNode(int data) {
  12. Node *newNode = (Node*)malloc(sizeof(Node));
  13. if (newNode == NULL) {
  14. printf("Memory allocation failed.\n");
  15. exit(1);
  16. }
  17. newNode->data = data;
  18. newNode->next = NULL;
  19. return newNode;
  20. }
  21.  
  22. // Function to add a new node at the end of the list
  23. Node* createList(Node *head, int data) {
  24. Node *newNode = createNode(data);
  25. if (head == NULL) {
  26. return newNode;
  27. }
  28. Node *current = head;
  29. while (current->next != NULL) {
  30. current = current->next;
  31. }
  32. current->next = newNode;
  33. return head;
  34. }
  35.  
  36. // Function to search for a value in the list
  37. Node* searchList(Node *head, int value) {
  38. Node *current = head;
  39. while (current != NULL) {
  40. if (current->data == value) {
  41. return current;
  42. }
  43. current = current->next;
  44. }
  45. return NULL;
  46. }
  47.  
  48. // Function to insert a value at a specific position
  49. Node* insertList(Node *head, int data, int position) {
  50. Node *newNode = createNode(data);
  51. if (position == 0) {
  52. newNode->next = head;
  53. return newNode;
  54. }
  55. Node *current = head;
  56. for (int i = 0; i < position - 1 && current != NULL; i++) {
  57. current = current->next;
  58. }
  59. if (current == NULL) {
  60. printf("Position out of bounds.\n");
  61. free(newNode);
  62. return head;
  63. }
  64. newNode->next = current->next;
  65. current->next = newNode;
  66. return head;
  67. }
  68.  
  69. // Function to delete a node with a specific value
  70. Node* deleteList(Node *head, int value) {
  71. if (head == NULL) {
  72. printf("List is empty.\n");
  73. return NULL;
  74. }
  75. if (head->data == value) {
  76. Node *temp = head;
  77. head = head->next;
  78. free(temp);
  79. return head;
  80. }
  81. Node *current = head;
  82. while (current->next != NULL && current->next->data != value) {
  83. current = current->next;
  84. }
  85. if (current->next == NULL) {
  86. printf("Value not found.\n");
  87. return head;
  88. }
  89. Node *temp = current->next;
  90. current->next = temp->next;
  91. free(temp);
  92. return head;
  93. }
  94.  
  95. // Function to display the list
  96. void displayList(Node *head) {
  97. if (head == NULL) {
  98. printf("List is empty.\n");
  99. return;
  100. }
  101. Node *current = head;
  102. printf("List: ");
  103. while (current != NULL) {
  104. printf("%d -> ", current->data);
  105. current = current->next;
  106. }
  107. printf("NULL\n");
  108. }
  109.  
  110. // Function to free the entire list
  111. void freeList(Node *head) {
  112. Node *current = head;
  113. while (current != NULL) {
  114. Node *temp = current;
  115. current = current->next;
  116. free(temp);
  117. }
  118. printf("List has been freed.\n");
  119. }
  120.  
  121. // Main function to test the list operations
  122. int main() {
  123. Node *head = NULL;
  124.  
  125. // Create list
  126. head = createList(head, 10);
  127. head = createList(head, 20);
  128. head = createList(head, 30);
  129. displayList(head);
  130.  
  131. // Search list
  132. int searchValue = 20;
  133. Node *foundNode = searchList(head, searchValue);
  134. if (foundNode != NULL) {
  135. printf("Value %d found in the list.\n", searchValue);
  136. } else {
  137. printf("Value %d not found in the list.\n", searchValue);
  138. }
  139.  
  140. // Insert into list
  141. head = insertList(head, 25, 2);
  142. displayList(head);
  143.  
  144. // Delete from list
  145. head = deleteList(head, 20);
  146. displayList(head);
  147.  
  148. // Free the list
  149. freeList(head);
  150. head = NULL; // Set the head to NULL after freeing
  151. displayList(head);
  152.  
  153. return 0;
  154. }
  155.  
Success #stdin #stdout 0s 5284KB
stdin
Standard input is empty
stdout
List: 10 -> 20 -> 30 -> NULL
Value 20 found in the list.
List: 10 -> 20 -> 25 -> 30 -> NULL
List: 10 -> 25 -> 30 -> NULL
List has been freed.
List is empty.