Advertisement
Guest User

Untitled

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