求大神解答,为什么链表的节点free不了,不加倒没事,一加就报。
暴打章鱼哥 2018-11-13 05:37:29 代码如下,大神就解答
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct info{
int num;
char name[100];
struct info * pnext;
};
void add(struct info **phead,int num,char name[100]){//增加节点
if ((*phead)==NULL){
(*phead) = (struct info * )malloc(sizeof(struct info));
(*phead)->num = num;
strcpy((*phead)->name, name);
(*phead)->pnext = NULL;
} else{
struct info *p = *phead;
while (p->pnext != NULL){
p=p->pnext;
}
struct info *pp = (struct info *)malloc(sizeof(struct info));
pp->num = num;
strcpy(pp->name, name);
pp->pnext = NULL;
p->pnext = pp;
}
}
void Modify(struct info **phead,int num,char oldname[100]){//修改节点
struct info * p = (*phead);
while( p->pnext != NULL){
if (p->num == num){
strcpy(p->name, oldname);
break;
}
p = p->pnext;
}
}
void delet(struct info **phead, int num){//删除节点
struct info *p = (*phead);
struct info * qq=NULL;//记录前一个节点的地址
while (p->pnext != NULL){
if (p->num == num){
qq->pnext = p->pnext;
free(p);
}
qq = p;
p = p->pnext;
}
}
int main(void){
struct info * phead=NULL;
add(&phead,1,"jjjjj");
add(&phead, 2, "jjkkj");
add(&phead, 3, "jjhmj");
Modify(&phead,2,"hahahahhahaha");
delet(&phead, 2);
printf("\n %d %s", phead->num, phead->name);
printf("\n %d %s", phead->pnext->num, phead->pnext->name);
//printf("\n %d %s", phead->pnext->pnext->num, phead->pnext->pnext->name);
system("pause");
}