Ç - 你如何调用链表的第一个元素? [英] C - How do you call the first element in a linked list?
问题描述
我想获得一个链接列表进行排序,然后才能够显示出来。
我的code的问题是,我可以整理之前显示,但排序后,它不会显示,它会崩溃。我认为这是与顶变量,因为通过调试,它不包含任何内容。我怎么能叫链表的第一个元素,并用它来显示它们呢?我真的很迷茫。
下面是只显示和排序功能。
//排序,并显示所有员工
无效displayAllEmps()
{
如果(numEmps == 0)
{
的printf(没有员工被录用。);
fflush(标准输出);
}
其他
{
CHAR输出[80];
结构EMP * EMP =顶部; INT I;
对于(i = 1; I< numEmps;我++)
{
如果(EMP!= NULL)
{
displayEmployee(EMP,输出); 的printf(%S,输出);
fflush(标准输出);
}
EMP = EMP - >下一个;
}}
}//排序函数调用插入排序功能
无效sortEmps()
{
TEMP = NULL;
结构EMP *下一=顶部;而(温度!= NULL)
{
接下来=顶部 - >下一个;
插入(TEMP);
TEMP =下一个;
}顶部=温度;
}//插入排序功能
无效插入(结构EMP * EMP)
{
preV = NULL;
电流=温度;而(电流= NULL&放大器;!&安培; CURRENT-> ID< EMP-和SEQ ID)
{
preV =电流;
电流=电流 - >接着,
}如果($ P $光伏== NULL)
{
TEMP = EMP;
}
其他
{
EMP - >接下来= preV - >下一个;
preV - >接下来= EMP;
}
}
您之类的功能无所事事,除了设置您的列表头NULL,所以你实际上并不带任何内容的清单。在,而
循环从未进入,因为温度
最初定义为 NULL
,所以温度!= NULL
不可能是真的。然后,设置 =顶部温度;
,所以现在顶= NULL
I am trying to get a linked list to sort, then be able to display it. The problem with my code is, I can display it before sorting, but after sorting, it won't display, it will crash. I think it has to do with the "top" variable, because through debugging, it doesn't contain anything. How can I call the first element in the linked list and use that to display them all? I am just really confused. Below is only the display and sort functions.
//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;
}
}
Your "sort" function is doing nothing except setting the head of your list to "NULL," so you don't actually have a list at all any more. The while
loop is never entered, since temp
is initially defined as NULL
, so temp != NULL
can't be true. You then set top = temp;
, so now top = NULL
.
这篇关于Ç - 你如何调用链表的第一个元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!