Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

feel thread

Name: Anonymous 2012-06-03 13:58

that feel when your handwritten linked_list in C is 5 seconds faster than the STL

Name: Anonymous 2012-06-03 16:14

critique


#include <stdio.h>
#include <string.h>
#include <malloc.h>

struct node_t
{
    int key;
    struct node_t * next;
};

typedef struct node_t node_t;

void list_init(node_t ** head, node_t ** tail, int head_key)
{
    *head = (node_t *) malloc(sizeof(node_t));
    *tail = (node_t *) malloc(sizeof(node_t));

    (**head).next = *tail;
    (**tail).next = *tail;

    (**head).key = head_key;
    (**tail).key = 0;
}

void list_destroy(node_t ** head, node_t ** tail)
{
    node_t * iter = NULL, * temp_next = NULL;

    //traverse and free
    for(iter = *head; iter != *tail; iter = temp_next)
    {
        temp_next = iter->next;
       
        free(iter);
        iter = NULL;
    }
}

node_t * list_insert_after(int key, node_t * before_this)
{
    node_t * this_list = (node_t *) malloc(sizeof(node_t));
    this_list->key = key;
   
    this_list->next = before_this->next;
    before_this->next = this_list;

    return this_list;
}

void list_remove_after(node_t ** before_removed)
{
    node_t * to_remove = (**before_removed).next;
   
    (**before_removed).next = to_remove->next;
    free(to_remove);
}


int main()
{
    node_t * head=NULL, * tail=NULL, * iter=NULL,
        * node1=NULL, * node2=NULL, * node3=NULL, * node4=NULL;
   
    list_init(&head, &tail, 5);
    node1 = list_insert_after(2, head);
    node2 = list_insert_after(9, node1);
    node3 = list_insert_after(7, node2);
    node4 = list_insert_after(8, node3);
    //list_remove_after(&node1);
    for(iter = head; iter != tail; iter = iter->next)
    {
        iter->key++;
    }
    //printf("%d\n", head->next->key);
    list_destroy(&head, &tail);
    return 0;
}

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List