链表有点吃力,想写个函数用来删除分数最小的那位学生,不知道怎么写
目前只实现了如何删除指定学生的结点想写个函数比较学生成绩大小,然后删除成绩最差的那个同学
求指导
程序代码:
// // main.c // 函数动态单向链表 // // Created by Hu Keming on 13-5-12. // Copyright (c) 2013年 Hu Keming. All rights reserved. // #include "stdio.h" #include "stdlib.h" #define LEN sizeof(struct Student) struct Student { long num; float score; struct Student * next; }; int n; struct Student * del (struct Student *head, long num) { struct Student * p1, *p2; if (head == NULL) { printf("\nlist null!\n"); return head; } p1 = head; while (num != p1->num && p1->next != NULL) { p2 = p1; p1 = p1->next; } if (num == p1->num) { if (p1 == head) head = p1->next; else p2->next = p1->next; printf("delete:%ld\n", num); n = n-1; } else printf("%ld not been found!\n", num); return head; } struct Student * creat() //建立链表函数 { struct Student * head; struct Student * p1, * p2; n = 0; p1 = p2 = (struct Student *)malloc(LEN); printf("Now, Plese input num and score:\n"); scanf("%ld, %f", &p1->num, &p1->score); head = NULL; while (p1->num != 0) { n = n+1; if (n ==1) head = p1; else p2->next = p1; p2 = p1; p1 = (struct Student *)malloc(LEN); scanf("%ld, %f", &p1->num, &p1->score); } p2->next = NULL; return (head); } void print(struct Student * head, long num) //输出链表函数 { struct Student * p; printf("\nNow, These %d records are :\n", n); p = head; if (NULL != head) //判断num是不是要剔除的num { if (num == p->num) { p = p->next; } while (NULL != p) { printf("%ld %.1f\n", p->num, p->score); p = p->next; } } } int main(void) { struct Student * head; head = creat(); //调用 creat 函数, 返回第一个结点的起始地址 del(head, 10103); //调用 del 函数, 用来剔除链表中的一个结点 print(head,10103); //调用 print 函数, 输出链表 return 0; }