Return to Snippet

Revision: 61176
at December 2, 2012 12:32 by rtperson


Initial Code
#include <stdio.h>
#include <stdlib.h>

typedef struct timeNode timeNode;
struct Node {
    int begin;  /* i.e., the data part */
    int end;
    timeNode *next;  /* ptr to the next element */
};

timeNode* addNode(timeNode *last, int begin, int end) {
    timeNode *node;
    node = (timeNode*)malloc(sizeof(timeNode));
    node->begin = begin;
    node->end = end;
    node->next = NULL;
    if (last != NULL) {
        last->next = node;
    }
    return node;
    
}

void insertNode(timeNode *before, int begin, int end) {
    timeNode *add = (timeNode*)malloc(sizeof(timeNode));
    add->begin = begin;
    add->end = end;
    add->next = before->next;
    before->next = add;
}

timeNode* insertBefore(timeNode *before, int begin, int end) {
    timeNode *add = (timeNode*)malloc(sizeof(timeNode));
    add->begin = begin;
    add->end = end;
    add->next = before; 
    return add;
}


timeNode* deleteNode(timeNode *toDelete) {
    timeNode *retval = toDelete->next;
    free(toDelete);
    return retval;
    
}

Initial URL

                                

Initial Description
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.

Initial Title
Linked List in C

Initial Tags

                                

Initial Language
Haskell