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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T15:36:26+00:00 2026-05-24T15:36:26+00:00

After breaking my head over this for quite sometime, I am looking for help.

  • 0

After breaking my head over this for quite sometime, I am looking for help. Maybe I am doing something really silly here. Below is a basic implementation of linkedlist. However, it does not seem to work. Can someone please take a look?

EDIT: By not working I mean its getting in to an endless loop in the else part of the add function.

#include <iostream>

struct node
{
    int data;
    node* link;
};

void add(struct node** list, int i)
{
    //populate a node
    node* tempNode = (node*) malloc(sizeof(node));
    tempNode->data = i; //**This does not seem to initialize data**
    tempNode->link = NULL; //**Same here**

    //check if the list is empty
    if(*list == NULL)
    {
        //create the node
        *list = tempNode;
    }
    else
    {
        while((*list)->link != NULL); //Enters in to an endless loop here because the link is not initialized
        (*list)->link = tempNode;
    }

}

void print(node** list)
{
    node* itr = *list;
    while(itr != NULL)
    {
        std::cout << itr->data << "\n";
        itr = itr->link;
    }
}

int main()
{
    node* linkedList = NULL;
    node** t = &linkedList;
    add(&linkedList, 10);
    add(&linkedList, 20);
    add(&linkedList, 30);
    add(&linkedList, 40);
    print(&linkedList);

}
  • 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-24T15:36:27+00:00Added an answer on May 24, 2026 at 3:36 pm

    Your add function is wrong (the else branch). Since (*list)->link doesn’t get changed, it will loop forever. Try this.

    void add(struct node** list, int i)
    {
        /* .... */
        if(*list == NULL)
            *list = tempNode;
        else {
            struct node *p = *list;
            /* Search for tail. */
            while(p->link)
                p = p->link;
    
            /* Since p->link is NULL, it's the tail. */
            p->link = tempNode;
        }
    }
    

    This inserts in O(n); user786653 has a nice suggestion in the comments.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm a real noob to encryption. After breaking my head over this for a
I'm breaking my head over this one. I'm kind of new in android development.
After I was convinced that labeled breaks/continues are a total nono over here ,
Here is my code snippt. But the code is breaking after inner for loop.
After reading the Head First Design Patterns book and using a number of other
After reading this question , I was reminded of when I was taught Java
Does anybody know how to turn line breaking in eclipse after you press CTRL
please why is the loop statement breaking, after the multiplication it only update the
After asking this question about implementing an aspect with PostSharp, it came to my
Our company has recently switched over to using After the Deadline for our spell

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.