桦~里胡哨 2022-04-21 20:11 采纳率: 25%
浏览 9

世纪难题,哪位大佬来帮我解决困惑?为啥打印出的money值会重复出现两个???

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
struct pepole{
char name[10];
int money;
struct pepole *next;
};
void show2(struct pepole *list){
struct pepole *g=list->next;
printf("第一次遍历完整链表\n");
while(g!=NULL){

    printf("%s",g->name );
    printf("%d",g->money );
    g=g->next ;
}
printf("\n");

}

struct pepole *del_list(struct pepole *list,int money){//定义函数
//定义两个辅助指针并赋值
struct pepole *p=list;
struct pepole *h=list->next ;
while(h!=NULL){//删除值为money的节点
if(h->money ==money){
p->next =h->next;
free(h);//释放删除节点的内存
}
else
p=h;//寻找要删除的节点
h=h->next ;
}
return list;

}
int main(){
struct pepole *list=(struct pepole *)malloc(sizeof(struct pepole));
struct pepole *wei=list;

char val[10]={0};
int val2=0;
printf("为每个节点赋值:\n");
while(true){//输入数据,为新节点赋值做准备 
    gets(val);
    scanf("%d",&val2);
    if(val2==0)//当输入的值为0时,结束输入 
    break;
    
    struct pepole *newnode=(struct pepole *)malloc(sizeof(struct pepole));//创建新节点 
    for(int i=0;i<10;i++)//为新节点赋值 
    newnode->name[i]=val[i];
    newnode->money =val2;
    newnode->next =NULL;
    
    wei->next=newnode;//更新尾指针位置 
    wei=newnode;    
}
int a;
printf("输入一个值:\n"); 
scanf("%d",&a);//输入一个要删除的工资值
 
show2(list);

struct pepole *header= del_list(list,a);//调用del_list(struct pepole *x,int y)删除函数

struct pepole *k=header->next;//定义辅助指针
printf("遍历删除后链表\n");
while(k!=NULL){//遍历节点

printf("%s",k->name);
printf("%d",k->money);
k=k->next;

}
return 0;
}

  • 写回答

1条回答 默认 最新

  • jsjjdcw 2022-04-21 21:29
    关注

    提个疑问,为什么可以两个不同的g和k->money,而不是如:p->mony=k或p->mony=g?

    评论

报告相同问题?

问题事件

  • 创建了问题 4月21日

悬赏问题

  • ¥15 有偿求苍穹外卖环境配置
  • ¥15 代码在keil5里变成了这样怎么办啊,文件图像也变了,
  • ¥20 Ue4.26打包win64bit报错,如何解决?(语言-c++)
  • ¥15 clousx6整点报时指令怎么写
  • ¥30 远程帮我安装软件及库文件
  • ¥15 关于#自动化#的问题:如何通过电脑控制多相机同步拍照或摄影(相机或者摄影模组数量大于60),并将所有采集的照片或视频以一定编码规则存放至规定电脑文件夹内
  • ¥20 深信服vpn-2050这台设备如何配置才能成功联网?
  • ¥15 Arduino的wifi连接,如何关闭低功耗模式?
  • ¥15 Android studio 无法定位adb是什么问题?
  • ¥15 C#连接不上服务器,