• 链表基本操作


    #include<stdio.h>
    #include<stdlib.h>
    
    struct list
    {
        int data;
        struct list *next;
    };
    
    //建立链表节点
    struct list *create_list()
    {
        return calloc(sizeof(struct list),1);
    }
    
    //往链表的第n个节点插入新节点
    struct list *insert_list(struct list *ls,int n,int data)
    {
        struct list *p = ls;
        while(p && n--)
        {
            p = p->next;
        }
        if(NULL == p)
        {
            return NULL;
        }
        struct list *node = create_list();
        node->data = data;
        node->next = p->next;
        p->next = node;
        return node;
    }
    
    //删除第n个节点 
    int delete_list(struct list *ls,int n)
    {
        struct list *p = ls;
        while(p && n--)
        {
            p = p->next;
        }
    
        if(NULL == p)
        {
            return -1;
        }
        
        struct list *temp = p->next;
        p->next = temp->next;
        free(temp);
        return 0;
    }
    
    //删除链表只留头节点
    void clear_list(struct list *ls)
    {
        struct list *p = ls->next;
        while(p)
        {
            struct list *tmp = p->next;
            free(p);
            p = tmp;
        }
        ls->next = NULL;
    }
    
    //判断链表是否为空
    int empty_list(struct list *ls)
    {
        if(ls->next)
        {
            return 0;
        }
        else
        {
            return -1;
        }
    }
    
    //定位第n个节点
    struct list *locale_list(struct list *ls,int n)
    {
        struct list *p = ls;
        while(p && n--)
        {
            p = p->next;
        }
        if(p == NULL)
        {
            return NULL;
        }
        
        return p;
    }
    
    //查找值为data的节点
    struct list *elem_list(struct list *ls,int data)
    {
        struct list *p = ls;
        while(p)
        {
            if(p->data == data)
                return p;
            p = p->next;
        }
        return NULL;
    }
    
    //查找值为data节点的下标
    int elem_pos(struct list *ls,int data)
    {
        int index = 0;
        struct list *p = ls;
        while(p)
        {
            index++;
            if(p->data == data)
                return index;
            p = p->next;
        }
        return -1;    
    }
    
    //统计链表长度
    int count_list(struct list *ls)
    {
        struct list *p = ls;
        int count = 0;
        while(p)
        {
            count++;
            p = p->next;
        }
        return count;
    }
    
    //返回链表最后一个节点
    struct list *last_list(struct list *ls)
    {
        struct list *p = ls;
        while(p->next)
        {
            p = p->next;
        }
        return p;
    }
    
    //合并两个节点
    void merge_list(struct list *ls1,struct list *ls2)
    {
        last_list(ls1)->next = ls2->next;
    }
    
    //遍历打印整个链表
    void traverse(struct list *ls)
    {
        struct list *p = ls;
        while(p)
        {
            printf("%d
    ",p->data);
            p = p->next;
        }
    }
    
    int main()
    {
        struct list *first = create_list();
        struct list *second = create_list();
        struct list *third = create_list();
    
        first->next = second;
        second->next = third;
        third->next = NULL;
    
        first->data = 1;
        second->data = 2;
        third->data = 3;
    
        insert_list(first,1,10);
        insert_list(first,1,20);
        insert_list(first,1,30);
        delete_list(first,2);
    
        //clear_list(first);
    
        traverse(first);
        printf("count = %d
    ",count_list(first));
    
        printf("data = %d
    ",locale_list(first,3)->data);
        printf("last data = %d
    ",last_list(first)->data);
    
        struct list *first1 = create_list();
        int i;
        for(i = 0; i < 10; i++)
        {
            insert_list(first1,0,i);
        }        
        merge_list(first,first1);
        traverse(first);
        return 0;
    }
  • 相关阅读:
    367 Valid Perfect Square 有效的完全平方数
    365 Water and Jug Problem 水壶问题
    363 Max Sum of Rectangle No Larger Than K 最大矩阵和不超过K
    357 Count Numbers with Unique Digits 计算各个位数不同的数字个数
    SpringBoot (四) :thymeleaf 使用详解
    SpringBoot(三) :Spring boot 中 Redis 的使用
    SpringBoot(二) :web综合开发
    SpringBoot (一) :入门篇
    程序员最核心的竞争力是什么?
    Java面试题:多继承
  • 原文地址:https://www.cnblogs.com/wanghao-boke/p/11271275.html
Copyright © 2020-2023  润新知