栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

企业级链表

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

企业级链表

//运行环境vs2019,vc6.0不可用

#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
struct  linknode
{
    struct  linknode* next;
};
struct linklist
{
    struct  linknode pheader;
    int size;
};
//初始化
struct linklist* firstlist()
{
    struct linklist* mylist=(struct linklist*)malloc(sizeof(struct linklist));
    mylist->pheader.next = NULL;
    mylist->size = 0;
    return mylist;
}

//插入
void addlist(struct linklist* mylist, int pos, struct linknode* address)
{
    if (mylist != NULL && address != NULL)
    {
        if (pos <= 0)
        {
            pos = 1;
        }
        if (pos > mylist->size)
        {
            pos = mylist->size + 1;
        }
        struct linknode* temp = &mylist->pheader;
        for(int i = 1; i < pos; i++)
        {
            temp = temp->next;
        }
        address->next = temp->next;
        temp->next = address;
        mylist->size++;
    }
}
//删除
void delet(struct linklist* mylist, int pos)
{
    struct linknode* temp = &mylist->pheader;
    for (int i = 0; i < pos; i++)
    {
        temp = temp->next;
    }
    temp->next = temp->next->next;
    mylist->size--;
}
//查找
int find(struct linklist* mylist,struct linknode* adderss, int(*compare)(struct linknode*, struct linknode*))
{
    int i = 1;
    if (mylist == NULL && adderss == NULL && compare == NULL)
    {
        return 0;
    }
    struct  linknode* temp = mylist->pheader.next;
    while (compare(temp, adderss) == 0)
    {
        temp=temp->next;
        i++;
    }
    return i;
}
//遍历
void scanlist(struct linklist* mylist, void(*myprintf)(struct linknode*))
{
    if (mylist == NULL && myprintf == NULL)
    {
        return;
    }
    struct linknode* temp = mylist->pheader.next;
    while (temp)
    {
        myprintf(temp);
        temp=temp->next;

    }
    return;
}
//用户自定义
typedef struct Student
{
    struct linknode node;
    int name;
    int age;
}Student;
//打印回调函数
void myprintf(struct linknode* node)
{
    Student* stu = (Student*)node;
    printf("姓名:%d,年龄%dn", stu->name, stu->age);
    return;
}
//比较回调函数
int compare(struct linknode* node1, struct linknode* node2)
{
    Student* stu1 = (Student*)node1;
    Student* stu2 = (Student*)node2;
    if (stu1->age == stu2->age && stu1->name==stu2->name)
    {
        return 1;
    }
    return 0;
}

int main()
{
//自定义
    Student s1, s2, s3, s4, s5;
    s1.age = 11;
    s2.age = 12;
    s3.age = 13;
    s4.age = 14;
    s5.age = 15;
    s1.name = 1;
    s2.name = 2;
    s4.name = 4;
    s3.name = 3;
    s5.name = 5;
    //创建链表
    struct linklist* mylist = firstlist();

    //插入
    addlist(mylist, 1, (struct linknode*)&s1);
    addlist(mylist, 2, (struct linknode*)&s2);
    addlist(mylist, 3, (struct linknode*)&s3);
    addlist(mylist, 4, (struct linknode*)&s4);
    addlist(mylist, 5, (struct linknode*)&s5);
    scanlist(mylist,myprintf);

    //查找
    Student s6;
    s6.age = 15;
    s6.name = 5;
    int location = find(mylist, (struct linknode*)&s6, compare);
    printf("第%d个元素n", location);

    //删除元素
    delet (mylist, 1);

    printf("删除后:n");
    scanlist(mylist,myprintf);
    return 0;
}

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/690324.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号