单链表删除环节问题求指点
#include <stdio.h>#include <stdlib.h>
#include <string.h>
struct Phone
{
char name;
char num;
struct Phone *next;
};
void addpeople(struct Phone **people);
void Inputpeople(struct Phone *people);
void updatepeople(struct Phone *people);
void delpeople(struct Phone **people);
void browse(struct Phone *people);
void release(struct Phone **people);
void meau();
void Inputpeople(struct Phone *buffer)
{
printf("请输入姓名:");
scanf("%s", buffer->name);
printf("请输入电话号码:");
scanf("%s", buffer->num);
}
void addpeople(struct Phone **people)
{
struct Phone *buffer, *temp;
buffer = (struct Phone *)malloc(sizeof(struct Phone));
if(buffer == NULL)
{
printf("申请内存失败!\n");
exit(1);
}
Inputpeople(buffer);
if(people != NULL)
{
temp = *people;
*people = buffer;
buffer->next = temp;
}
else
{
*people = buffer;
buffer->next = NULL;
}
}
void update(struct Phone *people)
{
char ch;
printf("请输入要修改的人的姓名:");
scanf("%s", ch);
while(people != NULL)
{
if(strcmp(ch, people->name) == 0)
{
printf("请输入要修改的电话号码:");
scanf("%s", people->num);
}
people = people->next;
}
}
void delpeople(struct Phone **people)
{
struct Phone *temp;
char ch;
printf("请输入要删除的联系人:");
scanf("%s", ch);
fflush(stdin);
while(*people != NULL)
{
temp = *people;
*people = (*people)->next;
if(strcmp(ch, temp->name) == 0)
{
free(temp);
break;
}
}
}
void browse(struct Phone *people)
{
printf("\nname\t num\n");
while(people != NULL)
{
printf("%s\t%s\n", people->name, people->num);
people = people->next;
}
}
void release(struct Phone **people)
{
struct Phone *temp;
while(*people != NULL)
{
temp = *people;
*people = (*people)->next;
free(temp);
}
}
void meau()
{
printf(" ------------meau----------\n");
printf(" |1-add2-del |\n");
printf(" |3-update 4-browse 5-out|\n");
printf(" ---------------------------\n\n");
}
int main(void)
{
struct Phone *people = NULL;
int op;
meau();
do
{
scanf("%d", &op);
switch(op)
{
case 1 : addpeople(&people);break;
case 2 : delpeople(&people);break;
case 3 : update(people);break;
case 4 : browse(people);break;
case 5 : exit(1);
}
// meau();
}while(op != 0);
release(&people);
return 0;
}
为什么删除不了第一个输入的节点(在次函数void delpeople()),单个上传总是失败,求指点 你的链表节点删除操作不对啊。
应该先找到节点,然后把节点从链表中拿出来,最后再free该节点。
你的程序没有把该节点从链表中剔除啊。
void delpeople(struct Phone **people)
{
struct Phone *pb,*pf;
char ch;
printf("请输入要删除的联系人:");
scanf("%s", ch);
fflush(stdin);
pb=*people;//让pb指向头节点
if(*people == NULL)//链表为空链表
{
printf("链表为空\n");
return ;
}
while(strcmp(ch, pb->name) && pb->next!=NULL)//循环查找要删除的节点
{
pf=pb;
pb=pb->next;
}
if(strcmp(ch, pb->name) == 0)//找到了一个节点的name和ch相等
{
if(pb == *people)//找到的节点是头节点
*people = pb->next;
else//找到的节点是普通节点
pf->next = pb->next;
free(pb);
}
else//没有找到咱们要删除的节点
printf("没有找到您要删除的节点\n");
}
sunrise085 发表于 2020-4-8 14:13
你的链表节点删除操作不对啊。
应该先找到节点,然后把节点从链表中拿出来,最后再free该节点。
你的程序 ...
谢谢
页:
[1]