【问题标题】:Doubly linked list always contains only 1 record双向链表始终只包含 1 条记录
【发布时间】:2013-02-10 10:42:23
【问题描述】:

我正在编写一个使用 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:感谢大家的帮助!

【问题讨论】:

  • 欢迎来到 Stack Overflow。您是否有特殊原因避免使用std::liststd::sort
  • std::string 用于存储学生姓名...
  • 另外请学习如何正确格式化您的代码,这样人们才能阅读它。
  • 是的。这是我的家庭作业,我必须使用 structurse。
  • 对不起代码,我只是粘贴文本。

标签: c++ gcc linked-list structure doubly-linked-list


【解决方案1】:

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

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

但是您将新的Structs 添加到列表中的方式是错误的,因为您总是将next 设置为0

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

即使你分配了 3 个新的Structs,它们都会有指向next 的指针等于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;
}

希望这会有所帮助:)

【讨论】:

  • 非常感谢您!但是,我还有一个问题。当我的程序尝试使用函数 delete_all_list 时,它会返回内存问题。我做错了什么?
【解决方案2】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-15
    相关资源
    最近更新 更多