我正在编写一个用于在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
s。
UPD:感谢所有人的帮助!
答案 0 :(得分:1)
您正尝试使用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;
}
。清理列表应如下所示:
{{1}}
希望这会有所帮助:)
答案 1 :(得分:0)
NewStruct-&gt; next始终为0.这是您所期望的吗?
此外,您可能希望将结构排序为一个单元而不是改变人们的年龄!