【发布时间】:2016-12-11 13:02:35
【问题描述】:
我真的不明白我的代码有什么问题。
当我添加第一个元素时一切都很好,但在那之后,它就不起作用了。当 ptr 为 NULL 时,它进入 while 循环。 检查是否为null有问题吗?
struct Car
{
int startTime;
char *model;
char *code;
char *location;
struct Car *next;
int deptEnterTime;
bool waitingForFraming;
bool waitingForPainting;
bool waitingForPolishing;
bool waitingForEngine;
bool waitingForElectronic;
bool waitingForIndoor;
bool waitingForTest;
};
struct Car *head = NULL;
void insert(int startTime, char *model, char *code)
{
/*create a link*/
struct Car *link = (struct Car*) malloc(sizeof(struct Car));
link->startTime= startTime;
link->model = model;
link->code = code;
link->waitingForFraming=true;
link->waitingForPainting=false;
link->waitingForPolishing=false;
link->waitingForEngine=false;
link->waitingForElectronic=false;
link->waitingForIndoor=false;
link->waitingForTest=false;
if(head == NULL)
{
head = link;
}
else
{
struct Car *ptr;
ptr = head->next;
while(ptr != NULL)
{
ptr = ptr->next;
}
ptr = link;
}
}
【问题讨论】: