fork download
  1. /*
  2.  ============================================================================
  3.  Name : week2.c
  4.  Author : Haytham
  5.  Version :
  6.  Copyright : Your copyright notice
  7.  Description : Hello World in C, Ansi-style
  8.  ============================================================================
  9.  */
  10.  
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13.  
  14. // Define the structure for a doubly linked list node
  15. struct Node {
  16. int data;
  17. struct Node *next;
  18. struct Node *prev;
  19. };
  20.  
  21. // Function to insert a new node at the beginning of the doubly linked list
  22. void insertAtBeginning(struct Node **head_ref, int new_data) {
  23. // 1. Allocate memory for the new node
  24. struct Node *new_node = malloc(sizeof(struct Node));
  25.  
  26. // 2. Set the data for the new node
  27. new_node->data = new_data;
  28.  
  29. // 3. Make the new node's next point to the current head
  30. new_node->next = *head_ref;
  31.  
  32. // 4. Set the previous pointer of the new node to NULL (as it will be the new head)
  33. new_node->prev = NULL;
  34.  
  35. // 5. Change the previous pointer of the current head (if it exists) to the new node
  36. if (*head_ref != NULL) {
  37. (*head_ref)->prev = new_node;
  38. }
  39.  
  40. // 6. Move the head pointer to the new node
  41. *head_ref = new_node;
  42. }
  43.  
  44. // Function to insert a new node at the end of the doubly linked list
  45. void insertAtEnd(struct Node **head_ref, int new_data) {
  46. // 1. Allocate memory for the new node
  47. struct Node *new_node = malloc(sizeof(struct Node));
  48.  
  49. // 2. Set the data for the new node
  50. new_node->data = new_data;
  51. new_node->next = NULL; // The new node will be the last, so next is NULL
  52.  
  53. // 3. If the list is empty, make the new node the head
  54. if (*head_ref == NULL) {
  55. new_node->prev = NULL;
  56. *head_ref = new_node;
  57. return;
  58. }
  59.  
  60. // 4. Traverse to the last node of the list
  61. struct Node *ptr = *head_ref;
  62. while (ptr->next != NULL) {
  63. ptr = ptr->next;
  64. }
  65.  
  66. // 5. Update the last node's next pointer to the new node
  67. ptr->next = new_node;
  68.  
  69. // 6. Set the new node's prev pointer to the last node
  70. new_node->prev = ptr;
  71. }
  72.  
  73. // Function to delete the first node of the doubly linked list
  74. void deleteFirstNode(struct Node **head_ref) {
  75. // 1. Check if the list is empty
  76. if (*head_ref == NULL) {
  77. printf("The list is empty. Nothing to delete.\n");
  78. return;
  79. }
  80.  
  81. // 2. Store the current head node in a temporary variable
  82. struct Node *ptr = *head_ref;
  83.  
  84. // 3. Update the head pointer to point to the second node
  85. *head_ref = ptr->next;
  86.  
  87. // 4. Set the previous pointer of the new head to NULL (if the new head exists)
  88. if (*head_ref != NULL) {
  89. (*head_ref)->prev = NULL;
  90. }
  91.  
  92. // 5. Free the memory occupied by the old head node
  93. free(ptr);
  94. }
  95.  
  96. // Function to delete the last node of the doubly linked list
  97. void deleteLastNode(struct Node **head_ref) {
  98. // 1. Check if the list is empty
  99. if (*head_ref == NULL) {
  100. printf("The list is empty. Nothing to delete.\n");
  101. return;
  102. }
  103.  
  104. // 2. Check if the list has only one node
  105. if ((*head_ref)->next == NULL) {
  106. free(*head_ref);
  107. *head_ref = NULL;
  108. return;
  109. }
  110.  
  111. // 3. Traverse to the last node
  112. struct Node *ptr = *head_ref;
  113. while (ptr->next != NULL) {
  114. ptr = ptr->next;
  115. }
  116.  
  117. // 4. Update the second-to-last node's next pointer to NULL
  118. ptr->prev->next = NULL;
  119. ptr->prev = NULL;
  120. // 5. Free the memory occupied by the last node
  121. free(ptr);
  122. }
  123.  
  124. // Function to print the doubly linked list
  125. void printList(struct Node *node) {
  126. struct Node *last;
  127. printf("Forward Traversal: ");
  128. while (node != NULL) {
  129. printf("%d ", node->data);
  130. last = node;
  131. node = node->next;
  132. }
  133. printf("\n");
  134. }
  135.  
  136. int main() {
  137. // Create an empty doubly linked list
  138. struct Node *head = NULL;
  139.  
  140. // Insert some nodes at the beginning
  141. insertAtBeginning(&head, 10);
  142. insertAtBeginning(&head, 20);
  143. insertAtBeginning(&head, 30);
  144.  
  145. // Print the doubly linked list
  146. printList(head);
  147.  
  148. // Insert some nodes at the end
  149. insertAtEnd(&head, 101);
  150. insertAtEnd(&head, 202);
  151. insertAtEnd(&head, 303);
  152.  
  153. // Print the doubly linked list
  154. printList(head);
  155.  
  156. // Delete the first node
  157. deleteFirstNode(&head);
  158.  
  159. // Delete the last node
  160. deleteLastNode(&head);
  161.  
  162. // Print the list after deletion
  163. printList(head);
  164.  
  165. return 0;
  166. }
  167.  
  168.  
Success #stdin #stdout 0s 5288KB
stdin
Standard input is empty
stdout
Forward Traversal: 30 20 10 
Forward Traversal: 30 20 10 101 202 303 
Forward Traversal: 20 10 101 202