linked list (Insert)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • momotaro
    Contributor
    • Sep 2006
    • 357

    linked list (Insert)

    why i keep having problem in thhis line:

    [CODE=c]
    node *InsertVehicule (node *N, vehicule *V)
    {
    node *pre;
    if(!N)
    {
    N = (node*)malloc(s izeof(node));
    N->data_vehicul e = V;
    }
    else
    {
    pre = N;
    while(pre->NextVehicule )
    {
    if(pre->data_vehicul e->distance > V->distance)
    {
    V = pre->NextVehicule->data_vehicul e;
    pre->NextVehicule->data_vehicul e = V;
    break;
    }
    else
    pre = pre->NextVehicule ;

    if(pre->NextVehicule == NULL) // ****THIS ONE****
    pre->NextVehicule->data_vehicul e = V;
    }
    while(N->NextEdge)
    printf("%s %d", N->data_vehicul e->CarId, N->data_vehicul e->distance);
    }
    return N;
    }[/CODE]
    Last edited by Ganon11; Dec 1 '07, 04:50 PM. Reason: Indenting makes everyone's life easier, including yours.
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Originally posted by momotaro
    pre = pre->NextVehicule ;

    if(pre->NextVehicule == NULL) // ****THIS ONE****
    You will crash if pre is NULL.

    Check pre for NULL before trying to use it in pre->NextVehicule .

    Comment

    • Ganon11
      Recognized Expert Specialist
      • Oct 2006
      • 3651

      #3
      Additionally, if pre->nextVehicule is NULL (which your if...statement checks), then there will be no pre->nextVehicule->data_vehicul e, so you will crash here as well.

      Comment

      • momotaro
        Contributor
        • Sep 2006
        • 357

        #4
        but i need to reach that status in order to insert at the end!

        Comment

        • momotaro
          Contributor
          • Sep 2006
          • 357

          #5
          I realy don't know how to overcome that NULL pointer issue please help


          [CODE=c] node *InsertVehicule (node *N, vehicule *V)
          {

          node *walker;

          if(!N)

          {

          N = CreatNode();

          N->data_vehicul e = V;

          printf("%s", N->data_vehicul e->CarId);

          }

          else

          {

          walker = N;

          while(walker->NextVehicule )

          walker = walker->NextVehicule ;

          if(!walker->NextVehicule )// ***walker->NextVehicule = NULL***

          walker->NextVehicule->data_vehicul e = V;//***hence this line gonna cruch but I need to Insert in here...please help***

          }

          while(N->NextVehicule )

          printf("%s", N->data_vehicul e->CarId);

          return N;

          }

          [/CODE]

          Comment

          • momotaro
            Contributor
            • Sep 2006
            • 357

            #6
            why N is always = NULL

            [CODE=c] node *InsertVehicule (node *N, vehicule *V)
            {

            node *walker;

            if(!N)// ***N is always NULL!***

            {

            N = CreatNode();

            N->data_vehicul e = V;

            printf("%s", N->data_vehicul e->CarId);

            }

            else

            {

            walker = N;

            while(walker->NextVehicule )

            walker = walker->NextVehicule ;

            if(!walker->NextVehicule )

            walker->NextVehicule->data_vehicul e = V;

            }

            while(N->NextVehicule )

            printf("%s", N->data_vehicul e->CarId);

            return N;

            }

            [/CODE]

            Comment

            • Laharl
              Recognized Expert Contributor
              • Sep 2007
              • 849

              #7
              Create a Vehicle*, then set walker->nextVehicle to it, then change the data values you need. This way, you set the pointer to something, then you can access the pointer safely.

              Comment

              • weaknessforcats
                Recognized Expert Expert
                • Mar 2007
                • 9214

                #8
                This code:
                Originally posted by momotaro
                walker = walker->NextVehicule ;

                if(!walker->NextVehicule )// ***walker->NextVehicule = NULL***
                is what I referred to in my Post #2.

                walker can be NULL before you get to the if statement. If it is, you will crash. Check you pointers before using the indirection operator (->) on them:
                [code=c]
                walker = walker->NextVehicule ;
                if (!walker)
                {
                //bail out
                return;
                }
                if(!walker->NextVehicule )// ***walker->NextVehicule = NULL***
                [/code]

                Comment

                • momotaro
                  Contributor
                  • Sep 2006
                  • 357

                  #9
                  but I need that pointer to be null in order to inset a t the end....
                  am lost plz help

                  Comment

                  • Laharl
                    Recognized Expert Contributor
                    • Sep 2007
                    • 849

                    #10
                    If the pointer 'walker' points to NULL, then you get a segmentation fault (segfault) when you try to access walker->NextVehicle because NULL has to link to any Vehicle, or anything else for that matter. You can take a NULL pointer and set it to point to something else, but that is the only thing you can do with it (that I know of) other than check if it points to NULL. Anything else gives you segfaults.

                    As such, you have to check if walker is null before you can do anything with it, since if it is, it'll go kablooie.

                    Comment

                    • momotaro
                      Contributor
                      • Sep 2006
                      • 357

                      #11
                      why my linked list is not set!!!! plz help


                      [CODE=c] node *walker = N;

                      //printf("%s\n", V->CarId); //all vehicules are ok

                      if(!N)

                      {

                      N = CreatNode();

                      N->data_vehicul e = V;

                      }

                      else

                      {

                      while(walker->NextVehicule )

                      walker = walker->NextVehicule ;

                      if(!walker->NextVehicule )

                      {

                      walker->NextVehicule = CreatNode();

                      walker->NextVehicule->data_vehicul e = V;

                      printf("%s\n", N->data_vehicul e->CarId);//there is only the one of thebegining condition****

                      }

                      }

                      return N;

                      }

                      [/CODE]

                      Comment

                      • momotaro
                        Contributor
                        • Sep 2006
                        • 357

                        #12
                        sill waiting for a help.... :)

                        Comment

                        • weaknessforcats
                          Recognized Expert Expert
                          • Mar 2007
                          • 9214

                          #13
                          You still haven't made the changes I mentioned in my Post #8.

                          Comment

                          • momotaro
                            Contributor
                            • Sep 2006
                            • 357

                            #14
                            to tell you the truth i did not get the meaning of "baikl out"

                            Comment

                            • Laharl
                              Recognized Expert Contributor
                              • Sep 2007
                              • 849

                              #15
                              "bail out" means to jump out before the ship sinks. In this case, further use in the function will lead to segfaults, so you need to exit before those can happen by using return; which instantly ends a void function (or any other, but anything else needs a value returned).

                              Comment

                              Working...