Advertisement
Guest User

Untitled

a guest
Sep 24th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.49 KB | None | 0 0
  1. /* list testing program
  2. * Dwight Makaroff:
  3. * modified Sep 24, 2010
  4. * modified October 2, 2013
  5. */
  6.  
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include "list.h"
  10.  
  11. int dblcomp(void *a, void *b)
  12. {
  13. return ( *(double*) a == *(double *) b);
  14. }
  15.  
  16.  
  17. void PrintDblList(LIST *L)
  18. {
  19. int i = 0;
  20. /* REAL HACK */
  21. void *p;
  22. void *saveItem = ListCurr(L);
  23.  
  24. p = ListFirst(L);
  25.  
  26. while (p && i< ListCount(L)){
  27. printf ("List Item is %f\n", * (double *) p);
  28. p = ListNext(L);
  29. i++;
  30.  
  31. }
  32.  
  33.  
  34. /* If multiple identical items, like integers are in the list,
  35. one would have to do some sort of counting to know which
  36. item in the list we were at when PrintIntList was called.
  37. I'm thinking that you could do a traverse to the end before
  38. printing and then after printing, move k steps back from the end.
  39. */
  40.  
  41. /* this works for now */
  42.  
  43. p = ListFirst(L);
  44.  
  45. while (saveItem != ListCurr(L))
  46. p = ListNext(L);
  47.  
  48. return;
  49.  
  50. }
  51.  
  52. void PrintDblItem(void *i)
  53. {
  54. printf("PI: %f\n", * (double*) i);
  55. }
  56.  
  57. void PrintIntItem(void *i)
  58. {
  59. printf("PI: %ld\n", (long) i);
  60. }
  61.  
  62.  
  63. void myItemFree(void *a)
  64. {
  65. return;
  66. }
  67.  
  68. void myDblItemFree(void *a)
  69. {
  70. free (a);
  71. return;
  72. }
  73.  
  74. void doubleListTesting(void)
  75. {
  76. void *myItem;
  77. double *b, *c, *a, *z;
  78.  
  79. LIST *L1,*L2;
  80. int rv, counter;
  81. b = (double *) malloc (sizeof(double));
  82. *b = 22.2;
  83. a = (double *) malloc (sizeof(double));
  84. *a = 1;
  85. z = (double *) malloc (sizeof(double));
  86. *z = 99.2;
  87. c = (double *) malloc (sizeof(double));
  88. *c = 420;
  89.  
  90. L1 = ListCreate();
  91.  
  92. counter = ListCount(L1);
  93.  
  94. myItem = ListLast(L1);
  95.  
  96. myItem = ListFirst(L1);
  97. if (myItem == NULL)
  98. {
  99. printf("item is null on empty list\n");
  100. }
  101.  
  102. myItem = ListNext(L1);
  103.  
  104.  
  105. if (myItem == NULL)
  106. {
  107. printf("trying to go to past the end of list \n");
  108. }
  109.  
  110. myItem = ListPrev(L1);
  111. if (myItem == NULL)
  112. {
  113. printf("trying to go to before the beginning of list \n");
  114. }
  115.  
  116. rv = ListAppend(L1, (void *)a);
  117. if (rv == -1)
  118. {
  119. printf("trying to append failed \n");
  120. }
  121. else
  122. {
  123. printf("appended %f to the list \n", *a);
  124. }
  125.  
  126. myItem = ListPrev(L1);
  127. if (myItem == NULL)
  128. {
  129. printf("trying to go to before the beginning of the end of list \n");
  130. }
  131.  
  132. rv = ListPrepend(L1, (void *) b);
  133. if (rv != 0)
  134. {
  135. printf("trying to prepend failed\n");
  136. }
  137. else
  138. {
  139. printf("prepended %f to the list \n", *b);
  140. }
  141.  
  142. if (rv == 0) PrintDblList(L1);
  143. myItem = ListFirst(L1);
  144. if (myItem)
  145. printf("at beginning of list\n");
  146. else
  147. {
  148. perror("ListFirst");
  149. }
  150.  
  151. rv = ListAdd(L1, (void *) z);
  152. if (rv !=0)
  153. {
  154. printf("trying to add failed\n");
  155. }
  156. else
  157. {
  158. printf("added %f to the list \n", *z);
  159. }
  160.  
  161.  
  162. rv = ListInsert(L1, (void *)c);
  163. if (rv != 0)
  164. {
  165. printf("trying to insert failed \n");
  166. }
  167. else
  168. {
  169. printf("insert %f to the list \n", *c);
  170. }
  171.  
  172.  
  173. if (rv == 0)
  174. {
  175. PrintDblList(L1);
  176. printf("list should be: %f %f %f %f\n ", *b, *c, *z, *a);
  177. }
  178.  
  179. myItem = ListFirst(L1);
  180.  
  181. L2 = ListCreate();
  182.  
  183. ListConcat(L1,L2);
  184. myItem = ListSearch(L1, dblcomp, (void *)z);
  185. myItem = ListTrim(L1);
  186. myItem = ListRemove(L1);
  187.  
  188. ListFree(L1,myItemFree);
  189.  
  190. printf("DONE WITH ptr to DOUBLE TESTING\n\n");
  191. }
  192.  
  193. int main (void)
  194. {
  195.  
  196. doubleListTesting();
  197. exit(0);
  198.  
  199.  
  200. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement