标题:链表有点吃力,想写个函数用来删除分数最小的那位学生,不知道怎么写
只看楼主
whukeming
Rank: 2
等 级:论坛游民
帖 子:76
专家分:51
注 册:2008-8-24
结帖率:0
已结贴  问题点数:20 回复次数:2 
链表有点吃力,想写个函数用来删除分数最小的那位学生,不知道怎么写
目前只实现了如何删除指定学生的结点

想写个函数比较学生成绩大小,然后删除成绩最差的那个同学

求指导

程序代码:
//
//  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;
}
搜索更多相关主题的帖子: 学生 函数 Copyright 动态 如何 
2013-05-16 21:15
whukeming
Rank: 2
等 级:论坛游民
帖 子:76
专家分:51
注 册:2008-8-24
得分:0 
可否从小到大排序,然后删除首结点。
2013-05-16 21:19
笑傲
Rank: 8Rank: 8
来 自:迪拜
等 级:蝙蝠侠
威 望:5
帖 子:223
专家分:856
注 册:2013-3-9
得分:20 
既然有想法,为何不去试一试呢!
亲自尝试永远比别人告诉你好!

练就一身本领,只为笑傲江湖!
2013-05-16 22:12



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-407287-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.146451 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved