Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.50 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include "linkedlist.h"
  4.  
  5. void init_node(node* newNode)
  6. {
  7. newNode->data = 0;
  8. newNode->next = NULL;
  9. }
  10.  
  11. node* traverse(node* head, int index)
  12. {
  13. node* current = head;
  14. node* previous = NULL;
  15.  
  16. int i;
  17. for(i = 0; i < index && current != NULL; i++)
  18. {
  19. previous = current;
  20. current = current->next;
  21. }
  22.  
  23. if(i == index)
  24. return previous;
  25.  
  26. else
  27. return NULL;
  28. }
  29.  
  30. node* insert_node(node* head, int index, int value)
  31. {
  32. node* newNode = (node*)malloc(sizeof(node));
  33. init_node(newNode);
  34. newNode->data = value;
  35.  
  36. if(index == 0) // Inserting at the beginning
  37. {
  38. if(head == NULL) // Means we have an empty list
  39. {
  40. /* For students to do */
  41. head = newNode;
  42. newNode->next = NULL;
  43. }
  44. else // Inserting at the beginning, non-empty list
  45. {
  46. /* For students to do */
  47. newNode->next = head;
  48. head = newNode;
  49. }
  50. }
  51. else // Inserting at middle or end
  52. {
  53. node* previous = traverse(head, index);
  54. if(previous == NULL) // Index is out of bounds, cannot insert_node
  55. {
  56. printf("Index %d is out of bounds.\n", index);
  57. }
  58. else // previous is at node before index
  59. {
  60. newNode->next = previous->next;
  61. previous->next = newNode;
  62. }
  63. }
  64.  
  65. return head;
  66. }
  67.  
  68.  
  69. node* delete_node(node* head, int index)
  70. {
  71. node* tempdlt;
  72. node* previous = traverse(head, index);
  73.  
  74. if(index == 0) // deleting the head node
  75. {
  76. if(head == NULL)
  77. {
  78. printf("Empty list, nothing to delete.\n");
  79. }
  80. else
  81. {
  82. tempdlt = head;
  83. head = head->next;
  84. free(tempdlt);
  85. }
  86. }
  87. else // delete in the middle or the end
  88. {
  89. /* for you to do */
  90. if(previous == NULL){
  91. printf("Error 404 not found, Enter correct index\n");
  92. }
  93. else{
  94. tempdlt = previous->next;
  95. previous->next = previous->next->next;
  96. free(tempdlt);
  97. }
  98. }
  99.  
  100. return head;
  101. }
  102.  
  103. void print_list(node* head)
  104. {
  105. node* current = head;
  106. /* for students to do */
  107. if(head == NULL){
  108. printf("Current doesn't exist: Error 404 not found\n");
  109. }
  110. else{
  111. while(current != NULL){
  112. printf("%d \n", current->data);
  113. current = current->next;
  114. }
  115. }
  116. }
  117.  
  118. node* destroy_list(node* head)
  119. {
  120. node* previous;
  121. while(head != NULL)
  122. {
  123. previous = head;
  124. head = head->next;
  125. free(previous);
  126. }
  127. return head;
  128. }
  129.  
  130. int return_data(node* head, int index)
  131. {
  132. node* previous = traverse(head, index);
  133. /* For students to do */
  134. return previous->next->data; // Zero is a place holder.
  135. // You need to replace it with something else.
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement