VC驿站

 找回密码
 加入驿站

QQ登录

只需一步,快速开始

搜索
查看: 330|回复: 1

链表增加尾结点,释放空间不报错.增加头结点运行时报错

[复制链接]
66_avatar_middle
最佳答案
0 
在线会员 发表于 2022-1-9 23:25:17 | 显示全部楼层 |阅读模式
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>

struct Node
{
        int num;
        struct Node *pnext;
};

void AddtoEnd(struct Node**pHead,struct Node **pEnd,int value);

void show_all(struct Node *phead);

void freeaa(struct Node *phead);

void AddtoHead(struct Node **phead, struct Node **pend, int value);

int main()
{
        struct Node * head =  NULL ;
        struct Node * hend = NULL ;
        AddtoEnd(&head, &hend, 5);
        AddtoEnd(&head, &hend, 10);
        AddtoEnd(&head, &hend, 28);
        AddtoHead(&head, &hend, 9999);
        //AddtoHead(&head, &hend, 6666);
       
        show_all(head);
        freeaa(head);
        head = NULL;
        hend = NULL;
        while (1);
}

void AddtoEnd(struct Node ** pHead, struct Node ** pEnd, int value)  //增加尾结点
{
        struct Node *temp = (struct Node *)malloc(sizeof(struct Node));  //1创建节点,且注意malloc是否赋值成功  
        if (NULL != temp)
        {
                temp->num = value;    //2节点赋值                                               
                temp->pnext = NULL;

                if (NULL == *pHead || NULL == *pEnd)  //空链表
                {
                        *pHead = temp;
                        *pEnd = temp;
                }
                else                                //3连上节点
                {
                        (*pEnd)->pnext = temp;
                        *pEnd = temp;
                }
        }
}


void show_all(struct Node *phead)  //输出所有节点
{
        while (phead != NULL)
        {
                printf("%d ", phead->num);
                phead = phead->pnext;
        }
        printf("\n");
}

void freeaa(struct Node *phead)  //释放所有节点
{
        while (phead != NULL)
                {
                        struct Node *pt = phead;
                        phead = phead->pnext;
                        free(pt);
                }
}

void AddtoHead(struct Node **phead, struct Node **pend, int value)  //增加头节点
{
        struct Node *temp = (struct Node *)malloc(sizeof(temp));
        temp->num = value;
        temp->pnext = NULL;
        if (NULL == *phead)
        {
                *phead = temp;
                *pend  = temp;
        }
        else
        {
                temp->pnext = *phead;
                *phead = temp;
        }
}

拜托各位了




上一篇:VS代码报错
下一篇:vs2019的msdn为什么很多东西搜不到?
00_avatar_middle
最佳答案
2 
donate_vip 发表于 2022-2-10 17:55:08 | 显示全部楼层
struct Node *temp = (struct Node *)malloc(sizeof(temp));
增加头节点里面的这个语句应该是
struct Node *temp = (struct Node *)malloc(sizeof(Node));
您需要登录后才可以回帖 登录 | 加入驿站 qq_login

本版积分规则

×【发帖 友情提示】
1、请回复有意义的内容,请勿恶意灌水;
2、纯数字、字母、表情等无意义的内容系统将自动删除;
3、若正常回复后帖子被自动删除,为系统误删的情况,请重新回复其他正常内容或等待管理员审核通过后会自动发布;
4、感谢您对VC驿站一如既往的支持,谢谢合作!

关闭

站长提醒上一条 /2 下一条

QQ|小黑屋|手机版|VC驿站 ( 辽ICP备09019393号-4 )|网站地图wx_jqr

GMT+8, 2023-6-4 21:31

Powered by CcTry.CoM

© 2009-2021 cctry.com

快速回复 返回顶部 返回列表