学生信息链表,建立,插入,删除,遍历,查找,修改,最大(小)值,平均...
/【例11-10】建立一个学生成绩信息(包括学号、姓名、成绩)的单向链表,学生数据按学号由小到大顺序排列,要求实现对成绩信息的插入、修改、删除和遍历操作。/
/* 用链表实现学生成绩信息的管理 */
include<stdio.h>
include<stdlib.h>
include<string.h>
struct stud_node{
int num;
char name[20];
int score;
struct stud_node next;
};
struct stud_node Create_Stu_Doc(); /* 新建链表 /
struct stud_node InsertDoc(struct stud_node * head, struct stud_node stud); / 插入 /
struct stud_node DeleteDoc(struct stud_node * head, int num); /* 删除 /
void Print_Stu_Doc(struct stud_node head); /* 遍历 */
struct stud_node * Find(struct stud_node * head, int num); /查找/
struct stud_node * Modify(struct stud_node * head, int num, int score); /修改成绩/
struct stud_node * Max(struct stud_node * head); /求最高分数的学生记录/
struct stud_node * Min(struct stud_node * head); /求最低分数的学生记录/
double Ave(struct stud_node * head); /求平均分/
void Save(struct stud_node * head);
struct stud_node *Read();
typedef struct stud_node *LNode;
LNode head,p;
int main(void)
{
struct stud_node head=NULL, p;
int choice, num, score;
char name[20];
int size = sizeof(struct stud_node);
do{
printf("1:Create 2:Insert 3:Delete 4:Print 5:Modify 6:Find 7:Max 8:Save 9:Read 10:Min 0:Exit\n");
scanf("%d", &choice);
switch(choice){
case 1:
head = Create_Stu_Doc();
break;
case 2:
printf("Input num,name and score:\n");
scanf("%d%s%d", &num,name, &score);
p = (struct stud_node *) malloc(size);
p->num = num;
strcpy(p->name, name);
p->score = score;
head = InsertDoc(head, p);
break;
case 3:
printf("Input num:\n");
scanf("%d", &num);
head = DeleteDoc(head, num);
break;
case 4:
Print_Stu_Doc(head);
break;
case 5:
printf("Input num:\n");
scanf("%d", &num);
printf("Input score:\n");
scanf("%d", &score);
head=Modify(head,num,score);
break;
case 6:
printf("Input num:\n");
scanf("%d", &num);
p=Find(head,num);
if(p!=NULL)
printf("%d\t%s\t%d \n", p->num, p->name, p->score);
else
printf("Not Found %d\n",num);
break;
case 7:
p=Max(head);
if(p!=NULL)
printf("%d\t%s\t%d \n", p->num, p->name, p->score);
else
printf("Not Found\n");
break;
case 8:
Save(head);
break;
case 9:
head=Read();
break;
case 10:
p=Min(head);
if(p!=NULL)
printf("%d\t%s\t%d \n", p->num, p->name, p->score);
else
printf("Not Found\n");
break;
case 0:
break;
}
}while(choice != 0);
return 0;
}
/新建链表/
struct stud_node * Create_Stu_Doc()
{
struct stud_node * head,*p;
int num,score;
char name[20];
int size = sizeof(struct stud_node);
head = NULL;
printf("Input num,name and score:\n");
scanf("%d%s%d", &num,name, &score);
while(num != 0){ /* 学号0表示输入结束,且学号0对应的姓名和成绩都要输入 */ p = (struct stud_node *) malloc(size);p->num = num;strcpy(p->name, name);p->score = score;head = InsertDoc(head, p); /* 调用插入函数 */scanf("%d%s%d", &num, name, &score);
}
return head;
}
/* 插入操作 /
struct stud_node InsertDoc(struct stud_node * head, struct stud_node stud)
{
struct stud_node ptr ,ptr1, ptr2;
ptr2 = head;
ptr = stud; /* ptr指向待插入的新的学生记录结点 */if(head == NULL){ /* 原链表为空时的插入 */ head = ptr; /* 新插入结点成为头结点 */head->next = NULL;
}
else{ /* 原链表不为空时的插入 */while((ptr->num > ptr2->num) && (ptr2->next != NULL)){ptr1 = ptr2; /* ptr1, ptr2各后移一个结点 */ptr2 = ptr2->next;}if(ptr->num <= ptr2->num){ if(head == ptr2) head = ptr;/* 新插入结点成为头结点 */else ptr1->next = ptr; /* 在ptr1与ptr2之间插入新结点 */ptr->next = ptr2;}else{ /* 新插入结点成为尾结点 */ptr2->next = ptr;ptr->next = NULL; }
}
return head;
}
/* 删除操作 /
struct stud_node DeleteDoc(struct stud_node * head, int num)
{
struct stud_node ptr1, ptr2;
if(head == NULL) /*链表空 */return NULL;
else if(head->num == num){/* 要被删除结点为表头结点 */while(head != NULL && head->num == num){ ptr2 = head;head = head->next;free(ptr2);}
}
else{/* 要被删除结点为非表头结点 */ptr1 = head;ptr2 = head->next; /*从表头的下一个结点搜索所有符合删除要求的结点 */while(ptr2 != NULL){if(ptr2->num == num){ /* ptr2所指结点符合删除要求 */ptr1->next = ptr2->next;free(ptr2); }else ptr1 = ptr2; /* ptr1后移一个结点 */ptr2 = ptr1->next; /* ptr2指向ptr1的后一个结点 */}
}return head;
}
/遍历操作/
void Print_Stu_Doc(struct stud_node * head)
{
struct stud_node * ptr;
if(head == NULL){printf("\nNo Records\n");return;
}
printf("\nThe Students' Records Are: \n");
printf("Num\tName\tScore\n");
for(ptr = head; ptr != NULL; ptr = ptr->next)printf("%d\t%s\t%d \n", ptr->num, ptr->name, ptr->score);
}
struct stud_node * Find(struct stud_node * head, int num) /查找/
{
struct stud_node *ptr;
if(head==NULL)return NULL;
else
{for(ptr=head;ptr!=NULL;ptr=ptr->next)/*for循环从head开始到NULL循环*/ if(ptr->num==num)break;return ptr;
}
}
struct stud_node * Modify(struct stud_node * head, int num, int score)/修改通过查找学号修改成绩/
{
struct stud_node *ptr;
if(head==NULL)return NULL;
else
{for(ptr=head;ptr!=NULL;ptr=ptr->next)if(ptr->num==num){ptr->score=score;break;}return head;
}
}
struct stud_node * Max(struct stud_node * head)
{
struct stud_node maxp,ptr;
if(head==NULL)return NULL;
else
{maxp=head;for(ptr=head->next;ptr!=NULL;ptr=ptr->next)if(maxp->score<ptr->score)maxp=ptr;return maxp;
}
}
struct stud_node * Min(struct stud_node * head)
{
struct stud_node minp,ptr;
if(head==NULL)return NULL;
else
{minp=head;for(ptr=head->next;ptr!=NULL;ptr=ptr->next)if(minp->score>ptr->score)minp=ptr;return minp;
}
}
void Save(struct stud_node * head)
{
FILE fp;
if((fp=fopen("data.txt","w")) == NULL){ /打开文件(套用)*/
printf("File open error!\n");
exit(0);
}
struct stud_node * ptr;if(head == NULL){return;
}for(ptr = head; ptr->next != NULL; ptr = ptr->next)fprintf(fp,"%d %s %d\n", ptr->num, ptr->name, ptr->score);
fprintf(fp,"%d %s %d", ptr->num, ptr->name, ptr->score);/*输出的最后没有换行*/if( fclose(fp) ){/*关闭文件(套用)*/printf( "Can not close the file!\n" );exit(0);
}
}
struct stud_node Read()
{
FILE fp;
if((fp=fopen("data.txt","r")) == NULL){
printf("File open error!\n");
exit(0);
}
struct stud_node * head,*tail,*p;
//int num,score;
//char name[20];
int size = sizeof(struct stud_node);head = tail=NULL;while(!feof(fp)){ /* 读到结束标志结束 */ p = (struct stud_node *) malloc(size);fscanf(fp,"%d%s%d", &p->num, p->name, &p->score);//head = InsertDoc(head, p); /* 调用插入函数 */p->next = NULL;if(head == NULL) head = p;else tail->next = p;tail = p;
}
if( fclose(fp) ){printf( "Can not close the file!\n" );exit(0);
}return head;
}
转载于:https://www.cnblogs.com/1112zx/p/10549694.html
学生信息链表,建立,插入,删除,遍历,查找,修改,最大(小)值,平均...相关推荐
- 用c语言实现单链表的初始化,建表,查找,求长度,插入,删除等操作,【YTU+2430+C语言习题+链表建立+插入+删除+输(5)...
的打印.判断链表是否为空.计算链表长度.插入节点.删除节点.删除整个链表.(2) 线性表adt顺序存储实现中的创建.查找.插入和删除等基本操作及相关算法,线性表adt链式存储实现中单链表.循环链表和双 ...
- PTA: 6-4建立学生信息链表(20分)
大一下半期数据结构 数据结构题目集 pta:建立学生信息链表 本题要求实现一个将输入的学生成绩组织成单向链表的简单函数. 函数接口定义: void input(); 该函数利用scanf从输入中获取学 ...
- 单链表的插入和遍历 包括头插入和尾插入
// Win32Project1.cpp : 定义控制台应用程序的入口点. // //单链表的插入和遍历 #include "stdafx.h" #include <AccC ...
- 顺序表和单链表的插入删除操作时间复杂度的区别
顺序表和单链表的插入删除操作时间复杂度的区别 最近在学习数据结构,看到如果需要用到大量的插入和删除操作,单链表的效率会高于顺序表.看到这里时内有有个疑惑,这两种数据结构的插入和删除操作的时间复杂度不都 ...
- 已知一个字典变量stu_ infors,包含n个学生信息,每个学生信息,即字典变量stu_ infors每个元素的值为“学号:课程成绩”。现编写-个程序实现以下功能: (1)创建包含n个学生信息的字
1.已知一个字典变量stu_ infors,包含n个学生信息,每个学生信息,即字典变量stu_ infors每个元素的值为"学号:课程成绩".现编写-个程序实现以下功能: (1)创 ...
- SqlServer 增加字段,修改字段名,删除字段,修改字段默认值
SqlServer 增加字段,修改字段名,删除字段,修改字段默认值 1:增加字段 语法:alter table 表名 add 新增字段名 字段类型 默认值- 2:修改字段名 语法:exec sp_re ...
- python列表输出学生姓名学号链表_c语言!!!程序设计:建立一个学生信息链表,包括学号,姓名,成绩.(实现添加,删除,查询,排序,平均)...
展开全部 代码如下: /*用c语言链表编写一个学生信息系统程序,62616964757a686964616fe4b893e5b19e31333365656636要求输出学生的学号,姓名,性别, 学号, ...
- 链表(创建,插入,删除和打印输出
http://www.bianceng.cn/Programming/C/200705/327.htm (以下不全,去此网址看) 数组作为存放同类数据的集合,给我们在程序设计时带来很多的方便,增加了 ...
- 随机生成大量学生信息,可插入到学生表进行有关项目的练习
最近在练习一个学生管理系统的项目,需要有一个初始的有大量数据学生表.参考 https://blog.csdn.net/yeyu_xing/article/details/108844330 生成随机学 ...
最新文章
- C#电视节目单展示案例
- grub修复与grub.conf
- pom文件内标签的讲解
- [Abp 源码分析]多语言(本地化)处理
- js 月份加6个月_美国切削工具6月份订单较上月增加10.1
- ArcGIS 10.3 AddIN编译旧版本项目问题
- 帆软报表-快速入门(持续更新)
- ztek usb转串口 linux,Z-Tek USB转串口驱动
- AFFF3%水成膜泡沫灭火剂生产厂家分析品牌哪家好数据说明
- 图书信息管理系统C语言IPO,基于IPO的Python教学设计
- Linux之LVM篇
- linux7安装网卡驱动,CentOS 7 安装无线网卡驱动
- Android EditText 监听回车键
- 〖全域运营实战白宝书 - 运营角色认知篇⑦〗- 运营人的能力模型
- wt32-eth01 arduino
- Centos7 Docker环境部署系统漏洞扫描工具Nessus
- Ktor 2.0?半香不香的尴尬
- 老司机带你玩转面试(2):Redis 过期策略以及缓存雪崩、击穿、穿透
- Lync(Skype)接口开发实录
- IDEA打包指定类为jar包