#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;
}