Advertisement
jimangel2001

Untitled

Mar 8th, 2020
480
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.26 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. /*
  5. Node element
  6. */
  7. struct list{
  8. int value;
  9. struct list *next;
  10. };
  11.  
  12. /*
  13. This function generates a list backwards
  14. */
  15.  
  16. struct list* generateList(){
  17. struct list *node, *previous_node;
  18. int size;
  19.  
  20. printf("How many elements do you want to insert in the list? \n");
  21. scanf("%d", &size);
  22.  
  23. if (size <= 0) return NULL; //If the user requests an empty or negative list we return NULL
  24.  
  25. for (int i = 0; i < size; i++){ //repeat as many times as the user requested
  26. node = malloc(sizeof(struct list)); //we allocate memory for a new node
  27. (i == 0) ? (node->next = NULL) : (node->next = previous_node); //if it's the first node the next value becomes NULL, otherwise it's assigned the address of the next node;
  28. printf("Enter value: %d\n", i); //requesting and assigning an int to the value of the node
  29. scanf("%d", &(node->value));
  30. previous_node = node; //a temporary variable used to store the memory of this node for the next iteration, it becomes the .next value
  31. }
  32.  
  33. return node;
  34. };
  35.  
  36. /*
  37. This function removes a node by finding the previous one (it never needs to remove the first node
  38. due to the problems nature), stores the soon to be removed node in a variable, bypasses it, and removes it from the memory
  39. */
  40.  
  41. void removeNode(struct list* head, int index){
  42. struct list* tmp;
  43.  
  44. for (int i = 0; i < index-1; i++){ //Finding the previous node
  45. head = head->next;
  46. }
  47.  
  48. tmp = head->next; //To be removed node
  49.  
  50. head->next = head->next->next; //We bypass it
  51. free(tmp); //Clearing the memory
  52. }
  53.  
  54. /*
  55. This function is the solution to the problem, I wrote it at 5 A.M. I just got it working,
  56. I'm now trying to add comments, let's see what I can do, it's a miracle it's working,
  57. I'm not really sure how it manages to work at some parts.
  58. */
  59. struct list * removeDupl_and_PushBack(struct list * head, int number){
  60. /*
  61. Index: We will use it to store at what part of the list we are, later on it will also be used to express the length of the list
  62. Found: Boolean, it becomes one if we have found at least one of the requested number
  63. First_index: The index of the first appearance of the wanted number in the list
  64. Removed: Boolean, becomes true if we have removed a node in the current iteration
  65. */
  66. int index = 0, found = 0, first_index = 0,removed = 0;
  67. /*
  68. tmp: Usually used to iterate through the list
  69. tmp_2: Usually to store necessary nodes of various reasons
  70. */
  71. struct list *tmp = head, *tmp_2;
  72.  
  73.  
  74. /*
  75. This block of code, finds all the duplicates, removes them, stores the index
  76. of the first appearance, side-effect: fetches the length of the list
  77. */
  78. while (tmp != NULL){ //Iterating through the list
  79. if (tmp->value == number){ //If we find the wanted number
  80. if(found == 0){ //For the first time
  81. found = 1; //We store that we have found it
  82. first_index = index; //And store it's position
  83. }
  84. else{ //If it's not the first time
  85. tmp = tmp->next; //We move to the next node
  86. removeNode(head, index--); //And remove it
  87. removed = 1; //We store that we have removed a node this iteration
  88. printf("Removed node at position: %d\n", index+1);
  89. }
  90. }
  91. index++;
  92. if (!removed) tmp = tmp->next; //We move to the next element only if we haven't removed anything (the move was executed previously)
  93. removed = 0; //Re-initialize the removed var
  94. }
  95.  
  96. tmp = head;
  97. tmp_2 = head;
  98. if(found == 1){ //Only if we have found the requested number do we execute this code
  99. if (first_index == --index ){
  100. return head; //If the wanted number is the last of the list, we return the list as is
  101. printf("Number was last already.\n");
  102. }
  103. else{
  104. if(first_index == 0){ //If it's the first number
  105. while(tmp->next != NULL){ //We find the last node
  106. tmp = tmp->next;
  107. }
  108. tmp->next = tmp_2; //We set the new last node to the first one
  109. head = head->next; //We set the second node as the head
  110. tmp_2->next=NULL; //Last node points now to NULL
  111. printf("Number was first, moved to last.\n");
  112. return head; //We return the new head
  113. }else{ //If it's not the first node
  114. for (int i = 0; i < first_index-1; i++){ //We find the node previous to the requested
  115. tmp = tmp->next;
  116. }
  117. tmp_2 = tmp->next; //We store the requested node
  118. tmp->next = tmp->next->next; //We bypass it
  119. tmp = head;
  120. while(tmp->next != NULL){ //We find the last node
  121. tmp = tmp->next;
  122. }
  123. tmp->next = tmp_2; //We point it to the requested node
  124. tmp_2->next=NULL; //The last node now points to NULL
  125. printf("Number was at position: %d, moved to last.\n", first_index);
  126. return head; //Returning the list
  127. }
  128. }
  129. }
  130.  
  131. return head;
  132. };
  133.  
  134. /*
  135. Function used to print all the elements of a list
  136. */
  137.  
  138. void printList(struct list* head){
  139. if (head == NULL){
  140. printf("Nothing to print");
  141. return ;
  142. }
  143.  
  144. while (head != NULL){
  145. printf("%d ", head->value);
  146. head = head->next;
  147. }
  148. };
  149.  
  150.  
  151. int main()
  152. {
  153. int number;
  154.  
  155. struct list* head;
  156. if((head = generateList()) == NULL) return 0; //We assign head the address of the first node of the list, if it's NULL we terminate the program.
  157. printList(head);
  158. printf("Give the wanted number: ");
  159. scanf("%d", &number);
  160. head = removeDupl_and_PushBack(head, number);
  161. printList(head);
  162. return 0;
  163. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement