struct _point *next; ? help - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

struct _point *next; ? help

Hi There..


I am having a little problem here, Ill describe it

with a bit of code, maybe you will figure out what I do wrong

I mean I know what I do wrong, I just dont know how to do it

the 'right way'.. here I go


typedef struct _point {

int x;

int y;

int z;

struct _point *next;

} point;


ok, the problem is when I want to attach a point

to another point.. what I do is,..


void attach ( point *dest, point *src )

{

while (dest)

dest = dest->next;


dest = src;

}


it works well when dest == NULL;

but when dest is already pointing to a point

it will over write the existing point because the pointing

address of dest will be over written by the next point

anytime I do dest = dest->next;


I could always do


if (!dest->next)

dest->next = src;

else if (!dest->nest->nest)

dest->next->next = src;


but you know that is not good to do, I mean it I have 100

points to attach, can you imagine the amount of

->next->next->next->next I would have to do ?


Please if somebody can tell how

how I can go attach a point to a last ->next which is

NULL without over writting my preview point ?

You dont know how thankful I would be :)

thanks :)


Comments

  • : typedef struct _point {

    : int x;

    : int y;

    : int z;

    : struct _point *next;

    : } point;


    : ok, the problem is when I want to attach a point

    : to another point.. what I do is,..


    : void attach ( point *dest, point *src )

    : {

    : while (dest)

    : dest = dest->next;


    : dest = src;

    : }


    I may be misunderstanding since I'm not sure how

    this would work even when dest = NULL. I see it

    would assign dest to src, but that is a local

    copy of dest. When the function returns, dest

    should still be NULL. (unless your not passing in

    a pointer but an address of a node).


    If you want your attach() function to append

    to the end of the list, and dest is presumably a

    pointer to the head of the list, then the

    easiest way is to do something like the following :


    point * attach(point * dest, point * src)

    {

    if(!dest)// empty list is special case

    dest = src;

    else

    { // find last node in list

    while(dest->next)

    dest = dest->next;

    // append new node to end

    dest->next = src;

    }


    return(dest); // return the possibly new head

    }

    And invoke it something like:


    point * head = NULL;


    head = attach(head, src1);

    head = attach(head, src2);

    .....


    It isn't the most elegant, but it is the easiest.

    If I misunderstood, please clarify.


    Rock


  • Hi There.. thanks for the reply, I found a easy

    way to do it (maybe it is wrong), but it really

    seems to work ok without any problem so far


    I will copy my function here,..


    void vertex_attach_vertex ( Vertex *dest, Vertex *src )

    {

    Vertex *v;


    if ( (!dest) || (!src) )

    return;


    if (!dest->next)

    dest->next = src;

    else

    {

    v = dest;

    while (v->next)

    v = v->next;


    v->next = src;

    }


    }


    v may be a local vertex

    but v does point to the last vertex struct

    of dest


    I guess its the same thing as


    int i = 5;


    void dummy ( int *i )

    {

    i = 10;

    }


    dummy ( &i)


    i will == 10 ... right ?


  • I posted it, and it shows up below your last post,

    but it isn't shown on the main board. What the heck?


    http://208.128.17.74/msgboard/board8/messages/683.html


Sign In or Register to comment.