简体   繁体   中英

Adding nodes to an array of pointers

i'm trying to add three nodes each containing an int value of 1 and a link to the next node. I am able to print to the first two nodes but when I try to access the third node I get segmentation fault. I don't why though, I traced the code but still lost.

struct node{
    int x;
    struct node *link;
};


add(struct node *arrayy[],int value)
{
    struct node *nodey = (struct node *)malloc(sizeof(struct node));
    nodey->x=value;

    if(arrayy[value]==NULL)
    {
        printf("I am not pointing to something...now I am hehehe\n");
        arrayy[value]=nodey;
    }
    else
    {
        printf("I already have a head..now my link is pointing at something\n");
        while(arrayy[value])
        {
            if(arrayy[value]->link==NULL)
            {
                arrayy[value]->link=nodey;
                break;
            }
            arrayy[value]=arrayy[value]->link;
        }

    }
}

print(struct node *arrayy[])
{
    int x= 0;

    for(x; x<10; x++)
    {
        while(arrayy[x])
        {
            printf("%d",arrayy[x]->x);
            arrayy[x]=arrayy[x]->link;
        }
    }

}

main(void)
{
    struct node *array[10]={NULL};
    add(array,1);
    add(array,1);
    add(array,1);
    printf("%d",array[1]->link->link->x);
}

Programming, is a relationship with a computer. You have to want to make it work, or you'll give up; you need to understand how she thinks, or you won't be able to communicate; and most of all, you should be clear about what you wan't, or else you'll never be able to ask for it, let alone be satisfied with where the whole thing is going.

The first thing you should do when learning a new algorithm, is to try and work it out on paper. Draw boxes representing your data structures: those arranged in arrays and free floating ones returned by malloc. write out the values of all your variables. then follow your algorithm step by step, filling the boxes with values, drawing arrows for your pointers etc. This may seem like a drag, but doing it the other way is far, far more painful, when you get really weird bugs.

I'll add comments to your code (which seems to have several problems with the algorithm itself, not just one bug) Keep editing the code in your question as you debug it and find more bugs. I'll help out.

Two bugs that I found: your add function always reduces the list to two elements. your code is guaranteed to segfault at some time because you are not setting the last pointer to NULL so you wont know where the end of the list is.

The single biggest problem with your code is that it attempts too much. you don't know how to implement a link list and you have jumped straight into implementing an array of linklists. the fastest way to painville. The technical term in software development for taking it one step at a time is "unit test". google it to see how fanatic people are about it's importance. :P

good luck!

struct node{
    int x;
    struct node *link;
};


add(struct node *arrayy[],int value) //arrayy is a array of pointers.
{
    struct node *nodey = (struct node *)malloc(sizeof(struct node));
    nodey->x=value;// nodey will be at the end of the list, but nodey->link contains some garbage value. how will you know that this is the last element the next time you traverse the list.

    if(arrayy[value]==NULL) // do you actually want the "value'th" element of the array or something else?
    //is value the position or the contents?
    {
        printf("I am not pointing to something...now I am hehehe\n");
        arrayy[value]=nodey;
    }
    else
    {
        printf("I already have a head..now my link is pointing at something\n");
        while(arrayy[value])
        {
            if(arrayy[value]->link==NULL)
            {
                arrayy[value]->link=nodey;
                break;
            }
            arrayy[value]=arrayy[value]->link;//you are replacing arrayy[value] with the next element in the array. the malloced structs that was originally pointed to by arrayy[value] now has no pointer pointing to it, so there is no way to access those nodes. maybe you want a separate variable current node that you can modify without affecting the original link list : current_node=current_node->link ? Also, since you are "deleting" all the elements bu the last one before adding the new node, your list will never be more than 2 elements long
        }

    }
}

print(struct node *arrayy[])
{
    int x= 0;

    for(x; x<10; x++)
    {
        while(arrayy[x])
        {
            printf("%d",arrayy[x]->x);
            arrayy[x]=arrayy[x]->link; //again, you are modifying the array as you read it.
        }
    }

}

main(void)
{
    struct node *array[10]={NULL}; //you will have array of 10 pointers, each pointing to address 0. does this mean that you wnt 10 seperate lists?
    add(array,1);
    add(array,1);//debug by putting different values in different nodes
    add(array,1);
    printf("%d",array[1]->link->link->x);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM