1

我正在编写一个简单的程序来处理 C++ 中的结构,但是有一个我无法解决的问题。

我的程序接收很少的结构作为输入。它应该通过键对它们进行排序并打印它们。但是对于我的代码,我的列表中总是只有一个结构:

#include "iostream"
#include "string.h"
#include "limits"  //ignore max
#include "stdlib.h"//atof
using namespace std; 

struct Struct {
    char text[10];
    int age;
    Struct* prev;
    Struct* next;
};

int input(string msg) {
    char str[2];
    int check = 0, len = 0,
    var = 0,
        i = 0;
    while (1) {
        cout << msg;
        cin.getline(str, 2);
        if (cin.fail()) {
            cin.clear();
            cin.ignore(numeric_limits<streamsize>::max(), '\n');
        }
        len = strlen(str);
        check = 0;
        for (i = 0; i < len; i++) {
            if (isdigit(str[i])) {
                check++;
            }
        }
        if (check == len && !(check == 1 && str[0] == '-') && check != 0 && atoi(str) != 0) {
            var = atoi(str);
            return var;
        } else {
            cout << "Error!" << endl;
        }

    }
}

Struct* add_struct_to_list(Struct* prev) {
    Struct* NewStruct = 0;
    char str[10];
    int age;
    cout << "Name: ";
    cin.getline(str, 10);
    if (cin.fail()) {
        cin.clear();
        cin.ignore(numeric_limits <streamsize>::max(), '\n');
    }
    age = input("Age: ");
    NewStruct = new Struct;
    strcpy(NewStruct->text, str);
    NewStruct->age = age;
    NewStruct->prev = prev;
    NewStruct->next = 0;
    return NewStruct;
}

Struct* start_new_list(int number) {
    Struct* NewList = 0;
    NewList = add_struct_to_list(0);
    Struct* NewStruct = NewList;
    int counter = 1;
    for (counter; counter < number; counter++) {
        NewStruct = add_struct_to_list(NewStruct);
    }
    return NewList;
}

void delete_all_list(Struct* list_begin) {
    Struct* to_delete = list_begin->next;
    Struct* next = 0;
    delete[] list_begin;
    if (to_delete != 0) {
        do {
            next = to_delete->next;
            delete[] to_delete;
        } while (next != 0);
    }
}

void sort_by_age(Struct* list_begin) {
    Struct* node = 0;
    Struct* node2 = 0;
    int age;
    for (node = list_begin; node; node = node->next) {
        for (node2 = list_begin; node2; node2 = node2->next) {
            if (node->age < node2->age) {
                age = node->age;
                node->age = node2->age;
                node2->age = age;
            }
        }
    }
}

void print_list(Struct* list_begin) {
    for (Struct* node = list_begin; node; node = node->next) {
        cout << "Age: " << node->age << "; Name: " << node->text << endl;
    }
}

int main() {
    int number = input("Number of students: ");
    Struct* NewList = start_new_list(number);
    sort_by_age(NewList);
    print_list(NewList);
    delete_all_list(NewList);
    return 0;
}

输入:

 Number of students: 3
 Name: as
 Age: 1
 Name: as
 Age: 2
 Name: as
 Age: 3

输出:

Age: 1; Name: as

另请注意,这是作业,我必须使用structs.

UPD:感谢大家的帮助!

4

2 回答 2

1

node->next您正在尝试使用指针遍历您的列表:

for (Struct* node = list_begin; node; node = node->next) {
    cout << "Age: " << node->age << "; Name: " << node->text << endl;
}

但是您将 new Structs 添加到列表中的方式是错误的,因为您始终设置next0

Struct* add_struct_to_list(Struct* prev) {
    ...
    NewStruct->prev = prev;
    NewStruct->next = 0;
    return NewStruct;
}

即使您分配 3 个 new Struct,它们都将具有指向nextequal to的指针0。将新添加到列表的正确方法Struct如下所示:

Struct* start_new_list(int number) {
    Struct* prevStruct = NULL;
    Struct* newList = NULL;                 // pointer to the first struct
    for (int counter = 0; counter < number; counter++) {
        Struct* newStruct = add_struct_to_list(prevStruct);
        if (prevStruct)                     // if there was previous struct:
            prevStruct->next = newStruct;   // make it point to new struct
        if (counter == 0)                   // if it is first allocated struct:
            newList = newStruct;            // store its address
        prevStruct = newStruct;             // store last struct as "prev"
    }
    return newList;
}

另请注意,当您通过调用分配内存时new,您应该通过调用释放它delete。您正在使用delete[],分配时应使用new[]。清理您的列表应如下所示:

void delete_all_list(Struct* list_begin) {
    Struct* structToDelete = NULL;
    Struct* node = list_begin;
    while (node->next) {
        structToDelete = node;
        node = node->next;
        delete structToDelete;
    }
    delete node;
}

希望这可以帮助 :)

于 2013-02-10T10:58:49.850 回答
0

NewStruct->next 始终为 0。这是您所期望的吗?

此外,您可能希望将结构作为一个单元进行排序,而不是改变人们的年龄!

于 2013-02-10T11:11:03.057 回答