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

  • Home
  • SEARCH
  • 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 7611753
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T01:44:30+00:00 2026-05-31T01:44:30+00:00

Hello My problem with this code is on my 2nd else loop; i never

  • 0

Hello My problem with this code is on my 2nd else loop; i never enter it and therefore i never make new nodes for my list. can anyone help me see what i am missing?

    bool List::Insert(int data)
{
    Node* P = new Node;
    if(P==NULL)
    {
        return false;
    }
    else
    {
        P ->info = data;
        P ->next = NULL;
            if(Head == NULL)
            {
                Head = P;
            }
            else
            {
                Node* lastNode;
                for(lastNode = Head; lastNode ->next != NULL; lastNode = lastNode ->next)
                {
                    lastNode ->next = P;
                }
            }
        return true;
    }
}
  • 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-31T01:44:31+00:00Added an answer on May 31, 2026 at 1:44 am

    This:

    Node* lastNode;
    for(lastNode = Head; lastNode ->next != NULL; lastNode = lastNode ->next)
    {
        lastNode ->next = P;
    }
    

    is dead wrong. It will change the next pointer, for every single node currently in the list, to point to your new node. You need to only change the pointer in the last node:

    Node* lastNode = Head;
    while (lastNode->next != NULL)
        lastNode = lastNode->next;
    lastNode->next = P;
    

    You may also, for efficiency, want to maintain a separate Tail pointer (in addition to your Head) so that you can simply replace that whole operation with:

    Tail->next = P;
    Tail = P;
    

    That way, you won’t have to traverse the entire list every time you want to append a new node. Your code then becomes something like (without the traversal, and with updating the tail pointer as well):

    // Prepare new node.
    
    Node *P = new Node;
    P->info = data;
    P->next = NULL;
    
    // If list empty, set head and tail to new node, otherwise
    //   append it.
    
    if (Head == NULL) {
        Head = P;
        Tail = P;
    } else {
        Tail->next = P;
        Tail = P;
    }
    

    I’ll stop short of criticising the fact that your Insert method doesn’t actually insert but rather appends. My near-anal-retentive nitpicking nature is unlikely to endear me to you:-)

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

Sidebar

Related Questions

Hello I hope someone can explain this problem. This is the code: class Memory{
Problem Hello all! I have this code which takes my jpg image loops through
Can Some one help me the problem with this code? I am getting bunch
Can some one please tell me the problem with this code. I am reading
Hello i have a problem with this line of code: Last Visted On :
Hello i`ve got a problem: when i run this code i get this: on
Hello I've runned in to a problem again that I can't solve on my
So I currently have this code: (ns contact-form.core (:gen-class)) (def foo Hello World!) (defn
got a small problem, this code <form action=<?php echo $_SERVER['PHP_SELF']; ?> method=post> <?php ...
Is there any problem with this code? bool Spellcheck::smart_comp(string value, string key){ return true;

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.