Advertisement
Guest User

comp1927 - E&K

a guest
Jul 26th, 2016
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.40 KB | None | 0 0
  1. // IntList.c - Lists of integers
  2. // Written by John Shepherd, July 2008
  3.  
  4. #include <stdlib.h>
  5. #include <stdio.h>
  6. #include "assert.h"
  7. #include "IntList.h"
  8.  
  9. // data structures representing IntList
  10.  
  11. struct IntListNode {
  12. int data; // value of this list item
  13. struct IntListNode *next;
  14. // pointer to node containing next element
  15. };
  16.  
  17. struct IntListRep {
  18. int size; // number of elements in list
  19. struct IntListNode *first;
  20. // node containing first value
  21. struct IntListNode *last;
  22. // node containing last value
  23. };
  24.  
  25. // create a new empty IntList
  26. IntList newIntList()
  27. {
  28. struct IntListRep *L;
  29.  
  30. L = malloc(sizeof (struct IntListRep));
  31. assert (L != NULL);
  32. L->size = 0;
  33. L->first = NULL;
  34. L->last = NULL;
  35. return L;
  36. }
  37.  
  38. // free up all space associated with list
  39. void freeIntList(IntList L)
  40. {
  41. // does nothing ...
  42. }
  43.  
  44. // display list as one integer per line on stdout
  45. void showIntList(IntList L)
  46. {
  47. IntListPrint(stdout, L);
  48. }
  49.  
  50. // create an IntList by reading values from a file
  51. // assume that the file is open for reading
  52. IntList getIntList(FILE *inf)
  53. {
  54. IntList L;
  55. int v;
  56.  
  57. L = newIntList();
  58. while (fscanf(inf,"%d",&v) != EOF)
  59. IntListInsert(L,v);
  60. return L;
  61. }
  62.  
  63. // create a new IntListNode with value v
  64. // (this function is local to this ADT)
  65. static struct IntListNode *newIntListNode(int v)
  66. {
  67. struct IntListNode *n;
  68.  
  69. n = malloc(sizeof (struct IntListNode));
  70. assert(n != NULL);
  71. n->data = v;
  72. n->next = NULL;
  73. return n;
  74. }
  75.  
  76. // apppend one integer to the end of a list
  77. void IntListInsert(IntList L, int v)
  78. {
  79. struct IntListNode *n;
  80.  
  81. assert(L != NULL);
  82. n = newIntListNode(v);
  83. if (L->first == NULL)
  84. L->first = L->last = n;
  85. else {
  86. L->last->next = n;
  87. L->last = n;
  88. }
  89. L->size++;
  90. }
  91.  
  92. // insert an integer into correct place in a sorted list
  93. void IntListInsertInOrder(IntList L, int v)
  94. {
  95. struct IntListNode *curr, *prev, *secLast;
  96. //printf("Part 0\n");
  97.  
  98.  
  99.  
  100. assert(IntListIsSorted(L) != 0);
  101.  
  102. IntListInsert(L, v);
  103.  
  104. prev = NULL;
  105. curr = L->first;
  106. secLast = L->first;
  107. //printf("Part 1 \n");
  108.  
  109. while ( curr != NULL && curr->data <=v && curr->next!=L->last){ //finds position for swapping
  110.  
  111. prev = curr;
  112. curr = curr->next;
  113. //printf("Part 2\n");
  114. }
  115.  
  116. while( secLast != NULL && secLast->next != L->last){ //anchor to close list/make it point to NULL
  117. secLast = secLast->next;
  118. }
  119. //printf("Part 3\n");
  120.  
  121.  
  122.  
  123. if (curr != NULL && curr->data > v) { //actually swapping/moving nodes
  124. if ( prev == NULL){
  125.  
  126. //printf("part 3.0\n");
  127. L->first = L->last;
  128. //printf("part 3.1\n");
  129. L->last->next = curr;
  130. //printf("part3.2\n");
  131. secLast->next = NULL;
  132. L->last = secLast;
  133. //printf("Part 4\n");
  134. } else {
  135. //printf("Part 5.0\n");
  136. prev->next = L->last;
  137. //printf("Part 5.1\n");
  138. L->last->next = curr;
  139. //printf("Part 5.2\n");
  140. secLast->next = NULL;
  141. L->last = secLast;
  142. //printf("Part 5\n");
  143. }
  144. }
  145.  
  146. //updating first and last
  147.  
  148.  
  149. }
  150.  
  151. // delete first occurrence of v from a list
  152. // if v does not occur in List, no effect
  153. void IntListDelete(IntList L, int v)
  154. {
  155. struct IntListNode *curr, *prev;
  156.  
  157. assert(L != NULL);
  158.  
  159. // find where v occurs in list
  160. prev = NULL; curr = L->first;
  161. while (curr != NULL && curr->data != v) {
  162. prev = curr;
  163. curr = curr->next;
  164. }
  165. // not found; give up
  166. if (curr == NULL) return;
  167. // unlink curr
  168. if (prev == NULL)
  169. L->first = curr->next;
  170. else
  171. prev->next = curr->next;
  172. if (L->last == curr)
  173. L->last = prev;
  174. L->size--;
  175. // remove curr
  176. free(curr);
  177. }
  178.  
  179. // return number of elements in a list
  180. int IntListLength(IntList L)
  181. {
  182. assert(L != NULL);
  183. return L->size;
  184. }
  185.  
  186. // make a physical copy of a list
  187. // new list looks identical to original list
  188. IntList IntListCopy(IntList L)
  189. {
  190. struct IntListRep *Lnew;
  191. struct IntListNode *curr;
  192.  
  193. Lnew = newIntList();
  194. for (curr = L->first; curr != NULL; curr = curr->next)
  195. IntListInsert(Lnew, curr->data);
  196. return Lnew;
  197. }
  198.  
  199. // make a sorted physical copy of a list
  200. IntList IntListSortedCopy(IntList L)
  201. {
  202. struct IntListRep *Lnew;
  203. struct IntListNode *curr;
  204.  
  205. Lnew = newIntList();
  206. for (curr = L->first; curr != NULL; curr = curr->next)
  207. IntListInsertInOrder(Lnew, curr->data);
  208. return Lnew;
  209. }
  210.  
  211. // check whether a list is sorted in ascending order
  212. // returns 0 if list is not sorted, returns non-zero if it is
  213. int IntListIsSorted(IntList L)
  214. {
  215. struct IntListNode *curr;
  216.  
  217. assert(L != NULL);
  218. // trivial cases, 0 or 1 items
  219. if (L->size < 2)
  220. return 1;
  221. // scan list, looking for out-of-order pair
  222. for (curr = L->first; curr->next != NULL; curr = curr->next) {
  223. if (curr->next->data < curr->data)
  224. return 0;
  225. }
  226. // nothing out-of-order, must be sorted
  227. return 1;
  228. }
  229.  
  230. // check sanity of an IntList (for debugging)
  231. int IntListOK(IntList L)
  232. {
  233. struct IntListNode *p;
  234. int count;
  235.  
  236. if (L == NULL)
  237. return 1;
  238. if (L->size == 0)
  239. return (L->first == NULL && L->last == NULL);
  240.  
  241. // scan to (but not past) last node
  242. count = 1; // at least one node
  243. for (p = L->first; p->next != NULL; p = p->next)
  244. count++;
  245.  
  246. return (count == L->size && p == L->last);
  247. }
  248.  
  249. // display list as one integer per line to a file
  250. // assume that the file is open for writing
  251. void IntListPrint(FILE *outf, IntList L)
  252. {
  253. struct IntListNode *curr;
  254.  
  255. assert(L != NULL);
  256. for (curr = L->first; curr != NULL; curr = curr->next)
  257. printf("%d\n", curr->data);
  258. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement