0

I am trying to insert values from the binary search tree to linkedlist.

  • insertToLinkedList , I am using this method. But there is a problem with assigning.
  • struct node
  • struct nodeForLinkedList
struct node
{// Here node struct for leafs of the Tree
   
    char word[STRING_LEN]; //words
    struct node *left;
    struct node *right;
   
    
};
struct nodeForLinkedList
{
    
    char word[STRING_LEN]; //words
    struct node *left;
    struct node *right;
    struct node *next;
    
}
void insertToLinkedList(nodeForLinkedList *nodeL,char *data){
    struct nodeForLinkedList* new_node = (struct nodeForLinkedList*)malloc(sizeof(struct nodeForLinkedList)); 
    
     strcpy(new_node->word, data);                                       // To copy the elements of data to new node's word
    
    new_node->next = *nodeL; // Error occurs here.**(1)**
    *nodeL = new_node;// and here**(2)**


} 

Errors:

no suitable conversion function from "nodeForLinkedList" to "node *" existsC/C++(413) (1)

no operator "=" matches these operands -- operand types are: nodeForLinkedList = nodeForLinkedList (2)

Barmar
  • 741,623
  • 53
  • 500
  • 612
Mahmut Salman
  • 111
  • 1
  • 10

1 Answers1

1

You need to change the declaration of nodeL to be a pointer to a pointer, so that you can update the caller's pointer variable.

void insertToLinkedList(nodeForLinkedList **nodeL,char *data){
    struct nodeForLinkedList* new_node = (struct nodeForLinkedList*)malloc(sizeof(struct nodeForLinkedList)); 
    
    strcpy(new_node->word, data);                                       // To copy the elements of data to new node's word
    
    new_node->next = *nodeL; // Error occurs here.**(1)**
    *nodeL = new_node;// and here**(2)**
}

And when you call the function, it should be

insertToLinkedList(&listVariable, stringVariable);

See Changing address contained by pointer using function for more information.

Barmar
  • 741,623
  • 53
  • 500
  • 612