码迷,mamicode.com
首页 > 其他好文 > 详细

用单链表实现一个模糊搜索, 待补充

时间:2016-05-05 22:09:27      阅读:263      评论:0      收藏:0      [点我收藏+]

标签:

 

 

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

typedef struct node_s{
        char *data;
        struct node_s *next;
}node_t;

node_t *create(){
        node_t *p = calloc(1, sizeof(node_t));
        return p;
}


int insert(node_t *head, const char *data){
        node_t *new = create();
        if(!new){
                return -1;
        }
        new->data = strdup(data);
        new->next = head->next;
        head->next = new;
        return 0;
}

int clear(node_t *head){
        node_t *p_next;
        if(!head){
                return -1;
        }
        while(head->next != NULL){
                p_next = head->next;
                if(head->data){
                        free(head->data);
                }
                free(head);
                head=p_next;
        }
        return 0;
}

#define foreach(head, a_unit)         for(head = head->next, a_unit = head; head != NULL; head = head->next, a_unit = head)

int main(){
        node_t *head = create();
        insert(head, "a");
        insert(head, "b");
        insert(head, "c");

        node_t *a_unit; 
        foreach(head, a_unit){
                printf("%s\n", a_unit->data);
        }

        clear(head);
}

 

用单链表实现一个模糊搜索, 待补充

标签:

原文地址:http://www.cnblogs.com/bai-jimmy/p/5463218.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!