DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Roger has posted 34 posts at DZone. View Full User Profile

Linked Lists In C

11.04.2008
| 4014 views |
  • submit to reddit
        // I coded this little piece of overkill for USACO's "Milk2" algorithm training problem. 
// I'm leaving out the actual solution to the problem, but am leaving in the basic 
// manipulations to the linked list itself.

#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;
    
}