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 473247
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T00:10:03+00:00 2026-05-13T00:10:03+00:00

I’m trying to make linked list similar too the one here: linked list in

  • 0

I’m trying to make linked list similar too the one here:

linked list in C

That is to have the “head”, I called it first, inside another struct. However I found doing that change. Makes it hard to add values to the list_item struct. I have tried some few things to see if it works. It compiles, however when I run the code it will crash. Any help would be helpful here. I know the cause of the crash is when I want to point the new_node to the linked_list.

#include <iostream>

using namespace std;

struct list_item
{
    int key;
    int value;
    list_item *next;
};

struct list
{
    struct list_item *first;
};

int main()
{
    list *head;
    list *new_node;

    head = NULL;
    head->first = NULL;

    for(int i = 0; i < 10; i++)
    {
        //allocate memory for new_node
        new_node = (list*)malloc(sizeof(list));
        new_node->first = (list_item*)malloc(sizeof(list_item));
        //adding the values
        new_node->first->key = i;
        new_node->first->value = 10 + i;

        //point new_node to first;
        new_node->first->next = head->first;

        //point first to new_node;
        head->first = new_node->first;

    }

    //print
     list *travel;
     travel->first = head->first;

     int i = 0;
     while(travel != NULL)
     {
         cout << travel->first->value << endl;
         travel->first = travel->first->next;
     }

    return 0;
}
  • 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-05-13T00:10:04+00:00Added an answer on May 13, 2026 at 12:10 am

    You are creating 10 lists, I think you might try to do something like this:

    #include <iostream>
    
    using namespace std;
    
    struct list_item
    {
        int key;
        int value;
        list_item *next;
    };
    
    struct list
    {
        struct list_item *first;
    };
    
    int main()
    {
        //Just one head is needed, you can also create this
        // on the stack just write:
        //list head;
        //head.first = NULL;
        list *head = (list*)malloc(sizeof(list));
        list_item *new_node = NULL;
    
        head->first = NULL;
    
        for(int i = 0; i < 10; i++)
        {
            //allocate memory for new_node
            new_node = (list_item*)malloc(sizeof(list_item));
            //adding the values
            new_node->key = i;
            new_node->value = 10 + i;
    
            //if the list is empty, the element you are inserting
            //doesn't have a next element
    
            new_node->next = head->first;
    
            //point first to new_node. This will result in a LIFO
            //(Last in First out) behaviour. You can see that when you 
            //compile
            head->first = new_node;
    
        }
    
         //print the list 
         list_item *travel;
         travel = head->first;
    
         while(travel != NULL)
         {
             cout << travel->value << endl;
             travel = travel->next;
         }
    
        //here it doesn't matter, but in general you should also make
        //sure to free the elements
        return 0;
    }
    

    This is what is going on. At first you only have one head and no elements.

    head
      |
      |
      V
     NULL
    

    Then you add your first element. Make sure that the “new_node->next==NULL”:

    head
      |
      |
      V
    node:   ------------------> NULL
    key = 0
    value = 10
    

    Then you add another node in front but append your first node to its next node. you move the pointer from the head to the new node

    head:
    first
      |
      |
      V
    node:   ---------> node:  -------------> NULL
    key: 1             key: 0   
    value: 11          value: 10  
    

    etc.

    Since you are using c++, you might consider using “new” and “delete”. Just replace

    new_node = (list_item*)malloc(sizeof(list_item));
    

    with

    list *head = new list
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm trying to create an if statement in PHP that prevents a single post
I am trying to loop through a bunch of documents I have to put
I'm making a simple page using Google Maps API 3. My first. One marker
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has

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.