Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9146805
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T10:50:03+00:00 2026-06-17T10:50:03+00:00

I want to add elements to a Linked List while Preorder Traversaling on a

  • 0

I want to add elements to a Linked List while Preorder Traversaling on a Binary Tree. I do not want to destroy the BT, just make a copy of the elements in a linked list. This is my code snippet.

void Preorder(treeNode *node, Nodelist * head){
    if(node==NULL){
        return;
    }
    //printf("%d\n", node->data);
    head = List_insert(head, node->data);
    Preorder(node->left, head);
    Preorder(node->right, head);
}

Nodelist * List_insert(Nodelist * head, int v)
{
    Nodelist * p = Node_construct(v);
    p->depth = 2222;
    p -> next = head;
    return p;
}

void List_print(Nodelist * head)
{
    while (head != NULL)
    {
        printf("%d ", head -> value);
        printf("%d ", head -> depth);
        printf("\n");
        head = head -> next;
    }
    printf("\n\n");
}

treeNode * Insert(treeNode *node,int data)
{
        if(node==NULL)
        {
                treeNode *temp;
                temp = (treeNode *)malloc(sizeof(treeNode));
                temp -> data = data;
                temp -> left = temp -> right = NULL;
                return temp;
        }

        if(data >(node->data))
        {
                node->right = Insert(node->right,data);
        }
        else if(data < (node->data))
        {
                node->left = Insert(node->left,data);
        }
        return node;

}

int main(int argc, char**argv) {
        treeNode *root = NULL;
        root = Insert(root, 14);
        root = Insert(root, 15);
        root = Insert(root, 4);
        root = Insert(root, 9);
        root = Insert(root, 7);
        root = Insert(root, 18);
        root = Insert(root, 3);
        root = Insert(root, 5);
        root = Insert(root, 16);
        root = Insert(root, 20);
        root = Insert(root, 17); 

        Nodelist * head = NULL;
        Preorder(root, head);
        List_print(head);

    return 0;
}

The above code prints nothing. I think the problem is with the use of head = List_insert(head, node->data); in the preorder function. Any help with be appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T10:50:04+00:00Added an answer on June 17, 2026 at 10:50 am

    You are passing NULL to the Preorder as the list head. This is passed by value and you cannot alter the head in the main function this way. Instead define Preorder like this:

    void Preorder(treeNode *node, Nodelist **head)
    

    So that you can do:

    *head = Linst_insert....
    

    inside the function to modify the list. Of course, you need to call preorder from the main function like this:

    Preorder(root, &head);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to add elements to the my linked list order by ascending but
This is my array and I want to add some elements to make it
I want to add elements in JList from an user-defined function which is not
I want to add elements to arrays within an object that I access like
HI.. i want to add elements dynamically to listview in QT for symbian OS,
I want to add database elements into array. how do I do that. the
I want to add a class to multiple span elements with common classname's based
I want to add a class to certain input or label elements in a
I want to maintain order of the elements being added in a list. So,
like array(a, b, c, d, e); I want to add new elements to it,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.