几周前我刚开始使用C编程,我的程序中出现了分段错误。
我相信这是因为这些方面:
for (int i =0; i < HOW_MANY; i++)
{
people = people -> next;
if (people -> next == NULL) return people;
} //for
}//else
这是我的C程序,其评论基于我的psedocode
struct person *insert_end (struct person *people, char *name, int age) {
//create a new space for the new person
struct person *pointer = malloc(sizeof(struct person));
// check it succeeded
if(pointer == NULL)
{
printf("The program could not allocate memory ");
exit(-1);
}
// set the data for the new person
strcpy(pointer -> name, name);
pointer -> age = age;
pointer -> next = people;
// if the current list is empty
if (people == NULL)
{
// set the new person's "next" link to point to the current list"
pointer -> next = people;
// return a pointer to the new person
return pointer;
}
else
{
// we need a loop to find the last item in the list
// (the one which as a "next" link of NULL)
for (int i =0; i < HOW_MANY; i++)
{
// set the "next link of this item to point
// to the new person, so that the person
// becomes the last item in the list
// (the next person should have a "next" link of NULL)
people = people -> next;
if (people -> next == NULL)
return people;
} //for
}//else
// return the start of the list
return pointer;
}
另外,如果您需要我的完整C代码,请告知我们,因为这只是一种方法
谢谢,
莎拉:)答案 0 :(得分:1)
这完全是Code Review的一个问题,但由于您确实提出了有关如何实现算法的问题,至少我认为它适用于Stack Overflow。
首先,这只是一个语法问题,但我认为你应该替换它:
(*pointer).age = age;
用这个
pointer -> age = age;
在我看来,这种表示法更加清晰,并且为了从结构指针访问元素而被合并到C中。除此之外,你到目前为止的代码缺少一些结束括号。这是您的代码版本,具有正确的语法和其他重要的东西(例如@ H2CO3提到的):
struct person *insert_end (struct person *people, char *name, int age) {
//create a new space for the new person
struct person *pointer = malloc(sizeof(struct person));
// check it succeeded
if(pointer == NULL)
{
printf("The program could not allocate memory ");
exit(-1);
}
// set the data for the new person
strcpy(pointer -> name, name);
pointer -> age = age;
pointer -> next = people;
// if the current list is empty
if (person == 0)
{
// set the new person's "next" link to point to the current list"
pointer -> next = people;
// return a pointer to the new person
return pointer;
}
else
{
// we need a loop to find the last item in the list
// (the one which as a "next" link of NULL)
for (int i =0; i < HOW_MANY; i++)
{
// set the "next link of this item to point
// to the new person, so that the person
// becomes the last item in the list
// (the next person should have a "next" link of NULL)
lastItem -> next = people;
}
// return the start of the list
return pointer;
}
}
最后,由于我没有完整的代码副本,也不需要它,我可以为您提供有关如何实现您不确定的伪代码部分的建议。
使用循环查找列表中的最后一项(即具有a的那一项) NULL的“下一个”链接
查找链表中的最后一项很简单。我在下面制作的以下函数显示了如何在某个索引处获取链表中的特定节点:
NODE* getNode(NODE* start, int index)
{
int i;
for(i = 0; i < index; i++)
{
start = start -> next;
}
return start;
}
可以修改此代码,使其返回最后一个节点。
NODE* getLastNode(NODE* start)
{
for(;;)
{
start = start -> next;
if(start -> next == NULL)
return start;
}
}
上面的代码遍历列表中的每个节点,直到它遇到一个连接节点为NULL
的节点。然后它返回最后一个节点。
现在你可以调用上面的函数并获取最后一个节点。
将此项目的“下一个”链接设置为指向新人,以便新建 人成为名单中的最后一项(即新人应该 有一个NULL的“下一个”链接
我确定你知道如何根据你上面提供的代码来做这件事。