我一直致力于在 C 中实现链表。所以在有人对我大喊大叫之前:是的,这是“家庭作业”。我一直在尝试解决和研究 Nick Parlante 的“链表基础知识”——可在以下网址免费获得:http: //cslibrary.stanford.edu/103
功能:在链表末尾插入元素
我偶然发现了一个实现问题并设法构建了一个解决方法:如果我在 LList 中使用“EndPointer”,我可以使用返回函数来设置函数的 EndPointer 以移交新的 ReferencePointer,然后在 main.xml 中更改它。
代码 - 工作正常,但解决方法:
// within main
lastPtrRef = _pushEnd(lastPtrRef, i);
// == function: push to end
node** _pushEnd(node **endRef, int value)
{
// 1) allocate stack mem / make room for new element
node *newNode = malloc(sizeof(node));
// do the data work
newNode->value = value;
// 2) make element point to NULL (fo beeing the new last element
newNode->next = NULL;
// 3) make old last element point to new element
*endRef = newNode;
return &(newNode->next); // more readable then vers. below
// this returns the mem address only of the pointer of the node!!!
//return (&((*endRef)->next));
}
==================================================== ==============================
到目前为止,这就是我在函数中所做的所有工作,但它实际上不起作用。关于我没有得到哪一点的任何提示?!
void _pushEnd(node **endRef, int value)
{
// 1) allocate stack mem / make room for new element
node *newNode = malloc(sizeof(node));
// do the data work
newNode->value = value;
// 2) make element point to NULL (fo beeing the new last element
newNode->next = NULL;
// 3) make old last element point to new element
*endRef = newNode;
}
难道是,我实际上需要一个指向引用指针的指针来实际更改指向最后一个元素的指针的内容(在范围内:main),所以我目前似乎只是在修改局部变量“endRef”和不是它的内容?!
任何帮助,将不胜感激...
编辑:这个想法是在 LList 的开头不使用虚拟节点来追加。
我的结构如下所示:
typedef struct node {
int value;
struct node *next; } node;
主要 - 局部变量(堆栈):
node *head = NULL;
node **lastPtrRef = &head;
编辑:无论如何,大多数命题最终都返回了 refPointer。但也许这不是一个坏主意,因为它不需要另一个指向 refPointer 的 refPointer。
感谢您的所有帮助和许多有用的评论!