zoukankan      html  css  js  c++  java
  • 学生信息链表,建立,插入,删除,遍历,查找,修改,最大(小)值,平均

    /【例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 ");
    scanf("%d", &choice);
    switch(choice){
    case 1:
    head = Create_Stu_Doc();
    break;
    case 2:
    printf("Input num,name and score: ");
    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: ");
    scanf("%d", &num);
    head = DeleteDoc(head, num);
    break;
    case 4:
    Print_Stu_Doc(head);
    break;
    case 5:
    printf("Input num: ");
    scanf("%d", &num);
    printf("Input score: ");
    scanf("%d", &score);
    head=Modify(head,num,score);
    break;
    case 6:
    printf("Input num: ");
    scanf("%d", &num);
    p=Find(head,num);
    if(p!=NULL)
    printf("%d %s %d ", p->num, p->name, p->score);
    else
    printf("Not Found %d ",num);
    break;
    case 7:
    p=Max(head);
    if(p!=NULL)
    printf("%d %s %d ", p->num, p->name, p->score);
    else
    printf("Not Found ");
    break;
    case 8:
    Save(head);
    break;
    case 9:
    head=Read();
    break;
    case 10:
    p=Min(head);
    if(p!=NULL)
    printf("%d %s %d ", p->num, p->name, p->score);
    else
    printf("Not Found ");
    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:
    ");
    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("
    No Records
    ");
        return;
    }
    printf("
    The Students' Records Are: 
    ");
    printf("Num	Name	Score
    ");
    for(ptr = head; ptr != NULL; ptr = ptr->next)
      	printf("%d	%s	%d 
    ", 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! ");
    exit(0);
    }

    struct stud_node * ptr;
    
    if(head == NULL){
        return;
    }
    
    for(ptr = head; ptr->next != NULL; ptr = ptr->next)
      	fprintf(fp,"%d %s %d
    ", 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!
    " );
    	exit(0);
    }	
    

    }

    struct stud_node *Read()
    {
    FILE *fp;
    if((fp=fopen("data.txt","r")) == NULL){
    printf("File open error! ");
    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!
    " );
    	exit(0);
    }
    
    return head;
    

    }

  • 相关阅读:
    jquery堆栈与队列
    类数组转成数组对象
    python基础教程总结8——特殊方法,属性,迭代器,生成器,八皇后问题
    python基础教程总结7——异常
    python基础教程总结6——类
    python基础教程总结5——函数
    python基础教程总结4—基本语句
    python——动态类型
    python基础教程总结3—字典
    python基础教程总结2——字符串
  • 原文地址:https://www.cnblogs.com/1112zx/p/10549694.html
Copyright © 2011-2022 走看看