我终于制作了我的 func,但不能在我的 main 中使用它。编译器错误:
cannot convert
Node' to
Node*' for argument1' to
void add(Node*, Node*)'
谁能帮我解决这个错误?
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
using namespace std;
struct Node
{
int data;
struct Node *next;
};
void add(Node* node, Node* newNode);
int main()
{
struct Node *llist;
struct Node *newNode;
newNode->data = 13;
llist = (Node*)malloc(sizeof(struct Node));
llist->data = 10;
llist->next = (Node*)malloc(sizeof(struct Node));
llist->next->data = 15;
llist->next->next = NULL;
add(llist,newNode);
printf("test\n");
struct Node *cursor = llist;
while (cursor != NULL)
{
printf("%d\n", cursor->data);
cursor = cursor->next;
}
system("pause");
return 0;
}
void add(Node* insertafter, Node* newNode)
{
newNode->next = insertafter->next;
insertafter->next = newNode;
}
最佳答案
应该是void add(struct Node* node, struct Node* newNode);
。
或:
struct Node
{
int data;
struct Node *next;
}Node;
此外,请注意,在为实际结构分配空间之前,您将值赋给了 newNode
的字段,这是一个指针:
newNode = malloc(sizeof(stuct Node));
还有一件事 - 如果这是 C 而不是 C++,您应该删除 using namespace std;
关于c++ - 我终于在链表中创建了我的添加函数,但不能在 main 中使用。 :(,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6077986/