任务要求
根据提示,在右侧编辑器的Program-End
区域内补充代码,完成“删除节点”模块。
输入要求
输入内容包含两部分内容,第一部分内容通过struct STUDENT *createList()
输入学生的姓名和成绩,实现链表创建;第二部分输入一个字符串name(1<=len(name)<=10)
,表示需要删除学生的姓名。
输出要求
输出内容包括三部分内容,第一部分内容通过void outputLinkedList(struct STUDENT *head);
输出链表全部结点内容。第二部分内容:当结点删除成功时,输出“Delete node success”(不包含双引号),否则输出“Delete node fail”(不包含双引号)。第三部分内容通过void outputLinkedList(struct STUDENT *head);
输出新链表全部结点内容。
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct STUDENT
{
char name[20];
int score;
struct STUDENT *next;
};
struct STUDENT *createList(); //创建链表
void outputLinkedList(struct STUDENT *head); //遍历链表
void deleteNode(struct STUDENT *head,char n[]); //删除节点
void sysMenu(); //系统菜单
int main()
{
struct STUDENT *head;
sysMenu();
head=createList();
outputLinkedList(head);
char name1[20];
scanf("%s",name1);
deleteNode(head,name1);
outputLinkedList(head);
return 0;
}
struct STUDENT *createList() //定义创建链表函数
{
struct STUDENT *head,*p,*q;
char name[20];
/**********Program**********/
head=(struct STUDENT*)malloc(sizeof(struct STUDENT));
q=head;
/********** End **********/
printf("Please input student's name('none' means end of input):");
scanf("%s",name);
while(strcmp(name,"none")!=0)
{
/**********Program**********/
p=(struct STUDENT*)malloc(sizeof(struct STUDENT));
q->next=p;
q=p;
strcpy(q->name,name);
printf("Please input %s's score:",name);
scanf("%d",&(q->score));
printf("Please input student's name('none' means end of input):");
scanf("%s",name);
/********** End **********/
}
q->next=NULL;
return head;
}
void outputLinkedList(struct STUDENT *head) //定义遍历链表函数
{
struct STUDENT *p;
printf("\noutput links'node name and score:\n");
for(p=head->next; p!=NULL; p=p->next)
{
printf("Name is %s,Score is %d\n",p->name,p->score);
}
}
void deleteNode(struct STUDENT *head,char n[]) //定义删除节点函数
{
struct STUDENT *p,*q;
q=head;
/**********Program**********/
p=NULL;
int flag=0;
while(1){
if(strcmp(q->name,n)==0){
flag=1;
break;
}
if(q->next==NULL){
break;
}
p=q;
q=q->next;
}
if(flag){
p->next=q->next;
free(q);
printf("Delete node success\n");
}
else{
printf("Delete node fail\n");
}
/********** End **********/
}
void sysMenu() //定义系统菜单函数
{
printf("====================\n");
printf(" link's operator\n");
printf(" 1. create Link\n");
printf(" 2. forEach Link\n");
printf(" 3. query score\n");
printf(" 4. insert node\n");
printf(" 5. delete node\n");
printf(" 0. exit\n");
printf("====================\n");
}
?
?