C 读取包含学生姓名和年龄的文件并按排序顺序显示

C 读取包含学生姓名和年龄的文件并按排序顺序显示,c,file,sorting,linked-list,C,File,Sorting,Linked List,当我运行这个程序时,它只打印到'I'的名称,而不是一直打印到'Z'。我尝试先读取文件并将其内容存储到链接列表中,然后按排序顺序显示内容。下面是程序正在读取的文件和程序本身。请帮忙 文件: #include <stdio.h> #include <stdlib.h> int main() { FILE *fp; struct student { char name[20]; int age; str

当我运行这个程序时,它只打印到'I'的名称,而不是一直打印到'Z'。我尝试先读取文件并将其内容存储到链接列表中,然后按排序顺序显示内容。下面是程序正在读取的文件和程序本身。请帮忙

文件:

#include <stdio.h>
#include <stdlib.h>

int main()
{
    FILE *fp;

    struct student
    {
        char name[20];
        int age;
        struct student *pre, *next;
    };
    struct student *s, *f;
    s = (struct student *)malloc(sizeof(struct student));
    s->pre = NULL;
    s->next = NULL;
    f = s;

    fp = fopen("A.txt", "r");
    if(fp == NULL)
    {
        printf("Not Opened");
        exit(0);
    }

    while(1)
    {
        if(fscanf(fp, "%s %d", s->name, &s->age) == EOF)
        {
            s = s->pre;
            s->next = NULL;
            break;
        }
        else
        {
           s->next = (struct student *)malloc(sizeof(struct student));
           s->next->pre = s;
           s->next->next = NULL;
           s = s->next;
        }
    }

    s = f;

    char ch = 'A';

    while(1)
    {
        if(ch == 'Z'+1)
            break;

        while(1)
        {
            if(f->name[0] == ch)
            {
                printf("%s %d\n", f->name, f->age);
                f->next->pre = f->pre;
                f->pre->next = f->next;
                if(f->next == NULL)
                    break;
                else
                    f = f->next;
            }

            if(f->next == NULL)
                break;
            else
                f = f->next;
        }

        ch = ch +1;
        f = s;
    }

    fclose(fp);
}
萨米尔20

奥雅纳18

尼哈22

阿希姆19

伊萨克21

节目:

#include <stdio.h>
#include <stdlib.h>

int main()
{
    FILE *fp;

    struct student
    {
        char name[20];
        int age;
        struct student *pre, *next;
    };
    struct student *s, *f;
    s = (struct student *)malloc(sizeof(struct student));
    s->pre = NULL;
    s->next = NULL;
    f = s;

    fp = fopen("A.txt", "r");
    if(fp == NULL)
    {
        printf("Not Opened");
        exit(0);
    }

    while(1)
    {
        if(fscanf(fp, "%s %d", s->name, &s->age) == EOF)
        {
            s = s->pre;
            s->next = NULL;
            break;
        }
        else
        {
           s->next = (struct student *)malloc(sizeof(struct student));
           s->next->pre = s;
           s->next->next = NULL;
           s = s->next;
        }
    }

    s = f;

    char ch = 'A';

    while(1)
    {
        if(ch == 'Z'+1)
            break;

        while(1)
        {
            if(f->name[0] == ch)
            {
                printf("%s %d\n", f->name, f->age);
                f->next->pre = f->pre;
                f->pre->next = f->next;
                if(f->next == NULL)
                    break;
                else
                    f = f->next;
            }

            if(f->next == NULL)
                break;
            else
                f = f->next;
        }

        ch = ch +1;
        f = s;
    }

    fclose(fp);
}
#包括
#包括
int main()
{
文件*fp;
结构学生
{
字符名[20];
智力年龄;
结构学生*pre,*next;
};
结构学生*s,*f;
s=(结构学生*)malloc(大小(结构学生));
s->pre=NULL;
s->next=NULL;
f=s;
fp=fopen(“A.txt”、“r”);
如果(fp==NULL)
{
printf(“未打开”);
出口(0);
}
而(1)
{
如果(fscanf(fp,“%s%d”,s->name,&s->age)=EOF)
{
s=s->pre;
s->next=NULL;
打破
}
其他的
{
s->next=(结构学生*)malloc(sizeof(结构学生));
s->next->pre=s;
s->next->next=NULL;
s=s->next;
}
}
s=f;
char ch='A';
而(1)
{
如果(ch='Z'+1)
打破
而(1)
{
如果(f->name[0]==ch)
{
printf(“%s%d\n”,f->name,f->age);
f->next->pre=f->pre;
f->pre->next=f->next;
如果(f->next==NULL)
打破
其他的
f=f->next;
}
如果(f->next==NULL)
打破
其他的
f=f->next;
}
ch=ch+1;
f=s;
}
fclose(fp);
}

似乎您混合了两个概念:排序和打印

如果(f->name[0]==ch)
则打印它并重新链接列表。我不知道你为什么要重新链接它,我没有检查你是否正确排序(我觉得不是)


首先对列表进行排序(例如,实现气泡或使用快速排序)然后打印它,或者像现在一样打印列表,但删除重新链接(然后它会打印得很好-除了
AB
可以在
AA
之前打印,因为您只检查第一个字母)。

问题在于以下几行:

f->next->pre = f->pre;
f->pre->next = f->next;
如果您删除这些,那么列表就可以正常打印。但是,只有打印是有序的,而不是列表。如果您想订购列表,请参阅:


hmm但为什么这两行会在打印中造成问题?因为为了重新排序任何内容,您需要一个临时缓冲区tmp。然后,如果您想交换元素a和b,您必须这样做:tmp=a,a=b,b=tmp。你在这里没有遵循这个原则。@abhirubbakshi如果我的回答涵盖了你的问题,你可以接受它来结束这篇文章。