[C] 纯文本查看 复制代码#include
#include
#include
#include
#define N 5
char string[100]; //string是全局的
typedef struct student
{
char name[20];
float score;
struct student *next;
}mm;
mm *build(void)
{
int i=0;
mm *stu,*pstu;
pstu=(mm*)malloc(sizeof(mm));
stu=pstu;
fflush(stdin);
printf("name[%d]:",i);
gets(pstu->name);
printf("score[%d]:",i);
scanf("%f",&pstu->score);
for(i=1;i
{
pstu->next=(mm*)malloc(sizeof(mm));
fflush(stdin);
printf("name[%d]:",i);
gets(pstu->next->name);
printf("score[%d]:",i);
scanf("%f",&pstu->next->score);
pstu->next->next=NULL;
pstu=pstu->next;
}
return stu;
}
void del(mm *head) //函数预期功能:删除链表中第一个跟输入名字符合的结点
{
if(head)
{
char *str; //str没有初始化,但是有值可以打印出来,整个del函数执行过程中str值没有改变,str所指向的字符串值也可以正常打印,gets(str)之后字符串值也没变.
str=string; //看这里,有这句和没这句的区别在哪里,不过是换了个地址而已,执行结果天壤之别{没这句寒风刺骨,有这句春暖花开}
mm *pr,*stu;
printf("\ndelete:\nname:\n");
fflush(stdin);
gets(str);
stu=head;
pr=NULL;
while(strcmp(stu->name,str) !=0 && (stu->next != NULL))
{
pr=stu;
stu=stu->next;
}
if(0==strcmp(stu->name,str))
{
if(pr)
{
pr->next=stu->next;
}
else
{
head=stu->next;
}
}
else
{
printf("there is no log for %s in this table!\n",str);
}
}
else
{
printf("\nthere is no data in this table and nothing to delete!\n");
}
}
void show(mm *stu)
{
int i=0;
mm *pstu;
pstu=stu;
printf("%20s\t\tscore\n","name");
for(i=0;;i++)
{
printf("%20s\t\t%5.1f\n",pstu->name,pstu->score);
if(pstu->next==NULL){break;}
pstu=pstu->next;
}
}
int main(void)
{
mm *head;
head=build();
show(head);
del(head);
show(head);
return 1;
}