• 列表顺序储存


    sqlist.h

    /*
    *列表顺序储存
    */
    
    #ifndef __SQLIST_H_
    #define __SQLIST_H_
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    //隐藏真实的类型
    typedef void Listing;
    typedef void ListingNode;
    
    //实现一个句柄 储存信息
    Listing* list_init(int n);
    //列表插入元素 n从0开始
    int list_insert(Listing* list, ListingNode* listnode, int n);
    //销毁内存
    void list_destroy(Listing* list);
    //清空列表
    void list_clear(Listing* list);
    //获得某个位置的结点
    ListingNode* list_catch(Listing* list,int n);
    //删除某个位置结点
    ListingNode* list_delete(Listing* list, int n);
    //返回列表长度
    int list_size(Listing* list);
    //返回列表容量
    int list_capacity(Listing* list);
    
    
    #endif // !__SQLIST_H_

    sqlist.c

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include "sqlist.h"
    //定义一个结构体用作句柄
    typedef struct SQMLIST
    {
        int capacity;
        int size;
        int * node;
    }thList;
    
    void* list_init(int n)
    {
        thList * tl = NULL;
        //传入无效数字
        if (n <= 0)
        {
            return NULL;
        }
        //开辟内存空间
        tl = (thList*)malloc(sizeof(thList)+n*sizeof(int *));
        if (!tl)
        {
            return NULL;
        }
        tl->capacity = n;
        tl->size = 0;
        //tl为thList类型 步长为thlist
        tl->node = (int*)(tl+1);
        return tl;
    }
    int list_insert(Listing* list, ListingNode* listnode, int n)
    {
        int i = 0;
        thList * tl = (thList*)list;
        //指针为空
        if (list == NULL || listnode == NULL)
        {
            return -1;
        }
        //插入的位置不正确
        if (n<0 || n >= tl->capacity)
        {
            return -2;
        }
        //已经满了
        if (tl->size >= tl->capacity)
        {
            return -3;
        }
        //超过长度则在后面插入
        if (n>tl->size)
        {
            n = tl->size;
        }
        //遍历后移
        for (i = tl->size; i > n; i--)
        {
            tl->node[i] = tl->node[i - 1];
        }
        //插入数据
        tl->node[n] = (int)listnode;
        //长度自增
        tl->size++;
        return 0;
    }
    void list_destroy(Listing* list)
    {
        if (list != NULL)
        {
            free(list);
        }
        return;
    }
    void list_clear(Listing* list)
    {
        if (list == NULL)
        {
            return;
        }
        thList * tl = (thList*)list;
        //归零
        tl->size = 0;
        return;
    }
    ListingNode* list_catch(Listing* list, int n)
    {
        thList * tl = (thList*)list;
        if (list == NULL || n >= tl->size || n < 0)
        {
            return NULL;
        }
        return (Listing*)tl->node[n];
    }
    ListingNode* list_delete(Listing* list, int n)
    {
        int i = 0;
        thList * tl = (thList*)list;
        ListingNode* temp = NULL;
        if (list == NULL || n >= tl->size || n < 0)
        {
            return NULL;
        }
        //保存缓存
        temp = (ListingNode*)tl->node[n];
        //遍历前移
        for ( i = n; i < tl->size; i++)
        {
            tl->node[i] = tl->node[i + 1];
        }
        //自减
        tl->size--;
        return temp;
    }
    
    int list_size(Listing* list)
    {
        if (list == NULL)
        {
            return 0;
        }
        thList * tl = (thList*)list;
    
        return tl->size;
    }
    
    int list_capacity(Listing* list)
    {
        if (list == NULL)
        {
            return 0;
        }
        thList * tl = (thList*)list;
    
        return tl->capacity;
    }

    main.c

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include "sqlist.h"
    
    typedef struct __Teacher
    {
        int age;
        char name[100];
    }Teacher;
    
    void main()
    {
        //建立一堆插入的结点
        Teacher a, b, c, d, e;
        memset(&a, 0, sizeof(Teacher));
        memset(&b, 0, sizeof(Teacher));
        memset(&c, 0, sizeof(Teacher));
        memset(&d, 0, sizeof(Teacher));
        memset(&e, 0, sizeof(Teacher));
        a.age = 1;
        b.age = 2;
        c.age = 3;
        d.age = 4;
        e.age = 5;
        memcpy(a.name, "a",1);
        memcpy(b.name, "b",1);
        memcpy(c.name, "c",1);
        memcpy(d.name, "d",1);
        memcpy(e.name, "e",1);
    
        Listing * ls = NULL;
        //创建列表
        ls = list_init(10);
        //插入数据
        list_insert(ls, (ListingNode*)&a, 0);
        list_insert(ls, (ListingNode*)&b, 0);
        list_insert(ls, (ListingNode*)&c, 0);
        list_insert(ls, (ListingNode*)&d, 0);
        list_insert(ls, (ListingNode*)&e,0);
        //打印当前长度和容量
        printf("%d:%d!
    ", list_size(ls), list_capacity(ls));
        //获取元素
        printf("%s catch successfully.
    ", ((Teacher*)list_catch(ls, 1))->name);
        //删除元素
        printf("%s delete successfully.
    ", ((Teacher*)list_delete(ls, 2))->name);
        //获取元素
        printf("%s catch successfully.
    ", ((Teacher*)list_catch(ls, 2))->name);
        //清空列表
        list_clear(ls);
        //销毁列表
        list_destroy(ls);
        system("pause");
    }
  • 相关阅读:
    element 树形控件使用
    js粘贴图片并显示
    vue-element Tree树形控件通过id默认选中
    解决win10shift+右键显示在此处打开powershell而不是命令窗口的问题
    maven安装与配置
    解决dos窗口中文乱码问题
    vue单页面应用刷新网页后vuex的state数据丢失的解决方案
    vue 单个倒计时组件
    js将秒转换为时分秒
    vue3.0用vue-awesome-swiper
  • 原文地址:https://www.cnblogs.com/godehi/p/9037575.html
Copyright © 2020-2023  润新知