处理链表数组

发布于 2024-10-17 02:07:05 字数 1266 浏览 1 评论 0原文

我的方法:

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

这是结构:

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

我现在对该数组的声明

struct node *nodesArr[20];

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

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")

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

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

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

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

更新::

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

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.

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:

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,,??

UPDATE::

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

酒儿 2024-10-24 02:07:05

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

q = malloc(sizeof(struct node));

但是 q 的范围仅限于 addNode()。您应该将 addNode() 声明为

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

并相应地调整您的代码:

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

依此类推...

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

q = malloc(sizeof(struct node));

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

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

and adjust your code accordingly:

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

and so on...

瞄了个咪的 2024-10-24 02:07:05

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

When you pass struct node *q to addNode you are giving it an address for an element in your array. If you use malloc inside, 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).

空名 2024-10-24 02:07:05
void addNode(struct node *q, char *d){
    if(q == NULL)
        q = malloc(sizeof(struct node));

问题就在这里。

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

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

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

并像这样调用它:

addNode(&nodesArr[i],word);

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

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

Here's the problem.

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

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 malloc a new node, the value in the array will be set to point to the new node.

拿命拼未来 2024-10-24 02:07:05
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];

    }

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

    }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文