gpt4 book ai didi

c++ - 双向链表总是只包含 1 条记录

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

我正在编写一个简单的程序来处理 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

另请注意,这是家庭作业,我必须使用struct

UPD:感谢大家的帮助!

最佳答案

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

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

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

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

即使您分配了 3 个新的 Struct,它们所有指向 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;
}

希望这有帮助:)

关于c++ - 双向链表总是只包含 1 条记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14796872/

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