아래 구조체로 연결된 학생 목록을 만들려고합니다.학생 구조의 링크 된 목록 구현
struct student
{
int student_ID;
char *student_name;
struct course *courses_enrolled;
Student *child;
};
//Insert student to the list with a given student pointer and the starting point
Student *insert_student(Student *child, Student *root)
{
Student *temp = (Student*)malloc(sizeof(Student));
//if there isn't a starting point, declare this as the start point
if(root->student_name == NULL)
{
root->student_ID = child->student_ID;
root->student_name = strdup(child->student_name;);
root->child = NULL;
}
//if this student's name is before current node, replace node.
else if(strcmp(child->student_name, root->student_name) < 0)
{
temp = root;
root = child;
child->child = temp;
}
//if this student's name is after current node, keep doing insert recursion
else if(strcmp(child->student_name, root->student_name) > 0)
{
insert_student(child, root->child);
}
return root;
}
첫 번째 루트 삽입은 항상 잘 작동 것이다 그러나 나는 2를 추가하려고 할 때, 프로그램은 SEG insert_student하는 2 호출 후에 잘못됩니다. 내가 그 의심 비교
if(root->student_name == NULL)
에서 실패 나 루트 (루트 레벨> 아이)의 자식 노드에 접근 함께 할 수있는 뭔가가하지만 난 정말로 확실하지 않다.
p/s : 내가 할당을 해제하지 않는다는 것을 알고 있습니다. 이것은 다른 라이브러리를 사용해야하는 임시 작업 일뿐입니다.
업데이트 : 초과 코드가 제거되었습니다.
네, 그 부분에서 실패하지 않는 것이 맞습니다. 다시 확인해 보았습니다. 실제로 비교에 실패했다고 생각합니다 (이를 반영하기 위해 내 게시물을 편집했습니다). 나는 또한 임시 할당을 제거했다. 그것은 내가 제거한 다른 것을위한 것이라고 생각했다. – rlhh