作业帮 > 综合 > 作业

请帮忙看下这个题怎么改错,★表示那行有错误,谢谢了! (急)

来源:学生作业帮 编辑:作业帮 分类:综合作业 时间:2024/05/10 23:12:33
请帮忙看下这个题怎么改错,★表示那行有错误,谢谢了! (急)
以下程序的功能是删除链表中的一个结点并将结果输出.
#include
#include
#include
#include
struct station
{ char name[8];
struct station *nextSta;
};
struct station *creat_sta(struct station *h);
void print_sta(struct station *h);
struct station *del_sta(struct station *h,char *str);
int num=0;
void main()
{ struct station head; ★
char name[50],*del_stas=&name; ★
head=NULL;
printf("请输入站名:\n");
head=creat_sta(head); /* 建立链表 */
printf("---------------------------\n");
printf("站点数为:%d\n",num);
print_sta(head); /* 输出链表中的站点信息 */
printf("\n请输入要删除的站名:\n");
scanf("%c",name); ★
head=del_sta(head,&del_stas); ★/* 删除链表中的一个站点 */
printf("---------------------------\n");
printf("新的站点为:\n");
print_sta(head); /* 输出删除站点后链表中的站点信息 */
printf("\n");
}
/* 建立由各站点组成的链表 */
struct station *creat_sta(struct station *h)
{ struct station *p1,*p2;
p1=p2=(struct station*)malloc(sizeof(struct station));
if(p2!=NULL)
{
scanf("%s",&p2->name); ★
p2->nextSta=NULL;
}
while(p2->name[0]!='#')
{ num++;
if(h==NULL)
h=p2;
else
p1->nextSta=p2;
p1=p2;
p2=(struct station*)malloc(sizeof(struct station));
if(p2!=NULL)
{
scanf("%s",&p2->name);
p2->nextSta=NULL;
}
}
return h;
}
/* 输出链表中的信息 */
void print_sta(struct station *h)
{ struct station *temp;
temp=h; /*获取链表的头指针*/
while(temp!=NULL)
{
printf("%-8s",temp->name); /*输出链表结点的值*/
temp=temp->nextSta; /*移到下一个结点*/
}
}
/* 修改链表中指针的指向,删除的站点名为str所指的字符串*/
struct station del_sta(struct station *h,char *str) ★
{ struct station p1,*p2; ★
p1=h;
if(p1==NULL)
{
printf("The list is null\n");
return h;
}
p2=p1->nextSta;
if(!strcmp(p1->name,str))
{
h=p2;free(p1);
return h;
}
while(p2!=NULL)
{
if(!strcmp(p2->name,str))
{
p1->nextSta=p2->nextSta;free(p2);
return h;
}
else
{
p1=p2;
p2=p2->nextSta;
}
}
return h;
}
你是计信的吧,嘿嘿