gpt4 book ai didi

c - 以相反的顺序读取输入

转载 作者:太空宇宙 更新时间:2023-11-04 06:13:12 25 4
gpt4 key购买 nike

我是 C 的新手。我刚刚学习了指针和 struct。我正在尝试修改以下程序,以便将每个 student read 插入到 students 列表的前面,而不是在结束。我怎样才能实现它?

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

#define MAX_LINE_LENGTH 80 // The longest line this program will accept
#define MAX_NUM_STUDENTS 500 // The maximum number of students this program can handle
#define MAX_NAME_SIZE 50 // The maximum allowable name length
typedef struct student_s Student;

struct student_s {
char name[MAX_NAME_SIZE];
int age;
Student* next; // Pointer to next student in a list
};
Student studentPool[MAX_NUM_STUDENTS]; // The student pool
int firstFree = 0;
Student* newStudent(const char* name, int age)
{
Student* student = NULL;
if (firstFree < MAX_NUM_STUDENTS) {
student = &studentPool[firstFree];
firstFree += 1;
strncpy(student->name, name, MAX_NAME_SIZE);
student->name[MAX_NAME_SIZE - 1] = '\0'; // Make sure it's terminated
student->age = age;
student->next = NULL;
}
return student;
}
Student* readOneStudent(FILE* file)
{
char buffer[MAX_LINE_LENGTH]; // Buffer into which we read a line from stdin
Student* student = NULL; // Pointer to a student record from the pool

// Read a line, extract name and age

char* inputLine = fgets(buffer, MAX_LINE_LENGTH, file);
if (inputLine != NULL) { // Proceed only if we read something
char* commaPos = strchr(buffer, ',');
if (commaPos != NULL) {
int age = atoi(commaPos + 1);
*commaPos = '\0'; // null-terminate the name
student = newStudent(buffer, age);
}
}
return student;
}
Student* readStudents(FILE *file)
{
Student* first = NULL; // Pointer to the first student in the list
Student* last = NULL; // Pointer to the last student in the list
Student* student = readOneStudent(file);
while (student != NULL) {
if (first == NULL) {
first = last = student; // Empty list case
} else {
last->next = student;
last = student;
}
student = readOneStudent(file);
}
return first;
}
void printOneStudent(Student student)
{
printf("%s (%d)\n", student.name, student.age);
}
void printStudents(const Student* student)
{
while (student != NULL) {
printOneStudent(*student);
student = student->next;
}
}
int main(void)
{
FILE* inputFile = fopen("studlist.txt", "r");
if (inputFile == NULL) {
fprintf(stderr, "File not found\n");
} else {
Student* studentList = readStudents(inputFile);
printStudents(studentList);
}
}

最佳答案

您当前有这段代码要插入到(非空列表的)末尾:

if (first == NULL) {
first = last = student; // Empty list case
} else {
last->next = student;
last = student;
}

要在非空列表的前面插入,只需每次将新学生设为第一个学生,方法是将其设为next。指针指向当前 first学生,并制作first指针指向新生。

if (first == NULL) {
first = last = student; // Empty list case
} else {
student->next = first;
first = student;
}

绘制方框;用箭头连接它们。它应该变得显而易见。


此外,您可以简单地使用:

student->next = first;
first = student;

如果first为空,student->next将(重新)设置为 null,因此不需要 first 上的特殊情况.自 last仅在添加到列表末尾的函数中使用,在前面插入时,不需要 last根本。这两个观察结果使代码比第一个版本更简单。

关于c - 以相反的顺序读取输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51998097/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com