以下は、リンクリストコードを使用した挿入ソートです。私はこれを他に類を見ないほどデバッグしましたが、それをソートする方法を理解できません。現在のところ、insert()のifステートメントに入る無限ループに入ります。何を変更する必要がありますか?
//Sort function to call insertion sort function
void sortEmps()
{
temp = NULL;
struct EMP* next = top;
while(top != NULL)
{
next = top -> next;
insert(temp);
temp = next;
}
top = temp;
}
//Insertion sort function
void insert(struct EMP *emp)
{
prev = NULL;
current = temp;
while (current != NULL && current->id < emp->id)
{
prev = current;
current = current->next;
}
if (prev == NULL)
{
temp = emp;
}
else
{
emp -> next = prev -> next;
prev -> next = emp;
}
}
これが私の構造体と追加関数です。並べ替えの前に使用されるのはほとんど唯一のものです。たくさんの従業員を初期化できるので、それらは保存されます。
typedef struct EMP
{
int id;
char name [MAX];
double salary;
struct EMP* next;
} EMPLOYEE;
int addEmployee(char* name, double salary)
{
struct EMP* emp = createEmployee(name, salary);
emp -> next = top;
top = emp;
numEmps++;
//employees[numEmps++] = emp;
return TRUE;
}