更改结构内的调用

标签 c struct call

鉴于我在下面创建的结构,我将如何更改代码以使其从插入(学生)而不是现在的方式读取。基本上,目前的代码以学生的身份输入:

student guy,23

输出将是:

student guy (23)

到目前为止,这部分工作正常。插入(和之前)代码将做的是当输入多个学生时,按字母顺序对他们进行排序 - 或者如果他们有相同的名字,则按年龄排序。我已经完成了执行此操作的代码,但似乎无法弄清楚如何正确调用它。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <stdbool.h>
#include <assert.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

// The declaration of the student record (or struct). Note that
// the struct contains the name as an array of characters, rather than
// containing just a pointer to the name as before.

typedef struct student_s Student;

struct student_s {
    char name[MAX_NAME_SIZE];
    int age;
    Student* next;              // Pointer to next student in a list
};

bool comesBefore(const Student* student1, const Student* student2) {
    int name_compare = strcmp(student1->name, student2->name);

    if (name_compare < 0) {
        return true;
    }
    else if (name_compare == 0) {
        int age1 = student1->age;
        int age2 = student2->age;
        if (age1 < age2) {
            return true;
        }
    }
    return false;
}

Student* insert(Student* student, Student* list) {
    Student* curr = NULL;
    Student* prev = NULL;
    if (list == NULL)
        return student;

    if (comesBefore(student, list)) {
        student->next = list;
        return student;
    }

    for (curr = list, prev = NULL;
         curr != NULL && comesBefore(student, curr) != true;
         prev = curr, curr = curr->next);

    assert(prev != NULL);

    student->next = curr;
    prev->next = student;
    return list;
}


// Create a pool of student records to be allocated on demand

Student studentPool[MAX_NUM_STUDENTS];  // The student pool
int firstFree = 0;
// Return a pointer to a new student record from the pool, after
// filling in the provided name and age fields. Returns NULL if
// the student pool is exhausted.
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;
}

// Read a single student from a csv input file with student name in first column,
// and student age in second.
// Returns: A pointer to a Student record, or NULL if EOF or an invalid
// student record is read. Blank lines, or lines in which the name is
// longer than the provided name buffer, or there is no comma in the line
// are considered invalid.
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* cp = fgets(buffer, MAX_LINE_LENGTH, file);
    if (cp != NULL) {           // Proceed only if we read something
        char* commaPos = strchr(buffer, ',');
        if (commaPos != NULL && commaPos > buffer) {
            int age = atoi(commaPos + 1);
            *commaPos = '\0';  // null-terminate the name
            student = newStudent(buffer, age);
        }
    }
    return student;
}

// Reads a list of students from a given file. Input stops when
// a blank line is read, or an EOF occurs, or an illegal input
// line is encountered.
// Returns a pointer to the first student in the list or NULL if no
// valid student records could be read.
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;
}


// printOneStudent: prints a single student, passed by value
void printOneStudent(Student student)
{
    printf("%s (%d)\n", student.name, student.age);
}


// printStudents: print all students in a list of students, passed
// by reference
void printStudents(const Student* student)
{
    while (student != NULL) {
        printOneStudent(*student);
        student = student->next;
    }
}

// Main program. Read a linked list of students from a csv file, then display
// the contents of that list.
int main(void)
{
    FILE* inputFile = stdin;
    if (inputFile == NULL) {
        fprintf(stderr, "File not found\n");
    }
    else {
        Student* studentList = readStudents(inputFile);
        printStudents(studentList);

        // The program could now do various things that make use of
        // the linked list, like deleting students and adding new ones,
        // but the program is already quite long enough!
    }
}

我认为它需要对 readStudents 进行某种编辑,但无论我阅读过何种源 Material ,都无法确定要进行哪些调用更改。

最佳答案

关键思想是你需要调整这个:

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

目前您将新学生放在列表的末尾。相反,您将按排序顺序保存列表。在概念上你有

 Arthur -> Bill -> Dave

还有一个新记录,查尔斯。因此,您首先与 Charles 进行比较,发现 Charles 更大,然后继续与 Bill 进行比较,然后最终与 Dave 进行比较,现在知道在 Bill 之后和 Dave 之前插入 Charles。

现在看一下 insert() 函数。你能看出这是它在做什么吗?遍历列表并最终插入记录 - 注意它如何调整“Bill”以指向新记录,并将新记录指向 Dave。

关于更改结构内的调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32420250/

相关文章:

c - math.h 默认舍入模式不明确

c++ - 结构上未声明的标识符

c - 非 NULL 用户输入

mysql - 我可以像这样从 select 调用 mysql SP 吗?

对没有 void 的调用方法感到困惑

java - 为什么 Java 中的这个存储过程调用不起作用?

c - gcc 不会编译 SDL C 程序(对 SDL 函数的 undefined reference )

c++ - 如何使用 getProcAddress() 而不进行类型转换?

c - 为什么将小 float 添加到大 float 只会降低小 float ?

c++ - 如何将结构定义为结构的成员?