设为首页 加入收藏

TOP

学生信息链表,建立,插入,删除,遍历,查找,修改,最大(小)值,平均(一)
2019-03-18 00:07:59 】 浏览:336
Tags:学生 信息 建立 插入 删除 遍历 查找 修改 最大 平均

/【例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, ptr
首页 上一页 1 2 3 下一页 尾页 1/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇cf1136E. Nastya Hasn't Writ.. 下一篇基于Cmake+QT+VS的C++项目构建开..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目