我终于制作了我的函数,但不能在我的主语言中使用它。 编译器错误如下:
无法将
Node' to
节点*'转换为参数1' to
无效添加(节点*,节点*)'
有人可以帮助我解决错误吗?
#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("testn");
struct Node *cursor = llist;
while (cursor != NULL)
{
printf("%dn", 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;