Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.54 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "mylist.h"
  4.  
  5.  
  6. /*
  7. * Create a node that holds the given data pointer,
  8. * and add the node to the front of the list.
  9. *
  10. * Note that this function does not manage the lifetime of the object
  11. * pointed to by 'data'.
  12. *
  13. * It returns the newly created node on success and NULL on failure.
  14. */
  15.  
  16. struct Node *addFront(struct List *list, void *data){
  17.  
  18. struct Node *newNode; //creating new node
  19. newNode= (struct Node *)malloc( sizeof(struct Node) ); //allocating memory to it
  20.  
  21. newNode -> data = data; //datas not same thing
  22.  
  23. newNode -> next= list ->head; //head is first element
  24. list -> head = newNode; //first element now points to new node we created
  25.  
  26. return newNode; //return data if newly created node sucessful
  27.  
  28.  
  29. }//end addFront
  30.  
  31. /*
  32. * Traverse the list, calling f() with each data item.
  33. */
  34.  
  35. void traverseList(struct List *list, void (*f)(void *)){ //f is pointer to function that takes void * and returns void
  36.  
  37. struct Node *traverser= list -> head;
  38.  
  39.  
  40. while ( traverser != 0) //looking at each element in the list
  41. {
  42. f(traverser -> data);
  43. traverser = traverser->next;
  44. }//end while loop
  45.  
  46.  
  47. }//end traverseList
  48.  
  49. /*
  50. * Compare two double values pointed to by the two pointers.
  51. * Return 0 if they are the same value, 1 otherwise.
  52. */
  53. int compareDouble(const void *data1, const void *data2) {
  54.  
  55. double c= *(double *) data1- *(double *) data2;
  56. if (c ==0) {
  57. return 0;
  58. }
  59. else return 1;
  60.  
  61. } //end compareDouble
  62.  
  63. /*
  64. * Traverse the list, comparing each data item with 'dataSought' using
  65. * 'compar' function. ('compar' returns 0 if the data pointed to by
  66. * the two parameters are equal, non-zero value otherwise.)
  67. *
  68. * Returns the first node containing the matching data,
  69. * NULL if not found.
  70. */
  71.  
  72. struct Node *findNode(struct List *list, const void *dataSought, int (*compar)(const void *, const void *)) {
  73.  
  74. struct Node *traverser = list -> head;
  75.  
  76. while ( traverser != 0) //for each element
  77. {
  78. if ( compar(traverser -> data, dataSought) == 0){ //looks for match using compare and returns node containing match
  79. return traverser;
  80. }//end inner if
  81.  
  82. traverser= traverser -> next;
  83.  
  84. }//end while loop
  85.  
  86. return NULL; //returns null if no match found
  87.  
  88.  
  89. }//end findNode
  90.  
  91. /*
  92. * Flip the sign of the double value pointed to by 'data' by
  93. * multiplying -1 to it and putting the result back into the memory
  94. * location.
  95. */
  96. void flipSignDouble(void *data) {
  97.  
  98. double * x = (double *) data;
  99. double y= *x*(-1); //dereference x
  100.  
  101. *x= y; //data now holds the address of the new value
  102.  
  103. }//end flipSignDouble
  104.  
  105.  
  106. /*
  107. * Remove the first node from the list, deallocate the memory for the
  108. * ndoe, and return the 'data' pointer that was stored in the node.
  109. * Returns NULL is the list is empty.
  110. */
  111. void *popFront(struct List *list){
  112.  
  113. struct Node *traverser= list -> head;
  114.  
  115. if ( traverser != 0 ) { //while list not empty
  116.  
  117. list -> head = traverser -> next;
  118. void * pointer= traverser ->data;
  119. free(traverser);
  120. return pointer;
  121.  
  122. } //end if statement
  123.  
  124. else
  125. return NULL; //returns null if list is empty
  126. }//end popFront
  127.  
  128. /*
  129. * Remove all nodes from the list, deallocating the memory for the
  130. * nodes. You can implement this function using popFront().
  131. */
  132. void removeAllNodes(struct List *list){
  133.  
  134. while ( list->head != 0) //looking at each element in the list
  135. {
  136. popFront(list); //removes each node at front hence removing all nodes
  137.  
  138. }//end while loop
  139.  
  140. }//end removeAllNodes
  141.  
  142. /*
  143. * Create a node that holds the given data pointer,
  144. * and add the node right after the node passed in as the 'prevNode'
  145. * parameter. If 'prevNode' is NULL, this function is equivalent to
  146. * addFront().
  147. *
  148. * Note that prevNode, if not NULL, is assumed to be one of the nodes
  149. * in the given list. The behavior of this function is undefined if
  150. * prevNode does not belong in the given list.
  151. *
  152. * Note that this function does not manage the lifetime of the object
  153. * pointed to by 'data'.
  154. *
  155. * It returns the newly created node on success and NULL on failure.
  156. */
  157. struct Node *addAfter(struct List *list, struct Node *prevNode, void *data){
  158.  
  159. struct Node *dataNode= NULL;
  160. if( prevNode ==NULL){
  161. dataNode= addFront(list, data);
  162.  
  163. } //end if statement
  164. else {
  165. dataNode= (struct Node *)malloc(sizeof(struct Node));
  166. dataNode -> data= data; //holds data pointer
  167.  
  168. dataNode-> next=prevNode -> next;
  169. prevNode-> next= dataNode;
  170. }
  171. return dataNode;
  172.  
  173.  
  174. }//end addAfter
  175.  
  176.  
  177. /*
  178.  
  179. * Note that this function reverses the list purely by manipulating
  180. * pointers. It does NOT call malloc directly or indirectly (which
  181. * means that it does not call addFront() or addAfter()).
  182. *
  183. * Implementation hint: keep track of 3 consecutive nodes (previous,
  184. * current, next) and move them along in a while loop. Your function
  185. * should start like this:
  186.  
  187.  
  188. struct Node *prv = NULL;
  189. struct Node *cur = list->head;
  190. struct Node *nxt;
  191.  
  192. while (cur) {
  193. ......
  194.  
  195.  
  196. * And at the end, prv will end up pointing to the first element of
  197. * the reversed list. Don't forget to assign it to list->head.
  198. */
  199. void reverseList(struct List *list){
  200.  
  201. struct Node *prv = NULL;
  202. struct Node *cur = list->head;
  203. struct Node *nxt;
  204.  
  205. while (cur) {
  206.  
  207. nxt = cur -> next;
  208. cur -> next = prv;
  209. prv = cur;
  210. cur = nxt;
  211.  
  212. }//end while loop
  213.  
  214. list -> head = prv;
  215. }//end reversist
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement