美文网首页
C语言中链表的增删改查

C语言中链表的增删改查

作者: 湾里有桃树 | 来源:发表于2018-07-24 17:38 被阅读65次

    一个简单的C指针链表,希望对某些小伙伴有所帮助:

    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    struct node{
      
        int id; //数据域
        char *name;
        struct node *next;//指针域
    };
    
    //类型定义
    typedef struct node node_t;
    
    //创建节点
    node_t *make_node(int id, char *name);
    //添加节点,从尾部添加
    void llist_append_node(node_t *head, node_t *new);
    //添加节点,从头部添加
    void llist_append_head(node_t *head, node_t *new);
    //遍历打印列表
    void llist_print_each(node_t *head);
    //将节点添加指定节点的后面
    void llist_insert_node_after_by_id(node_t *head,node_t *new, int id);
    //根据id查找节点
    node_t *llist_search_node_by_id(node_t *head, int id);
    //根据名字查询节点
    node_t *llist_search_node_by_name(node_t *head, char *name);
    //根据id找到改节点的上一个节点
    node_t *llist_search_node_by_prev(node_t *head, int id);
    //根据id删除节点
    void llist_delete_node_by_id(node_t *head, int id);
    
    int main(int argc, char *argv[])
    {
        //创建头结点
        node_t *head = make_node(-1,"head");
        
        //创建4个结点
        node_t *n1 = make_node(1,"name1");
        node_t *n2 = make_node(2,"name2");
        node_t *n3 = make_node(3,"name3");
        node_t *n4 = make_node(4,"name4");
        
        //添加结点n1->n2->n3->n4
        llist_append_node(head,n1);
        llist_append_node(head,n2);
        llist_append_node(head,n3);
        llist_append_node(head,n4);
        //添加节点 n4->n3->n2->n1
    //    llist_append_head(head,n1);
    //    llist_append_head(head,n2);
    //    llist_append_head(head,n3);
    //    llist_append_head(head,n4);
        
        //根据id插入结点,把第5个结点插入到id为2的结点后面
        node_t *n5 = make_node(5,"name5");
        llist_insert_node_after_by_id(head,n5,2);
        
        //根据id插入结点,把第7个结点插入到id为6的结点后面
        node_t *n7 = make_node(7,"name7");
        llist_insert_node_after_by_id(head,n7,6);
        
        //根据id删除节点4
        llist_delete_node_by_id(head, 4);
        
        //打印链表内容
        llist_print_each(head);
        return 0;
    }
    
    //创建节点
    node_t *make_node(int id, char *name)
    {
        node_t *ret = (node_t *)malloc(sizeof(node_t));
        ret->id = id;
        ret->name = name;
        ret->next = NULL;
        return ret;
    }
    
    //添加节点,从尾部添加
    void llist_append_node(node_t *head, node_t *new)
    {
        node_t *cur = head;
        //找到最后的节点
        while (cur->next != NULL) {
            cur = cur->next;
        }
        cur->next = new;
    }
    //添加节点,从头部添加
    void llist_append_head(node_t *head, node_t *new)
    {
        node_t *cur = head;
        if (cur != NULL) {
            
            new->next = cur->next;
            cur->next = new;
        }
    }
    //根据id查找节点
    node_t *llist_search_node_by_id(node_t *head, int id)
    {
        node_t *cur = head;
        while (cur != NULL) {
            
            if (cur->id == id) {
                return cur;
            }
            cur = cur->next;
        }
        return NULL;
    }
    
    //将节点添加指定节点的后面
    void llist_insert_node_after_by_id(node_t *head,node_t *new, int id)
    {
        node_t *cur = llist_search_node_by_id(head,id);
        if (cur != NULL) {
            new->next = cur->next;
            cur->next = new;
        }else{
            llist_append_node(head,new);
        }
    }
    
    //根据名字查询节点
    node_t *llist_search_node_by_name(node_t *head, char *name)
    {
        node_t *cur = head;
        while (cur != NULL) {
            
            if (strcmp(cur->name,name) == 0)
            {
                return cur;
            }
            cur = cur->next;
        }
        return NULL;
    }
    
    //根据id找到改节点的上一个节点
    node_t *llist_search_node_by_prev(node_t *head, int id)
    {
        node_t *cur = head;
        while (cur->next != NULL) {
            
            if (cur->next->id == id) {
                printf("=====找到了\n");
                return cur;
            }
            cur = cur->next;
        }
        printf("=====没有找到了\n");
        return NULL;
    }
    //根据id删除节点
    void llist_delete_node_by_id(node_t *head, int id)
    {
        node_t *prev_node = llist_search_node_by_prev(head,id);
        if (prev_node != NULL) {
            printf("找到了前面的节点\n");
            //先保存要删除的节点
            node_t *saveNode = prev_node->next;
            prev_node->next = saveNode->next;
            saveNode->next = NULL;
            free(saveNode);
        }
    }
    
    //遍历打印列表
    void llist_print_each(node_t *head)
    {
        if (head == NULL) {
            return;
        }
        node_t *cur = head->next;
        while (cur != NULL) {
            printf("%p| %d | %s| %p->\n",cur,cur->id,cur->name,cur->next);
            cur = cur->next;
        }
    }
    

    最后的结果:

    liaoshuhua:c语言 liaoshuhua$ gcc list.c
    liaoshuhua:c语言 liaoshuhua$ ./a.out
    0x7fba0fc02760| 1 | name1| 0x7fba0fc02780->
    0x7fba0fc02780| 2 | name2| 0x7fba0fc027e0->
    0x7fba0fc027e0| 5 | name5| 0x7fba0fc027a0->
    0x7fba0fc027a0| 3 | name3| 0x7fba0fc02800->
    0x7fba0fc02800| 7 | name7| 0x0->
    liaoshuhua:c语言 liaoshuhua$ 
    

    相关文章

      网友评论

          本文标题:C语言中链表的增删改查

          本文链接:https://www.haomeiwen.com/subject/wesymftx.html