【发布时间】:2014-12-04 08:32:30
【问题描述】:
所以这对于我所在的课程来说是个问题。规定我们使用下面的代码作为我们的主要不编辑它。
int main(void) {
// a reference to the head of our list
node *head = NULL;
node ** list = &head;
// the nodes we will put in our list
node a = {6,NULL};
node b = {8,NULL};
node c = {4,NULL};
node d = {10,NULL};
node e = {2,NULL};
// build the list
append(list, &a);
append(list, &b);
prepend(list, &c);
append(list, &d);
prepend(list, &e);
// print the list
print(list);
// test the find function
int value;
for (value = 1; value <= 10; value++) {
node * found = find(list, value);
if (found == NULL)
printf("Node %d not found...\n", value);
else
printf("Found node %d!\n", found->content);
}
// test delete function
delete(list, 4);
delete(list, 8);
print(list);
return 0;
}
我们需要自己创建 main 中使用的所有函数。目前正在处理附加功能。有人告诉我 append 函数应该是这样的:append(node * list, node * new_node);
tydef stuct node_t {
int content;
struct node_t *next;
} node;
这就是我的节点声明。
void append(node ** list, node * new_nodes) {
node ** current = list;
while ((*current)->next != NULL) {
(*current) = (*current)->next;
}
(*current)->next = new_node;
list = current;
}
这是我的附加功能。我相对确定最后一行是错误的,但我一开始就不知所措。任何想法或建议都会很棒。
【问题讨论】:
-
最好的办法是绘制节点图,并在阅读代码时仔细更改图上的指针。
标签: c linked-list nodes