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

 

 

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

 

转载于:https://www.cnblogs.com/bai-jimmy/p/5463218.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值