关于c语言中链表

关于c语言中链表的学习

萌新最近学习链表的时候,发现有很多回答都比较深奥,于是结合自己的学习分享给大家我所学的
前期所要知道的
1.1知道malloc函数的用法
https://baike.baidu.com/item/malloc函数/8582146?fr=aladdin
1.2与malloc对应的free函数的用法
https://baike.baidu.com/item/free()/9848328?fr=aladdin
1.3了解typedef(看回答人为“幻的上帝”的回答)
https://zhidao.baidu.com/question/181081049.html
1.4了解结构体
http://www.runoob.com/cprogramming/c-structures.html

2.接下来上图(注解非常细,真的)
在这里插入图片描述
2.1定义一个结构体

    typedef struct people               //定义一个简单的结构体
    {
        int a;
        people* pnext;
    }people;

2.2创建一个由用户指定大小的链表

    struct people* cre_peo()          //创建一个由用户指定节点数的链表
    {
        int num;                                 //结点数(人数)
        int cot = 0;                             //计数,当cot<num时,新建节点
        int num_instr;                        //新增节点数据的赋值,即对新增节点的变量a的赋值
        struct people* phead = (struct people*)malloc(sizeof(struct people));          //phead是头节点,只存下一个结点的地址
        if (phead == NULL)                //malloc若开辟内存失败,返回NULL
        {
            printf("开辟内存失败");
        }
        else                                        //开辟成功
        {
            struct people* ptail;             //ptail指向尾节点
            ptail = phead;                      //此时尾节点为phead(头结点)
            ptail->pnext = NULL;           //头结点此时指向NULL
            printf("how many people you want to make  ");
            scanf_s("%d", &num)          ;//结点数(人数)
            while (cot < num)
            {
                printf("show the people's a");
                scanf_s("%d", &num_instr);                    //输入各个节点的a
                struct people* pnode = (struct people*)malloc(sizeof(struct people));//新增节点
                if (phead == NULL)//malloc若开辟内存失败,返回NULL
                {
                    printf("wrong");
                }
                pnode->a = num_instr;			//新增节点的a赋值
                ptail->pnext = pnode;				//当前尾节点指向新增节点
                pnode->pnext = NULL;			//新增节点指向null
                ptail = pnode;					//尾指针重新指向现在的尾部,即刚刚新增的pnode
                cot++;
            }
        }
        return phead;
    }

2.3遍历链表

    struct people* trav_ls(struct people* phead)		//遍历链表
    {
        if (isempty(phead))						//调用自己写的isempty函数,判断链表是否为空
            printf("empty");
        else
        {
            phead = phead->pnext;					//指向首节点,不是头结点,头结点只包含头节点的地址
            while (phead != NULL)					//巧妙利用最后一个节点的pnext指向NULL,在rev中也有利用这个
            {
                printf("%d\t", phead->a);				//遍历输出
                phead = phead->pnext;
            }
        }
        return phead;
    }

2.4添加指定位置的节点
在这里插入图片描述

    struct people* add(struct people* phead, int num)//增加指定位置的节点,num为用户指定的位置
    {
        int cot = 0;							//cot为从头开始,当加到num时,就到了要加的地方了
        struct people* p1 = phead;
        while (cot != num)					//往后找
        {
            cot++;
            p1 = p1->pnext;
        }
        if (cot == num)						//找到
        {
            struct people* newpeo = (struct people*)malloc(sizeof(struct people));//新建一个节点
            printf("press new people's a");
            scanf_s("%d", &newpeo->a);			//输入新节点中的a的值
            newpeo->pnext = p1->pnext;			//新增节点连接上后一个节点
            p1->pnext = newpeo;				//前一个节点连接上新增节点
            printf("add success");
        }
        else
        {
            printf("wrong num");
        }
        return phead;
    }

2.5删除指定位置的节点
在这里插入图片描述

 struct people* del(struct people* phead, int num)
 								//删除指定节点(按a的数值)
 {
     struct people* p1 = phead->pnext;		//p1指向要删除的数值
     struct people* p2 = phead;				//p2记录p1上一次的位置
     while (p1->a != num && p1->pnext != NULL)//遍历链表,看能否找到要找的数值
     {
              p2 = p1;
         p1 = p1->pnext;
     }
     if (p1->a == num)				//若找到,链表上一节(p2所在位置)链接上p1的下一节
     {
         p2->pnext = p1->pnext;
         printf("delete success");
     }
         else
     {
         printf("delete fail");
     }
     free(p1);						//注意要free掉内存
     p1 = NULL;					//不要出现野指针
     return phead;
 }
 ```
我也在学习当中,如果有哪里错误的话,请不吝赐教!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值