リンクされたリストをソートして表示できるようにしようとしています。私のコードの問題は、ソート前に表示できますが、ソート後に表示されず、クラッシュすることです。デバッグでは何も含まれていないため、「トップ」変数に関係していると思います。リンクされたリストの最初の要素を呼び出し、それを使用してすべてを表示するにはどうすればよいですか? 私は本当に混乱しています。以下は、表示とソート機能のみです。
//Sort and display all employees
void displayAllEmps()
{
if(numEmps == 0)
{
printf("No employees are hired.");
fflush(stdout);
}
else
{
char output[80];
struct EMP* emp = top;
int i;
for(i = 1; i < numEmps; i++)
{
if (emp != NULL)
{
displayEmployee(emp, output);
printf("%s", output);
fflush(stdout);
}
emp = emp -> next;
}
}
}
//Sort function to call insertion sort function
void sortEmps()
{
temp = NULL;
struct EMP* next = top;
while(temp != 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;
}
}