69,382
社区成员
发帖
与我相关
我的任务
分享
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
typedef struct student
{
char name[32];
float score;
struct student *next;
}STU;
STU *myCreatelist(STU *head)
{
STU *q,*p;
char temp[10];
int flag=1;
float t=0;
head=(STU*)malloc(sizeof(STU));
head->next = NULL;
q=head;
while(flag)
{
p = (STU*)malloc(sizeof (STU));
p->next = NULL;
printf("input name\n");
scanf("%s", temp);
printf("input score\n");
strcpy(p->name,temp);
scanf("%f", &t);
p->score = t;
q->next=p;
q = p;
printf("inpuut flag\n");
scanf("%d",&flag);
}
p=head->next;
while(p)
{
printf("name is :%s\n",p->name);
printf("score is :%.2f\n",p->score);
p=p->next;
}
printf("\n");
return head;
}
int main ()
{
STU *head = NULL,*p,*q;
head = myCreatelist(head);
q = head->next;
while (q)
{
if(!strcmp (q->name,"x6")) {
p=q->next;
q->next=p;
break;
} else {
q = q->next;
}
}
p = head->next;
while (p)
{
printf("name is :%s\n",p->name);
printf("score is :%f\n",p->score);
p = p->next;
}
return 0;
}
结构体定义有问题,name定义的长度太短,只要输入一个字符就会导致溢出,score若是一个成绩,建议定义成变量而非数组。// cstdlib #defines NULL to 0
STU *head = NULL; // ok: equivalent to int *pi = 0;