I finally made my func but can’t use it in my main. The compiler errors with:
cannot convert
Node' toNode*’ for argument1' tovoid add(Node*, Node*)’
Can somebody help me resolve the error?
#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;
}
It should be
void add(struct Node* node, struct Node* newNode);.OR:
Also, please note that you asign values to fields of
newNodewhich is a pointer, before allocating space for the actual struct:And one more thing – if this is C and not C++, you should remove
using namespace std;