C 向指针数组添加节点

C 向指针数组添加节点,c,pointers,nodes,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))

我试图添加三个节点,每个节点都包含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);
}
struct节点{
int x;
结构节点*链接;
};
添加(结构节点*arrayy[],int值)
{
结构节点*节点=(结构节点*)malloc(sizeof(结构节点));
nodey->x=值;
if(arrayy[value]==NULL)
{
printf(“我不是指什么……现在我是呵呵\n”);
arrayy[value]=nodey;
}
其他的
{
printf(“我已经有一个头了..现在我的链接指向某个东西\n”);
while(arrayy[value])
{
if(arrayy[value]->link==NULL)
{
arrayy[value]->link=nodey;
打破
}
arrayy[value]=arrayy[value]->link;
}
}
}
打印(结构节点*arrayy[])
{
int x=0;
对于(x;xx);
arrayy[x]=arrayy[x]->链路;
}
}
}
主(空)
{
结构节点*数组[10]={NULL};
添加(数组,1);
添加(数组,1);
添加(数组,1);
printf(“%d”,数组[1]->link->link->x);
}

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

当你学习一个新的算法时,你应该做的第一件事就是在纸上试着把它写出来。绘制代表您的数据结构的框:按数组排列的框和malloc返回的自由浮动框。写出所有变量的值。然后一步一步地遵循你的算法,用值填充框,为指针画箭头等等。这可能看起来像是一种拖拉,但当你遇到非常奇怪的bug时,用另一种方法做会非常非常痛苦

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

我发现了两个错误:add函数总是将列表减少为两个元素。您的代码一定会在某个时候出现segfault,因为您没有将最后一个指针设置为NULL,所以您不知道列表的末尾在哪里

你的代码最大的一个问题是它尝试了太多。你不知道如何实现一个链接列表,你已经直接开始实现一个链接列表数组。去潘维尔最快的路。在软件开发中,一步一个脚印的技术术语是“单元测试”。谷歌看看人们对它的重要性有多狂热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);
}
struct节点{
int x;
结构节点*链接;
};
add(struct node*arrayy[],int-value)//arrayy是指针数组。
{
结构节点*节点=(结构节点*)malloc(sizeof(结构节点));
nodey->x=value;//nodey将位于列表的末尾,但是nodey->link包含一些垃圾值。下次遍历列表时,您如何知道这是最后一个元素。
if(arrayy[value]==NULL)//您实际需要数组的“value'th”元素还是其他元素?
//价值是位置还是内容?
{
printf(“我不是指什么……现在我是呵呵\n”);
arrayy[value]=nodey;
}
其他的
{
printf(“我已经有一个头了..现在我的链接指向某个东西\n”);
while(arrayy[value])
{
if(arrayy[value]->link==NULL)
{
arrayy[value]->link=nodey;
打破
}
arrayy[value]=arrayy[value]->link;//您正在用数组中的下一个元素替换arrayy[value]。最初由arrayy[value]指向的malloced结构现在没有指向它的指针,因此无法访问这些节点。可能您需要一个单独的变量current node,可以在不影响原始链接列表的情况下进行修改:current\u node=current\u node->link?另外,因为您正在“删除”在添加新节点之前,所有元素都是最后一个元素,您的列表长度永远不会超过2个元素
}
}
}
打印(结构节点*arrayy[])
{
int x=0;
对于(x;xx);
arrayy[x]=arrayy[x]->link;//同样,您在读取数组时正在修改它。
}
}
}
主(空)
{
struct node*array[10]={NULL};//您将拥有一个包含10个指针的数组,每个指针指向地址0。这是否意味着您拥有10个单独的列表?
添加(数组,1);
add(array,1);//通过在不同节点中放置不同的值进行调试
添加(数组,1);
printf(“%d”,数组[1]->link->link->x);
}

对于不到60行的代码(包括空格),您将听到的最有用的东西是:学会调试。你将花费一半的职业生涯来做这件事,你最好现在就开始。您不能指望其他人为您调试它。