【发布时间】:2016-10-06 11:32:47
【问题描述】:
我无法将元素添加到我的链接列表中。我已经能够验证 char 字符串是否在 main() 中被正确扫描以及被复制到临时节点但是这没有被添加到链表中。
struct node{
char string[MAX_STRING_LENGTH];
node *next;
};
void insertAsFirstElement(node *head, node *last, char string[MAX_STRING_LENGTH])
{
//create temporary node
node *temp = new node;
printf("The temp's string before copy is %s.\n", temp->string);
//this is the string copy from the user to the temp
strcpy(temp->string, string);
printf("The temp's string is %s.\n", temp->string);
//set the next pointer to NULL (points to nothing)
temp->next = NULL;
//set first value in list to temp
head = temp;
//set last value to temp
last = temp;
}
【问题讨论】:
-
欢迎来到 SO!请参考以下link 了解如何提出一个好的问题。当您只转储所有代码时,很难看出问题出在哪里。
-
C 和 C++ 是不同的语言。这是 C++,不是 C!你只是将 C 编码风格混合到 C++ 中,这是一个非常糟糕的主意。
-
我同意蒂姆和奥拉夫的观点。您不应该混合使用 C 和 C++。国际海事组织 Tim 提出的良好解决方案。
标签: c++ c arrays string linked-list