Posted By

rtperson on 12/02/12


Tagged


Versions (?)

Linked List in C


 / Published in: Haskell
 

I bit of overkill for USACO's "Milk2" algorithm training problem. This problem would be better suited to an interval tree, but here it is.

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. typedef struct timeNode timeNode;
  5. struct Node {
  6. int begin; /* i.e., the data part */
  7. int end;
  8. timeNode *next; /* ptr to the next element */
  9. };
  10.  
  11. timeNode* addNode(timeNode *last, int begin, int end) {
  12. timeNode *node;
  13. node = (timeNode*)malloc(sizeof(timeNode));
  14. node->begin = begin;
  15. node->end = end;
  16. node->next = NULL;
  17. if (last != NULL) {
  18. last->next = node;
  19. }
  20. return node;
  21.  
  22. }
  23.  
  24. void insertNode(timeNode *before, int begin, int end) {
  25. timeNode *add = (timeNode*)malloc(sizeof(timeNode));
  26. add->begin = begin;
  27. add->end = end;
  28. add->next = before->next;
  29. before->next = add;
  30. }
  31.  
  32. timeNode* insertBefore(timeNode *before, int begin, int end) {
  33. timeNode *add = (timeNode*)malloc(sizeof(timeNode));
  34. add->begin = begin;
  35. add->end = end;
  36. add->next = before;
  37. return add;
  38. }
  39.  
  40.  
  41. timeNode* deleteNode(timeNode *toDelete) {
  42. timeNode *retval = toDelete->next;
  43. free(toDelete);
  44. return retval;
  45.  
  46. }

Report this snippet  

You need to login to post a comment.