C语言---链表实现学生成绩管理(并求出排名最高的三位)

C语言—链表实现学生成绩管理(并求出排名最高的三位)

在这里插入代码片#include <stdio.h>
//#include<malloc.h>
#include <stdlib.h>
#include<string.h>
//void Pfree(struct Student  *PStudent);
typedef struct Student
{
    int ID;
    char name[10];
    float score;
    struct Student *pNextNode;
}                      Student, *PStudent;

//建立节点
PStudent ByeNode(int _ID, char _name[], float _score)
{
    PStudent pNewNode = (PStudent)malloc(sizeof(struct Student));   
    if (NULL != pNewNode)         
    {
        pNewNode->ID = _ID;
        strcpy(pNewNode->name , _name);
        pNewNode->score = _score;
        pNewNode->pNextNode = NULL;     
    }
    return pNewNode;
}

// 尾插法建立单链表
void InsertTail(PStudent* pHead, int _ID, char _name[], float _score)
{
    PStudent pNode = NULL; 
    PStudent pNewNode = NULL;
    //  assert(pHead);
    if (NULL == (*pHead))     
    {
        *pHead = ByeNode(_ID, _name, _score); 
    }
    else
    {
        pNode = *pHead;
        while (NULL != pNode->pNextNode)     
        {
            pNode = pNode->pNextNode;
        }
        pNewNode = ByeNode(_ID, _name, _score); 
        pNode->pNextNode = pNewNode;
    }
}
void SortList(PStudent pHead)       
{
    int IDTemp;
    char nameTemp[10];
    float scoreTemp;
    int flag = 0;
    PStudent pTailNode = NULL;     


    if (NULL == pHead)          
    {
        return ;
    }
    else
    {
        flag = 0;
        pTailNode = NULL;
        while (pTailNode != pHead)  
        {
            PStudent pPreNode = pHead;   
            while (pPreNode->pNextNode != pTailNode)    
            {
                PStudent pCurNode = pPreNode->pNextNode;  
                if (pPreNode->score < pCurNode->score)
                {
                    IDTemp = pPreNode->ID;
                    strcpy(nameTemp , pPreNode->name);
                    scoreTemp = pPreNode->score;

                    pPreNode->ID = pCurNode->ID;
                    strcpy(pPreNode->name , pCurNode->name);
                    pPreNode->score = pCurNode->score;

                    pCurNode->ID = IDTemp;
                    strcpy(pCurNode->name, nameTemp);
                    pCurNode->score = scoreTemp;

                    flag = 1;
                }
                pPreNode = pPreNode->pNextNode;
            }
            if (0 == flag)
            {
                break;
            }
            pTailNode = pPreNode;    
        }
    }
}

//从头到尾打印单链表
void PrintList(PStudent pHead, int n)  
{
    int count = 0;
    PStudent tempPnode = NULL;        
    tempPnode = pHead;         
    if (NULL != pHead)
    {
        while (tempPnode != NULL)   
        {
            if (count < n)
            {
                printf("%-5d  %-8s  %-4.2f\n", tempPnode->ID, tempPnode->name, tempPnode->score);
                tempPnode = tempPnode->pNextNode;     
                count++;
            }
            else if (count == n)
            {
                break;
            }
        }
    }
}
void Pfree(struct Student *PStudent
{
    struct Student *p, *q; 
    p = PStudent;
    while (p->pNextNode != NULL)
    {
        q = p->pNextNode;
        p->pNextNode = q->pNextNode;
        free(q);//1
    }
    free(p);
}

int main(void)
{
    PStudent pList1 = NULL;
    PStudent tempPnode = NULL;        
    int i = 0;
    int tempID = 0;
    char tempname[10];
    float tempscore;
    int n = 0;

    printf("请输入学生人数:\n");
    scanf("%d", &n);          

    for (i = 0; i < n; i++)  
    {
        printf("请输入第%d个学生的ID、姓名和成绩:", i + 1);
        scanf("%d %s %f", &tempID, tempname, &tempscore);     
        //  printf("%d %s %f\n", tempID, tempname, tempscore);
        InsertTail(&pList1, tempID, tempname, tempscore);                   
    }

    printf("所有学生的成绩:\n");
    printf("ID     姓名      成绩\n");
    PrintList(pList1, n);                        
    SortList(pList1);                            
    printf("前三名学生的成绩:\n");
    printf("ID     姓名      成绩\n");
    PrintList(pList1, 3);
    Pfree(pList1);                                
    return 0;
}
  • 4
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值