c语言编程

输入若干个学生信息(包括学号、姓名和成绩),输入学号为0时输入结束,建立一个单向链表,再输入一个成绩值,将成绩大于等于该值的学生信息输出。
提示:
定义函数struct stud_node *Creat_Stu_Doc()完成创建链表
定义函数struct stud_node *DeleteDoc(struct stud_node *head,int min_score)将分数低于min_score的结点删除
定义函数void Ptrint_Stu_Doc(struct stud_node *head)打印链表
输入输出示例:括号内为说明
输入:
1 zhang 78
2 wang 80
3 li 75
4 zhao 85
0
80
输出:
2 wang 80
4 zhao 85

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>

struct Student
{
char name[20];
int num;
int math;
struct Student *next;
};
struct Student *Create(struct Student *head); /*生成数据链表*/
void saveList(struct Student *head); /*保存链表数据*/
void ShowList(struct Student *head); /*显示链表*/
void AddTail(struct Student *head,struct Student *temp); /*表尾添加元素(*/
struct Student *AddHead(struct Student *head,struct Student *temp); /*表头添加元素*/
struct Student *DeleteSomeone(struct Student *head); //删除元素
void FindSomeone(struct Student *head); //查询元素
void FindMax(struct Student *head,); /*查找并输出 math最大节点的信息*/
void FindMin(struct Student *head,); /*查找并输出 math最小节点的信息 */
void Average(struct Student *head,); /*统计链表节点总数及 math的平均 */

void main()
{
struct Student *head=NULL,*temp=NULL;
int choice;
do
{
printf("1:创建数据链表\n");
printf("2:添加链表单元\n");
printf("3:删除链表单元\n");
printf("4:查询链表单元\n");
printf("5:退出\n");
printf("请输入功能选择:");
scanf("%d",&choice);
if(choice==1)
{
head=Create(temp);
ShowList(head);
saveList(head);
}
else if(choice==2)//表头添加元素
{
temp=(struct Student*)malloc(sizeof(struct Student));
printf("姓名:"); scanf("%s",temp->name);
printf("学号:"); scanf("%d",&temp->num);
printf("成绩:"); scanf("%d",&temp->math);
head=AddHead(head,temp);// head=AddTail(head,temp);
ShowList(head);
saveList(head);
}
else if(choice==3) //删除元素
{
head=DeleteSomeone(head);
ShowList(head);
saveList(head);
}
else if(choice==4) //查找元素
FindSomeone(head);
else if(choice==5)
printf("谢谢使用!\n");
else
printf("非法输入,请输入1~5之间的数!\n");
}while(choice!=5);
}
struct Student *Create(struct Student *head) //不包含打开文件
{
struct Student *pS,*pEnd;
while(1)
{
pS=(struct Student*)malloc(sizeof(struct Student));
printf("姓名:");
scanf("%s",pS->name);
if(strcmp(pS->name,"000"))
{
printf("学号:");
scanf("%d",&pS->num);
printf("成绩:");
scanf("%d",&pS->math);
}
else
break;
if(head==NULL)
head=pS;
else
pEnd->next=pS;
pEnd=pS;

}
pEnd->next=NULL;
delete pS;
return (head);
}
void ShowList(struct Student *head)//显示链表上的数据
{
printf("学生姓名,成绩如下:\n");
while(head)
{
printf("%20s",head->name);
printf("%6d",head->num);
printf("%3d\n",head->math);
head=head->next;
}
}
void saveList(struct Student *head)//保存链表上的数据
{
FILE *fp;
fp=fopen("e:\\list.txt","w");
if(fp==NULL)
{
printf("无法创建文件e:\\list.txt!");
return;
}
while(head)
{
fprintf(fp,"%s %d %d\n",head->name,head->num,head->math);
head=head->next;
}
fclose(fp);
}
void AddTail(struct Student *head,struct Student *temp)//加在表尾
{
struct Student *last;
while(head)
{
last=head;
head=head->next;
}
last->next=temp;
temp->next=NULL;
}
struct Student *AddHead(struct Student *head,struct Student *temp)//加在表头
{
struct Student *oldhead;
oldhead=head;
head=temp;
head->next=oldhead;
return head;
}
struct Student *DeleteSomeone(struct Student *head)
{
struct Student *temp;
int xnum,flag=0;
printf("请输入学号:");
scanf("%d",&xnum);
if(head==NULL)
{
printf("表为空!");;
return head;
}
if(head->num==xnum)
{
temp=head;
head=head->next;
delete temp;
return head;
}
struct Student *first=head;
while(head)
{
if(head->next==NULL)
break;
if(head->next->num==xnum)
{
temp=head->next;
head->next=temp->next;
delete temp;
flag=1;
break;
}
head=head->next;
}
if(flag==0)
printf("未发现!");;
return first;
}
void FindSomeone(struct Student *head)//按学号查找
{
int xnum,flag=0;
printf("请输入学号:");
scanf("%d",&xnum);
while(head)
{
if(head->num==xnum)
{
printf("%20s",head->name);
printf("%6s",head->num);
printf("%3d",head->math);
flag=1;
break;
}
head=head->next;
}
if(flag==0)
printf("未发现!");
}
温馨提示:答案为网友推荐,仅供参考