私のコードでは、変数を次のclass_tに進めようとしているにもかかわらず、検索と呼ばれる同じclass_t変数を出力する無限ループがあるようです。すべての class_t 構造体は (class_t*)0 を指すか (class_t* と void* を比較していたために NULL を使用するとコンパイラの警告が表示されたため)、または次の適切な class_t 構造体を指します。私は何を間違っていますか、それとも私の問題を別の場所で探す必要がありますか?
class_t *search = (students + i)->class_p;//students is a seperate structure where class_p is a pointer to a class_t structure
while(search != (class_t*)0)
{
fprintf(output,": %s%d %d %d\n", search->name, search->number, search->section, search->credits);
search = search->nextClass;
}
出力のサンプルを次に示します。これを見ると、ファイルから class_t で最後に読み取られたものです。
: CS521 1 4
: CS521 1 4
: CS521 1 4
: CS521 1 4
: CS521 1 4
: CS521 1 4
: CS521 1 4
: CS521 1 4
: CS521 1 4
: CS521 1 4
: CS521 1 4
: CS521 1 4
class_t の作成は次のとおりです。
class_t newClass;
newClass.number = classid;
newClass.section = section;
newClass.credits = credits;
newClass.nextClass = (class_t*)0;
ノードが追加されると、次のようになります。
void addNode(student_t students[], class_t addClass, int ref)
{
int found = 0;
if((students + ref)->class_p == (class_t*)0)//no classes yet
{
(students + ref)->class_p = &addClass;
found = 1;
}
else if((*((students + ref)->class_p)).number > addClass.number && found == 0)//checks first class
{
class_t *temp = (students + ref)->class_p;
(students + ref)->class_p = &addClass;
addClass.nextClass = temp;
found = 1;
}
else//works way through the class linked list to find where it goes
{
class_t *beforesearch = (students + ref)->class_p;
class_t *search = beforesearch->nextClass;
while(search != (class_t*)0 && found == 0)
{
if(search->number < addClass.number)
{
beforesearch->nextClass = &addClass;
addClass.nextClass = search;
found = 1;
}
else
{
beforesearch = search;
search = search->nextClass;
}
}
if(found == 0)
{
beforesearch->nextClass = &addClass;
found = 1;
}
}
}
typedef を含むヘッダー ファイル:
typedef struct class_t {
char name[3];
int number;
int section;
int credits;
struct class_t *nextClass;
} class_t;
typedef struct student_t {
int id;
class_t *class_p;
} student_t;