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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T18:29:00+00:00 2026-05-14T18:29:00+00:00

The problem appears with the insert function that I wrote. 3 conditions must work,

  • 0

The problem appears with the insert function that I wrote.

3 conditions must work, I tested b/w 1 and 2, b/w 2 and 3 and as last element, they worked.


EDIT;
It was my own problem. I did not realize I put MAXINPUT = 3 (instead of 4). I do appreciate all the efforts to help me becoming a better programmer, using more advance and more concise features of C++.

Basically, the problem has been solved.


Efficiency is not my concern here (not yet). Please guide me through this debug process.

Thank you very much.

#include<iostream>
#include<string>
   using namespace std;

    struct List    // we create a structure called List
   {
      string name;  
      string tele;
      List *nextAddr;
   };

   void populate(List *);
   void display(List *);
   void insert(List *);

    int main()
   {
      const int MAXINPUT = 3;
      char ans;  

      List * data, * current, * point;  // create two pointers
      data = new List;
      current = data;

      for (int i = 0; i < (MAXINPUT - 1); i++)
      {
         populate(current);
         current->nextAddr = new List;
         current = current->nextAddr;
      }

   // last record we want to do it sepeartely

      populate(current);
      current->nextAddr = NULL;
      cout << "The current list consists of the following data records: " << endl;
      display(data);

   // now ask whether user wants to insert new record or not

      cout << "Do you want to add a new record (Y/N)?";

      cin  >> ans;

      if (ans == 'Y' || ans == 'y')
      {
     /* 

   To insert b/w first and second, use point as parameter
   between second and third uses point->nextAddr
   between third and fourth uses point->nextAddr->nextAddr
   and insert as last element, uses current instead 

   */       
   point = data;
   insert(());  
         display(data);
      }

      return 0;  
   }


    void populate(List *data)
   {
      cout << "Enter a name: ";
      cin >> data->name;
      cout << "Enter a phone number: ";
      cin >> data->tele;

      return;
   }

    void display(List *content)
   {
      while (content != NULL)
      {
         cout << content->name << "     " << content->tele;
         content = content->nextAddr;
         cout << endl; // we skip to next line
      }

      return;
   }

    void insert(List *last)
   {
      List * temp = last->nextAddr; //save the next address to temp
  last->nextAddr = new List; // now modify the address pointed to new allocation
      last = last->nextAddr; 
      populate(last);
      last->nextAddr = temp; // now link all three together, eg 1-NEW-2

      return;
   }
  • 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-14T18:29:01+00:00Added an answer on May 14, 2026 at 6:29 pm

    Your code works fine on my machine (once the insert(()) statement is “filled in” properly as explained in the code comment). The insertion works in all positions.


    Something else, though: I initially had a look at your insert function. I thought I’d give you a hint on how to make it a little shorter and easier to understand what’s going on:

    void insert(List *last)
    {
        // create a new item and populate it:
        List* new_item = new List;
        populate(new_item);
    
        // insert it between 'last' and the item succeeding 'last':
        new_item->nextAddr = last->nextAddr;
        last->nextAddr = new_item;
    }
    

    This would be preferable because it first creates a new, separate item, prepare it for insertion, and only then, when this has worked successfully, will the function “mess” with the linked list. That is, the linked list is not affected except in the very last statement, making your function “safer”. Contrast this with your version of insert, where you mix code for constructing the new item with the actual insertion. If something goes wrong inside this function, chances are far higher that the linked list is messed up, too.

    (What’s still missing btw. is a initial check whether the passed argument last is actually valid, ie. not a null pointer.)


    P.S.: Of course you could just use a standard C++ std::list container instead of building your own linked list, but seeing that you tagged your question beginner, I assume you want to learn how it actually works.

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

Sidebar

Ask A Question

Stats

  • Questions 390k
  • Answers 390k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer that would be great. I have asked for it a… May 15, 2026 at 1:06 am
  • Editorial Team
    Editorial Team added an answer Consider learning a PHP framework and its philosophy and architecture… May 15, 2026 at 1:06 am
  • Editorial Team
    Editorial Team added an answer Very simple example to get you started, without locking or… May 15, 2026 at 1:06 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.