【发布时间】:2015-11-12 01:57:09
【问题描述】:
所以我一直在做关于数据结构的家庭作业,几个小时以来我都陷入了死胡同。为了解释,我必须在文本文件 ListJava 和 ListDS 中获取以下格式的信息:Name Surname NUM Grade。这两个文件包含相同的名称但顺序不同。作业基本上是要我们对文件进行合并排序。
这些是我的结构:
typedef struct student
{
string name;
string surname;
int am;
int grade;
}StudentFile;
typedef struct node {
StudentFile element;
struct node* next;
}Node;
typedef struct stud
{
string name;
string surname;
int am;
int grade;
int grade2;
struct stud* next;
}Student;
这是我合并它们的函数:
/*Merge Lists into one*/
Student* MergeLists(Node* headDS, Node* headJava, Student* head)
{
bool flag = false;
Student *a = new Student;
Student *prev = NULL;
Student *temp = NULL;
Node *tempDS = headDS;
Node *tempJava = headJava;
Node *prevJava = NULL;
if (head == NULL)
{
head = a; //mermory alocation for head<Student>
temp = head;
// temp->next = NULL;
}
while (tempDS != NULL)
{
if(head != NULL)
{
if (tempDS->element.surname.compare(tempJava->element.surname) == 0) // if surnames are equal
{
prev = temp;
temp->name = tempDS->element.name;
temp->surname = tempDS->element.surname;
temp->am = tempDS->element.am;
temp->grade = tempDS->element.grade;
temp->grade2 = tempJava->element.grade;
tempJava = tempJava->next;
tempDS = tempDS->next;
temp = temp->next;
flag = false; //meaning that prevJava can get a new value again.
}
else // if DS > Java
{
/*Keep tempJava in mermory while iterating through the next nodes to find the temp that is equal to DS*/
if (flag == false)
{
prevJava = tempJava;
tempJava = tempJava->next;
flag = true;
}
else
{
tempJava = tempJava->next;
}
}
/*temp = temp->next;
tempJava = tempJava->next;
tempDS = tempDS->next;*/
}
prev->next = a;
}
a->next = NULL;
return a;
}
问题出在 temp = temp-> 下一行。虽然第一次运行非常好,然后正确搜索 ListJava 以找到与 ListDS 相同的名称临时值是 0xcdcdcdcd {...} 并且它抛出了一个异常:
Exception thrown at 0x00C38EF0 in Exercise3_zitima2.exe: 0xC0000005: Access violation reading location 0xCDCDCDE5.
我该如何应对这个错误,我真的到处搜索尝试过的东西,但似乎没有任何效果。我知道这当然不是一个找人解决我的任务的地方,只需要一点指导。
【问题讨论】:
-
请注意,虽然它有效,但您不必执行
typedef struct student{ }StudentFile;,而是可以执行struct StudentFile{ };- 它更清晰且几乎相同。在this post 中了解更多信息。 -
^这真的很丰富!感谢分享:)