【发布时间】:2020-11-09 14:39:58
【问题描述】:
当我运行这个程序时,它只打印到“I”而不是一直到“Z”的名称。我尝试先读取文件并将其内容存储到链接列表中,然后按排序顺序显示内容。下面是程序正在读取的文件和程序本身。请帮忙。
文件:
萨米尔 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);
}
【问题讨论】:
标签: c file sorting linked-list