C语言 处理链表数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4940043/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 07:44:44  来源:igfitidea点击:

dealing with array of linked list

carrayslinked-list

提问by Rami Jarrar

My approach:

我的做法:

An array of fixed-length (lets say 20) each element is pointer to the first node of a linked list. so i have 20 different linked list.

一个固定长度的数组(比如 20 个),每个元素都是指向链表第一个节点的指针。所以我有 20 个不同的链表。

This is the structure:

这是结构:

struct node{
       char data[16];
       struct node *next;
};

My declaration for that array

我对该数组的声明

struct node *nodesArr[20];

now to add a new node to one of the linked list, i do this:

现在要将新节点添加到链表之一,我这样做:

struct node *temp;

temp = nodesArr[i]; // i is declared and its less than 20
addNode(temp,word); // word is declared (char *word) and has a value ("hello")

The addNode function:

addNode 函数:

void addNode(struct node *q, char *d){
    if(q == NULL)
        q = malloc(sizeof(struct node));
    else{
        while(q->next != NULL)
            q = q->next;

        q->next = malloc(sizeof(struct node));
        q = q->next;
    }

    q->data = d; // this must done using strncpy
    q->next = NULL; 
}

and to print data from the array of linked list, i do this:

并从链表数组打印数据,我这样做:

void print(){
    int i;
    struct node *temp;

    for(i=0 ; i < 20; i++){
        temp = nodesArr[i];
        while(temp != NULL){
            printf("%s\n",temp->data);
            temp = temp->next;
        }
    }
}

now compiler gives no error, the program run and i pass the data to it, and when i call print it doesn't print any thing,,??

现在编译器没有错误,程序运行,我将数据传递给它,当我调用 print 时,它不打印任何东西,??

UPDATE::

更新::

after I edited the code (thx for you), i think the problem in the print function,, any idea ?

在我编辑了代码之后(谢谢你),我认为是打印功能的问题,有什么想法吗?

采纳答案by thkala

The problem lies in addNode(). When the list is empty you do:

问题在于addNode()。当列表为空时,您执行以下操作:

q = malloc(sizeof(struct node));

but the scope of qis limited to addNode(). You should have declared addNode()as

但范围q仅限于addNode(). 你应该声明addNode()

void addNode(struct node **q, char *d)

and adjust your code accordingly:

并相应地调整您的代码:

*q = malloc(sizeof(struct node));

and so on...

等等...

回答by Jacobo de Vera

When you pass struct node *qto addNodeyou are giving it an address for an element in your array. If you use mallocinside, then you are overwriting this variable q, which is local to the function and now points to something different, but you haven't changed your original array. Try using a pointer to pointer to node (struct node **q).

当你传递struct node *qaddNode你时,你是在给它一个数组中元素的地址。如果您使用mallocinside,那么您将覆盖这个变量q,它是函数的本地变量,现在指向不同的东西,但您没有更改原始数组。尝试使用指向节点 ( struct node **q) 的指针。

回答by Anon.

void addNode(struct node *q, char *d){
    if(q == NULL)
        q = malloc(sizeof(struct node));

Here's the problem.

这就是问题所在。

The new value of qdoesn't ever get out of the function, so your array of linked lists never gets updated.

的新值q永远不会离开函数,因此您的链表数组永远不会更新。

Normally the solution here is to use a double-pointer:

通常这里的解决方案是使用双指针:

void addNode(struct node **q, char *d){
    if(*q == NULL)
        *q = malloc(sizeof(struct node));

And call it like so:

并这样称呼它:

addNode(&nodesArr[i],word);

Then, if you malloca new node, the value in the array will be set to point to the new node.

然后,如果你malloc有一个新节点,数组中的值将被设置为指向新节点。

回答by Jay Mistry

struct node
{

  int actual, estimated;

  char c;

  struct node *next;

} *head[4], *var[4], *trav[4];


void
insert_at_end (char c, int value, int value1)
{

  struct node *temp;

  temp = head[i];

  var[i] = (struct node *) malloc (sizeof (struct node));

  var[i]->actual = value;

  //var1=(struct node *)malloc(sizeof(struct node));

  var[i]->estimated = value1;

  var[i]->c = c;

  //printf("%d",var->estimated);

  if (head[i] == NULL)

    {

      head[i] = var[i];

      head[i]->next = NULL;

    }

  else

    {

      while (temp->next != NULL)

    {

      temp = temp->next;

    }

      var[i]->next = NULL;

      temp->next = var[i];

    }

}