鱼C论坛

 找回密码
 立即注册
查看: 963|回复: 6

[已解决]链表学生管理为什么学号降序直接强制退出呢?

[复制链接]
发表于 2021-7-19 17:56:26 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
#include <stdio.h>
#include<stdlib.h>
struct student{
        char name[20];
        int num;
        int score;
};
struct Node{
        struct student data;//自定义类型数据域data
        struct Node *next;
};
struct Node *createlist()//创建链表
{
        struct Node *headNode = (struct Node*)malloc(sizeof(struct Node));
        headNode->next = NULL;
        return headNode;
}
struct Node *createNode(struct student data)//创建节点
{
        struct Node *newNode = (struct Node*)malloc(sizeof(struct Node));
        newNode->data = data;
        newNode->next = NULL;
        return newNode;
}
void printlist(struct Node *headNode)//打印链表
{
        struct Node *pMove = headNode->next;
        printf("name\tnum\tscore\n");
        while(pMove)
        {
                printf("%s\t%d\t%d\n",pMove->data.name,pMove->data.num,pMove->data.score);
                pMove = pMove->next;
        }
}
void insertNodeByHead(struct Node *headNode,struct student data)//插入节点(头插法)
{
        struct Node *newNode = createNode(data);
        newNode->next = headNode->next;
        headNode->next = newNode;
}
void deleteNodeByAppoinNum(struct Node *headNode,int num)//删除节点
{
        struct Node *posNode = headNode->next;
        struct Node *posNodeFront = headNode;
        if(posNode == NULL)
        {
                printf("链表为空无法删除\n");
        }
        else
        {
                while(posNode->data.num != num)
                {
                        posNodeFront = posNode;
                        posNode = posNodeFront->next;
                        if(posNode == NULL)
                        {        
                                printf("没有找到无法删除\n");
                                break;
                        }
                }                
                posNodeFront->next = posNode->next;
                free(posNode);
        }
}
void modifyNodeByAppoinNum(struct Node *headNode,int num)//修改节点
{
        struct Node *posNode = headNode->next;
        struct Node *posNodeFront = headNode;
        if(posNode == NULL)
        {
                printf("链表为空无法修改\n");
        }
        else
        {
                while(posNode->data.num != num)
                {
                        posNodeFront = posNode;
                        posNode = posNodeFront->next;
                        if(posNode == NULL)
                        {        
                                printf("没有找到无法修改\n");
                                return;
                        }
                }        
                printf("请输入学生姓名,学号,成绩:\n");
                scanf("%s%d%d",posNode->data.name,&posNode->data.num,&posNode->data.score);
                printf("修改成功!\n");
        }                
}
void searchNodeByAppoinNum(struct Node *headNode,int num)//查找节点
{
        struct Node *posNode = headNode->next;
        struct Node *posNodeFront = headNode;
        if(posNode == NULL)
        {
                printf("链表为空\n");
        }
        else
        {
                while(posNode->data.num != num)
                {
                        posNodeFront = posNode;
                        posNode = posNodeFront->next;
                        if(posNode == NULL)
                        {        
                                printf("没有找到\n");
                                return;
                        }
                }
                printf("name\tnum\tscore\n");
                printf("%s\t%d\t%d\n",posNode->data.name,posNode->data.num,posNode->data.score);
        }
}
struct Node *DescendingByNum(struct Node *headNode)//学号降序
{
        struct Node *p = headNode->next;
        struct Node *pre = headNode;
        struct Node *temp = NULL,*tail = NULL;
        
