-1
下面是我使用链接列表代码的插入排序。我已经调试过,没有其他的,但无法弄清楚如何进行排序。因为它现在就位于insert()中,所以它进入if语句的无限循环。我需要改变什么?C-插入排序链接列表
//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;
}
要求人们代码中的现货错误并不是特别有效。您应该使用调试器(或添加打印语句)来隔离问题(即其行为与您预期/期望的行为不同),然后构造一个[最小测试用例](http://sscce.org)。 – 2013-02-17 20:35:43
试图找到错误,但缺少关键部分(main()和struct-def),所以我无法找到它。 – 2013-02-17 20:49:23
我下来投票,因为我看到相同的问题,其中已经问过你的相同的代码。 – Michael 2013-02-18 06:03:07