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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T10:42:11+00:00 2026-06-15T10:42:11+00:00

I am trying to implement one bool function that receives List and Int as

  • 0

I am trying to implement one bool function that receives List and Int as arguments and should insert int and return true if the int does not exist in the list, or false if it is already there, i have been working for several hours with this function, and the if-else statements can insert sorted int, the problem (and crash) is how to check if the value already exist and return false, here is my function:
declaration of struct

typedef struct E_Type * List;
struct E_Type
 {
  int data;
  List next = 0;
 };

and function

bool insert(List & l, int data)
{   

   List current = l;
       do{//check if the int is already in the list
          current->data;
          current = current->next;
        //return false;
     }while (current->data == data);

      if (l == 0 || l->data > data){
              List new_list = new E_Type;
              new_list->data = data;
               new_list->next = l;
               l = new_list;
           return true;
      }

       else if(l->data < data){
             insert(l->next, data);
           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-06-15T10:42:12+00:00Added an answer on June 15, 2026 at 10:42 am
    do{
          //this line doesn't really do anything...
          current->data;
          //moving current forward, good.
          current = current->next;
     //If current->data is less than data in the list, it will exit the loop here anyway.
    }while (current->data == data);
    

    You also aren’t checking whether you have reached the end of the list. Perhaps what you are attempting to do is something like this:

    //This is correct for for iterative approach, but I don't think this is really what you need, either...
    while(current != null) {
        if (current->data == data)
            return false;
        current = current->next;
    }
    

    However, you probably don’t want to use iteration like this to make this check in a recursive function, so instead, simply replace that whole bit with:

    if (current->data == data)
       return false;
    

    And to return the correct value through the recursive calls, you’ll want to change:

    else if(l->data < data){
         insert(l->next, data);         //Recursive call
         return true;  //you don't want to just return true, return what the recursive call returns!
    }
    

    To:

    else if(l->data < data){
         return insert(l->next, data);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to implement Mutex lock on one of my static function of
I'm trying to implement invitations with Game Center and there's one thing that i
I am trying to implement a table that is sortable on more than one
I am trying to implement a function that will check to see if a
I'm trying to implement an animation that happens when a user taps one of
I'm trying to implement one of active appearance models (AMM) and on one of
I am trying to implement a one-to-many relationship using NHibernate 2.1.2 but keep getting
I am trying to implement OpenID as one preferred option to my next web
I am trying to implement google map api into one of my web page
I'm trying to implement a segue from one a table view controller to another

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.