将节点添加到指针数组

时间:2013-09-17 02:55:30

标签: c pointers nodes

我正在尝试添加三个节点,每个节点包含一个int值1和一个指向下一个节点的链接。我能够打印到前两个节点,但是当我尝试访问第三个节点时,我得到了分段错误。我不知道为什么,我追踪了代码但仍然丢失了。

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);
}

1 个答案:

答案 0 :(得分:1)

编程,是与计算机的关系。你必须想要让它发挥作用,否则你会放弃;你需要了解她的想法,否则你将无法沟通;最重要的是,你应该清楚不想要什么,否则你永远无法要求它,更不用说对整个事情的发展方向感到满意。

学习新算法时应该做的第一件事就是尝试在纸上进行处理。绘制表示数据结构的框:以数组排列的框和由malloc返回的自由浮动框。写出所有变量的值。然后一步一步地按照你的算法,用值填充框,为你的指针绘制箭头等。这看起来像是一个拖累,但另一种方式 远,更痛苦,当你得到非常奇怪的错误。

我会在您的代码中添加注释(这似乎与算法本身存在一些问题,而不仅仅是一个错误)在调试时继续编辑问题中的代码并发现更多错误。我会帮忙的。

我发现了两个错误:你的添加功能总是将列表缩减为两个元素。你的代码肯定会在某个时候出现段错误,因为你没有将最后一个指针设置为NULL,所以你不知道列表末尾的位置。

您的代码存在的最大问题是它尝试过多。你不知道如何实现一个链接列表,你已经直接实现了一个链接列表数组。最快的方式去painville。软件开发中用于一步一步的技术术语是“单元测试”。谷歌看看狂热的人们是如何重视它的。 :P

祝你好运!

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);
}