        while((headNode->next->next)!=tail)
        {
                while(p->next != tail)
                {
                        if((p->data.num) < (p->next->data.num))
                        {
                                pre->next = p->next;
                                temp = p->next->next;
                                p->next->next = p;
                                p->next = temp;
                                p = pre->next;
                        }
                        p = p->next;
                        pre = pre->next;
                }
                tail = p;
        }
        return headNode;
}
void Save(struct Node *headNode)//写入文件
{
        FILE *fp = fopen("student.dat","w");
        
        if(headNode == NULL)
                return;
        struct Node *pMove = headNode->next;
        while(pMove)
        {
                fprintf(fp,"%-8s%-8s%-8s\n%-8s%-8d%-8d\n","name","num","score",pMove->data.name,pMove->data.num,pMove->data.score);
                pMove = pMove->next;
        }
        fclose(fp);
        printf("写入成功\n");
}
void Read(struct Node *headNode)//读取文件
{
        FILE *fp = fopen("student.dat","r");
        char ch;
        
        if (fp == 0)
        {
                printf("读取失败!文件不存在\n");
                return;
        }
        else
        {
                while(EOF != (ch  =fgetc(fp)))
                {
                        putchar(ch);
                }
                fclose(fp);
                printf("读取成功!\n");
        }
}
void main()
{
                
        char choice,type;
        int run = 1,searchnum;
        struct Node *list = createlist();
        struct student info;
        
        system("mode con cols=100 lines=30");
        printf("Management for Students' scores\n");
    printf("1.Append record\n");
        printf("2.List record\n");
        printf("3.Delete record\n");
        printf("4.Modify record\n");
        printf("5.Search record\n");        
        printf("6.Sort Score in descending order by sum\n");
        printf("7.Sort Score in ascending order by sum\n");
        printf("8.Sort Score in descending order by num\n");
        printf("7.Sort Score in ascending order by num\n");
    printf("i.Insert Sort Score in ascending order by sum\n");
        printf("w.Write to a File\n");
        printf("r.Read from a File\n");        
        printf("0.Exit\n");
        
        do{
                printf("Please Input your choice:");
                fflush(stdin);
                scanf("%c",&type);
                switch(type){
                case '0':
                        run = 0;
                        break;
                case '1':
                        printf("请输入学生姓名 学号 成绩\n");
                        scanf("%s%d%d",info.name,&info.num,&info.score);
                        insertNodeByHead(list,info);
                        while(1)
                        {
                                printf("是否继续(Y/N)?\n");
                                fflush(stdin);
                                choice = getchar();
                                if(choice == 'N'||choice == 'n')
                                        break;
                                else
                                        printf("请输入学生姓名 学号 成绩\n");
                                        scanf("%s%d%d",info.name,&info.num,&info.score);
                                        insertNodeByHead(list,info);
                        }
                        break;
                case '2':
                        printlist(list);
                        break;
                case '3':
                        printf("请输入学号用于删除:\n");
                        scanf("%d",&searchnum);
                        deleteNodeByAppoinNum(list,searchnum);
                        break;
                case '4':
                        printf("请输入学号用于修改:\n");
                        scanf("%d",&searchnum);
                        modifyNodeByAppoinNum(list,searchnum);
                        break;
                case '5':
                        printf("请输入学号用于检索:\n");
                        scanf("%d",&searchnum);
                        searchNodeByAppoinNum(list,searchnum);
                        break;
                case '6':
                        break;
                case '7':
                        break;
                case '8':
                        DescendingByNum(list);
                        break;
                case '9':
                        break;
                case 'w':
                        Save(list);
                        break;
                case 'r':
                        Read(list);
                        break;
                default :
                        break;                
        }}while(run);         
}
最佳答案
2021-7-19 20:21:54
老K哥 发表于 2021-7-19 19:11
单纯结构体时候二层for循环好点,链表的话我勉强改成这样

可以参考这个https://blog.csdn.net/weixin_443 ... 1018.2226.3001.4187
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-7-19 18:46:41 | 显示全部楼层
为啥我还能正常运行

                               
登录/注册后可看大图
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-7-19 18:54:02 | 显示全部楼层
大马强 发表于 2021-7-19 18:46
为啥我还能正常运行

三个及以上就不行了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-7-19 18:55:20 | 显示全部楼层
哦哦,我发现了,我再看看
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-7-19 19:01:17 | 显示全部楼层
建议搞一个嵌套循环来排序,每次找出最大的排在前面,我先去吃饭回来再看看
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-7-19 19:11:09 From FishC Mobile | 显示全部楼层
大马强 发表于 2021-7-19 19:01
建议搞一个嵌套循环来排序,每次找出最大的排在前面,我先去吃饭回来再看看

单纯结构体时候二层for循环好点,链表的话我勉强改成这样
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-7-19 20:21:54 | 显示全部楼层    本楼为最佳答案   
老K哥 发表于 2021-7-19 19:11
单纯结构体时候二层for循环好点,链表的话我勉强改成这样

可以参考这个https://blog.csdn.net/weixin_443 ... 1018.2226.3001.4187
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-7-4 00:25

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表