欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

学生信息链表,建立,插入,删除,遍历,查找,修改,最大(小)值,平均

程序员文章站 2022-07-02 10:50:15
/ 【例11 10】建立一个学生成绩信息(包括学号、姓名、成绩)的单向链表,学生数据按学号由小到大顺序排列,要求实现对成绩信息的插入、修改、删除和遍历操作。 / / 用链表实现学生成绩信息的管理 / include include include struct stud_node{ int num; ......

/【例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;

}