Advertisement
Pweebs

Untitled

Nov 16th, 2019
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.97 KB | None | 0 0
  1. // Jevin Olano
  2. // jolano
  3. // CSE 101
  4. // November 9, 2019
  5. // List.c - implementation file for List ADT
  6.  
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <assert.h>
  10.  
  11. const int tableSize = 101;
  12.  
  13. #include "List.h"
  14.  
  15. // Constructors & Destructors ---------------------------
  16.  
  17. // newNode()
  18. // constructor of the Node type
  19. Node newNode(long d) {
  20. Node N = malloc(sizeof(NodeObj));
  21. assert(N != NULL);
  22. N->data = d;
  23. N->next = NULL;
  24. N->prev = NULL;
  25. return N;
  26. }
  27.  
  28. // freeNode()
  29. // frees heap memory pointed to by *pN, sets *pN to NULL
  30. void freeNode(Node *pN) {
  31. if (pN != NULL && *pN != NULL) {
  32. free(*pN);
  33. *pN = NULL;
  34. }
  35. }
  36.  
  37. // newList()
  38. // returns reference to new empty List object.
  39. List newList() {
  40. List L;
  41. L = malloc(sizeof(ListObj));
  42. L->front = NULL;
  43. L->back = NULL;
  44. L->cursor = NULL;
  45. L->length = 0;
  46. L->index = -1;
  47. return L;
  48. }
  49.  
  50. // freeList()
  51. // frees heap memory associated with List *pL, sets *pL to NULL
  52. void freeList(List *pL) {
  53. if (pL != NULL && *pL != NULL) {
  54. clear(*pL);
  55. free(*pL);
  56. *pL = NULL;
  57. }
  58. }
  59.  
  60. // Access Functions -------------------------------------
  61.  
  62. // length()
  63. // returns the number of elements in L
  64. int length(List L) {
  65. if (L == NULL) {
  66. fprintf(stderr, "Error: calling size() on NULL List reference\n");
  67. exit(1);
  68. }
  69. return L->length;
  70. }
  71.  
  72. // index()
  73. // Returns the index of cursor element if defined, returns -1 otherwise
  74. int index(List L) {
  75. if (L == NULL) {
  76. fprintf(stderr, "Error: calling index() on NULL List reference\n");
  77. exit(1);
  78. }
  79. return L->index;
  80. }
  81.  
  82. // front()
  83. // Returns the front element of L
  84. // pre: length() > 0
  85. long front(List L) {
  86. if (L == NULL) {
  87. fprintf(stderr, "Error: calling front() on NULL List reference\n");
  88. exit(1);
  89. }
  90. if (isEmpty(L)) {
  91. fprintf(stderr, "Error: calling front() on an empty List\n");
  92. exit(1);
  93. }
  94. Node temp = L->front;
  95. return temp->data;
  96. }
  97.  
  98. // back()
  99. // Returns the back element of L
  100. // pre: length() > 0
  101. long back(List L) {
  102. if (L == NULL) {
  103. fprintf(stderr, "Error: calling back() on NULL List reference\n");
  104. exit(1);
  105. }
  106. if (isEmpty(L)) {
  107. fprintf(stderr, "Error: calling back() on an empty List\n");
  108. exit(1);
  109. }
  110. Node temp = L->back;
  111. return temp->data;
  112. }
  113.  
  114. // get()
  115. // Returns cursor element of L
  116. // pre: length() > 0
  117. long get(List L) {
  118. if (L == NULL) {
  119. fprintf(stderr, "Error: calling get() on NULL List reference\n");
  120. exit(1);
  121. }
  122. if (isEmpty(L)) {
  123. fprintf(stderr, "Error: calling get() on an empty List\n");
  124. exit(1);
  125. }
  126. if (L->cursor == NULL) {
  127. return -1;
  128. }
  129. Node temp = L->cursor;
  130. return temp->data;
  131. }
  132.  
  133. // isEmpty()
  134. // Returns true (1) if L is empty, otherwise returns false (0)
  135. int isEmpty(List L) {
  136. if (L == NULL) {
  137. fprintf(stderr, "Error: calling isEmpty() on NULL List reference\n");
  138. exit(1);
  139. }
  140. return L->length==0;
  141. }
  142.  
  143. /*// equals()
  144. // Returns true(1) iff Lists A and B are in same state, and returns false(0) otherwise
  145. int equals(List A, List B) {
  146. int eq = 0;
  147. Node nodeA = NULL;
  148. Node nodeB = NULL;
  149. if (A == NULL || B == NULL) {
  150. fprintf(stderr, "Error: calling equals() on NULL List reference\n");
  151. exit(1);
  152. }
  153. eq = (A->length == B->length);
  154. nodeA = A->front;
  155. nodeB = B->front;
  156. while (eq && nodeA != NULL) {
  157. eq = (nodeA->data == nodeB->data);
  158. nodeA = nodeA->next;
  159. nodeB = nodeB->next;
  160. }
  161. return eq;
  162. } */
  163.  
  164. // Manipulation Procedures ------------------------------
  165.  
  166. // clear()
  167. // Resets L to its original empty state
  168. void clear(List L) {
  169. while (!isEmpty(L)) {
  170. deleteFront(L);
  171. }
  172. L->cursor = NULL;
  173. L->index = -1;
  174. }
  175.  
  176. // moveFront()
  177. // If L is non-empty, sets cursor under the front element, otherwise does nothing
  178. void moveFront(List L) {
  179. if (!isEmpty(L)) {
  180. L->cursor = L->front;
  181. L->index = 0;
  182. }
  183. }
  184.  
  185. // moveBack()
  186. // If L is non-empty, sets cursor under the back element, otherwise does nothing
  187. void moveBack(List L) {
  188. if (!isEmpty(L)) {
  189. L->cursor = L->back;
  190. L->index = L->length - 1;
  191. }
  192. }
  193.  
  194. // movePrev()
  195. // If cursor is defined and not at front, move cursor one step toward the front of L;
  196. // if cursor is defined and at front, cursor becomes undefined;
  197. // if cursor is undefined do nothing
  198. void movePrev(List L) {
  199. if (L->cursor != NULL) {
  200. L->cursor = L->cursor->prev;
  201. L->index--;
  202. }
  203. }
  204.  
  205. // moveNext()
  206. // If cursor is defined and not at back, move cursor one step toward the back of L;
  207. // if cursor is defined and at back, cursor becomes undefined;
  208. // if cursor is undefined do nothing
  209. void moveNext(List L) {
  210. if (L->cursor != NULL) {
  211. L->cursor = L->cursor->next; // if cursor is on last Node, this automatically makes it null
  212. if (L->cursor != NULL) {
  213. L->index++;
  214. } else {
  215. L->index = -1;
  216. }
  217. }
  218. }
  219.  
  220. // prepend()
  221. // Insert new element into L
  222. // If L is non-empty, insertion takes place before front element
  223. void prepend(List L, long data) {
  224. Node node = newNode(data);
  225. if (isEmpty(L)) {
  226. L->front = node;
  227. L->back = node;
  228. } else {
  229. L->front->prev = node;
  230. node->next = L->front;
  231. L->front = node;
  232. }
  233. L->length++;
  234. if (L->cursor != NULL) {
  235. L->index++;
  236. }
  237. }
  238.  
  239. // append()
  240. // Insert new element into L
  241. // If L is non-empty, insertion takes place after back element
  242. void append(List L, long data) {
  243. Node node = newNode(data);
  244. if (isEmpty(L)) {
  245. L->front = node;
  246. L->back = node;
  247. } else {
  248. L->back->next = node;
  249. node->prev = L->back;
  250. L->back = node;
  251. }
  252. L->length++;
  253. }
  254.  
  255. // insertBefore()
  256. // Insert new element before cursor
  257. // pre: length() > 0, index() >= 0
  258. void insertBefore(List L, long data) {
  259. if (L == NULL) {
  260. fprintf(stderr, "Error: calling insertBefore() on NULL List reference\n");
  261. exit(1);
  262. }
  263. Node node = newNode(data);
  264. node->prev = L->cursor->prev;
  265. node->next = L->cursor;
  266. if (node->prev != NULL) {
  267. L->cursor->prev->next = node;
  268. } else {
  269. L->front = node;
  270. }
  271. L->cursor->prev = node;
  272. L->length++;
  273. L->index++;
  274. }
  275.  
  276. // insertAfter()
  277. // Insert new element after cursor
  278. // pre: length() > 0, index() >= 0
  279. void insertAfter(List L, long data) {
  280. if (L == NULL) {
  281. fprintf(stderr, "Error: calling insertAfter() on NULL List reference\n");
  282. exit(1);
  283. }
  284. Node node = newNode(data);
  285. node->next = L->cursor->next;
  286. node->prev = L->cursor;
  287. if (node->next != NULL) {
  288. L->cursor->next->prev = node;
  289. } else {
  290. L->back = node;
  291. }
  292. L->cursor->next = node;
  293. L->length++;
  294. }
  295.  
  296. // set()
  297. // Overwrites the cursor element with x. Pre: length()>0, index()>=0
  298. void set(List L, long x) {
  299. if (L->cursor != NULL) {
  300. L->cursor->data = x;
  301. }
  302. }
  303.  
  304. // deleteFront()
  305. // Delete the front element
  306. // pre: length() > 0
  307. void deleteFront(List L) {
  308. if (L == NULL) {
  309. fprintf(stderr, "Error: calling deleteFront() on NULL List reference\n");
  310. exit(1);
  311. }
  312. if (L->length == 0) {
  313. fprintf(stderr, "Error: calling deleteFront() on empty List reference\n");
  314. exit(1);
  315. }
  316. if (L->length == 1) {
  317. Node temp = L->front;
  318. free(temp);
  319. L->front = NULL;
  320. L->back = NULL;
  321. L->cursor = NULL;
  322. L->index = -1;
  323. } else {
  324. Node temp = L->front;
  325. L->front = L->front->next;
  326. L->front->prev = NULL;
  327. if (L->cursor != NULL) {
  328. L->index--;
  329. }
  330. free(temp);
  331. }
  332. L->length--;
  333. }
  334.  
  335. // deleteBack()
  336. // Delete the back element
  337. // pre: length() > 0
  338. void deleteBack(List L) {
  339. if (!isEmpty(L)) {
  340. Node back = L->back;
  341. L->back = L->back->prev;
  342. if (L->back != NULL) {
  343. L->back->next = NULL;
  344. }
  345. L->length--;
  346. if (L->cursor == back) {
  347. L->cursor = NULL;
  348. L->index = -1;
  349. }
  350. freeNode(&back);
  351. }
  352. }
  353.  
  354. // delete()
  355. // Delete cursor element, making cursor undefined
  356. // pre: length() > 0, index() >= 0
  357. void delete(List L) {
  358. if (L->cursor != NULL) {
  359. if (L->length == 0) { // one element in the List
  360. clear(L);
  361. } else if (L->cursor == L->front) { // cursor is in front
  362. deleteFront(L);
  363. } else if (L->cursor == L->back) { // cursor is in back
  364. deleteBack(L);
  365. } else { // cursor is in the middle somewhere
  366. L->cursor->prev->next = L->cursor->next;
  367. L->cursor->next->prev = L->cursor->prev;
  368. freeNode(&(L->cursor));
  369. L->cursor = NULL;
  370. L->index = -1;
  371. L->length--;
  372. }
  373. }
  374. }
  375.  
  376. // Other Operations -------------------------------------
  377.  
  378. // printList()
  379. // Prints to the file pointed to by out,
  380. // a string representation of L consisting of a space separated sequence of integers,
  381. // with front on left
  382. void printList(FILE* out, List L) {
  383. if (!isEmpty(L)) {
  384. Node node = L->front;
  385. while (node != NULL) {
  386. if (node == L->front) {
  387. fprintf(out, "%ld", node->data);
  388. } else {
  389. fprintf(out, " %ld", node->data);
  390. }
  391. node = node->next;
  392. }
  393. }
  394. }
  395.  
  396. // copyList()
  397. // Returns a new List representing the same integer sequence as L.
  398. // The cursor in the new list is undefined, regardless of the state of the cursor in L.
  399. // The state of L is unchanged.
  400. List copyList(List L) {
  401. List newL = newList();
  402. if (isEmpty(L)) {
  403. return newL;
  404. }
  405. moveFront(L);
  406. Node temp = L->cursor;
  407. while (temp != NULL) {
  408. append(newL, temp->data);
  409. moveNext(L);
  410. temp = L->cursor;
  411. }
  412. return newL;
  413. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement