设计程序,利用链表完成“信息管理系统”软件项目的相应功能模块。要求:
(1)“创建链表”模块:功能是输入学生的姓名和成绩(3个节点以上)
(2)“遍历链表”模块:功能是输出所有学生的姓名和成绩
(3)“成绩查询”模块:功能是按成绩查找学生的信息
(4)“插入节点”模块:功能是在“Tom”学生之后插入“Dave”学生信息
(5)“删除节点”模块:功能是删除指定学生信息
(6)“系统菜单”模块:功能是显示系统包含的各项功能
请根据提示信息填写程序,并调试完成相应功能。
```c
#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 insertNode(struct STUDENT *head); //插入节点
void deleteNode(struct STUDENT *head,char n[]); //删除节点
void queryByScore(struct STUDENT *head,int s); //成绩查询
void sysMenu(); //系统菜单
int main()
{
struct STUDENT *head;
sysMenu();
head=createList();
outputLinkedList(head);
int s;
printf("\n请输入要查询的成绩:");
scanf("%d",&s);
queryByScore(head,s);
insertNode(head);
outputLinkedList(head);
char name1[20];
printf("\n请输入要删除的节点姓名:");
fflush(stdin);
gets(name1);
deleteNode(head,name1);
outputLinkedList(head);
return 0;
}
struct STUDENT *createList() //定义创建链表函数
{
struct STUDENT *head,*p,*q;
char name[20];
p=(struct STUDENT *)malloc(sizeof(struct STUDENT));
head=p;
q=p;
q->next=NULL;
printf("请输入学生姓名(输入'none'表示输入结束):");
scanf("%s",name);
while(strcmp(name,"none")!=0)
{
/**********Program**********/
int score;
p=(struct STUDENT *)malloc(sizeof(struct STUDENT));
strcpy(p->name,name);
p->score=score;
q->next=p;
q=p;
/********** End **********/
}
q->next=NULL;
return head;
}
void outputLinkedList(struct STUDENT *head) //定义遍历链表函数
{
struct STUDENT *p;
printf("\n输出链表各节点的姓名、分数:\n");
for(p=head->next; p!=NULL; p=p->next)
{
printf("Name is %6s,Score is %d\n",p->name,p->score);
}
}
void insertNode(struct STUDENT *head) //定义插入节点函数
{
struct STUDENT *p,*q;
p=(struct STUDENT *)malloc(sizeof(struct STUDENT));
strcpy(p->name,"Dave");
p->score=100;
for(q=head->next; q!=NULL; q=q->next)//查找节点
if(strcmp(q->name,"Tom") == 0)
break;
if(q!=NULL) //找到要插入节点的位置
{
p->next=q->next;
q->next=p;
printf("\n插入节点成功!\n");
}
else
{
printf("\n插入节点失败!!\n");
}
}
void deleteNode(struct STUDENT *head,char n[]) //定义删除节点函数
{
struct STUDENT *p,*q;
q=head;
/**********Program**********/
p=q;
while(q!=NULL&&strcmp(q->name,n)!=0)
{
p=q;
q=q->next;
}
/********** End **********/
if(q!=NULL) //找到要删除的节点
{
p->next=q->next; //删除q节点
free(q); //释放q节点所占内存
printf("\n删除节点成功!\n");
}
else
{
printf("\n没有找到要删除的节点\n");
}
}
void queryByScore(struct STUDENT *head,int s) //定义成绩查询函数
{
struct STUDENT *p;
printf("输出查询结果:\n");
/**********Program**********/
p=head;
while(p!=NULL&&p->score!=s)
{
p=p->next;
}
if(p=NULL)
{
printf("未找到\n");
}
else
{
printf("已找到:%s\n",p->name);
}
/********** End **********/
}
void sysMenu() //定义系统菜单函数
{
printf("====================\n");
printf(" 链表操作\n");
printf(" 1. 创建链表\n");
printf(" 2. 遍历链表\n");
printf(" 3. 成绩查询\n");
printf(" 4. 插入节点\n");
printf(" 5. 删除节点\n");
printf(" 0. 退出\n");
printf("====================\n");
}
```
输入none就直接跳过姓名分数,输入名字按回车以后就输入不了了,不知道哪里出了问题,截图在评论区
问题已经解决了
#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 insertNode(struct STUDENT *head); //插入节点
void deleteNode(struct STUDENT *head,char n[]); //删除节点
void queryByScore(struct STUDENT *head,int s); //成绩查询
void sysMenu(); //系统菜单
int main()
{
struct STUDENT *head;
sysMenu();
head=createList();
outputLinkedList(head);
int s;
printf("\n请输入要查询的成绩:");
scanf("%d",&s);
queryByScore(head,s);
insertNode(head);
outputLinkedList(head);
char name1[20];
printf("\n请输入要删除的节点姓名:");
fflush(stdin);
gets(name1);
deleteNode(head,name1);
outputLinkedList(head);
return 0;
}
struct STUDENT *createList() //定义创建链表函数
{
struct STUDENT *head,*p,*q;
char name[20];
p=(struct STUDENT *)malloc(sizeof(struct STUDENT));
head=p;
q=p;
q->next=NULL;
printf("请输入学生姓名(输入'none'表示输入结束):");
scanf("%s",name);
while(strcmp(name,"none")!=0)
{
/**********Program**********/
int score;
p=(struct STUDENT *)malloc(sizeof(struct STUDENT));
strcpy(p->name,name);
p->score=score;
q->next=p;
q=p;
printf("请输入学生姓名(输入'none'表示输入结束):");
scanf("%s",name);
/********** End **********/
}
q->next=NULL;
return head;
}
void outputLinkedList(struct STUDENT *head) //定义遍历链表函数
{
struct STUDENT *p;
printf("\n输出链表各节点的姓名、分数:\n");
for(p=head->next; p!=NULL; p=p->next)
{
printf("Name is %6s,Score is %d\n",p->name,p->score);
}
}
void insertNode(struct STUDENT *head) //定义插入节点函数
{
struct STUDENT *p,*q;
p=(struct STUDENT *)malloc(sizeof(struct STUDENT));
strcpy(p->name,"Dave");
p->score=100;
for(q=head->next; q!=NULL; q=q->next)//查找节点
if(strcmp(q->name,"Tom") == 0)
break;
if(q!=NULL) //找到要插入节点的位置
{
p->next=q->next;
q->next=p;
printf("\n插入节点成功!\n");
}
else
{
printf("\n插入节点失败!!\n");
}
}
void deleteNode(struct STUDENT *head,char n[]) //定义删除节点函数
{
struct STUDENT *p,*q;
q=head;
/**********Program**********/
p=q;
while(q!=NULL&&strcmp(q->name,n)!=0)
{
p=q;
q=q->next;
}
/********** End **********/
if(q!=NULL) //找到要删除的节点
{
p->next=q->next; //删除q节点
free(q); //释放q节点所占内存
printf("\n删除节点成功!\n");
}
else
{
printf("\n没有找到要删除的节点\n");
}
}
void queryByScore(struct STUDENT *head,int s) //定义成绩查询函数
{
struct STUDENT *p;
printf("输出查询结果:\n");
/**********Program**********/
p=head;
while(p!=NULL&&p->score!=s)
{
p=p->next;
}
if(p==NULL)
{
printf("未找到\n");
}
else
{
printf("已找到: %s\n",p->name);
}
/********** End **********/
}
void sysMenu() //定义系统菜单函数
{
printf("====================\n");
printf(" 链表操作\n");
printf(" 1. 创建链表\n");
printf(" 2. 遍历链表\n");
printf(" 3. 成绩查询\n");
printf(" 4. 插入节点\n");
printf(" 5. 删除节点\n");
printf(" 0. 退出\n");
printf("====================\n");
}