C语言实现链表简单地实现

/*
 * link.h
 *
 *  Created on: Nov 6, 2010
 *      Author: jenson
 */

#ifndef LINK_H_
#define LINK_H_

typedef struct node *link;
typedef int item_type;
struct node{
    item_type item;
    link next;
};

extern void init_nodes(int);
extern link new_node(int);
extern void free_node(link);
extern void insert_next(link,link);
extern void delete_next(link);
extern link next(link);
extern int item(link);

#endif /* LINK_H_ */

/*
 * link.c
 *
 *  Created on: Nov 6, 2010
 *      Author: jenson
 */

#include "link.h"
link free_list;
void init_nodes(int N){
    int i ;
    free_list = malloc((N+1)*sizeof(* free_list));
    for(i = 0;i<N+1;i++){
        free_list[i]->next = &free_list[i+1];
    }
    free_list[N]->next = NULL;
}

link new_node(int i){
    link x = delete_next(free_list);
    x->item = i;
    x->next = x;
    return x;
}

void free_node(link x){
    insert_next(free_list,x);
}

void insert_next(link x,link t){
    t->next = x->next;
    x->next = t;
}

void delete_next(link x){
    link t = x->next;
    x->next = t->next;
    return t;
}

link next(link x){
    return x->next;
}

int item(link x){
    return x->item;
}

你可能感兴趣的:(算法,职场,休闲)