【问题标题】:node pointer of a list wont update列表的节点指针不会更新
【发布时间】:2023-01-11 02:19:37
【问题描述】:

我有一个问题,我似乎找不到解决方案。我试图做一个简单的程序来创建一个列表并在 C 中打印它,但是当我尝试运行它时它循环打印列表的第一个值。这是如果有人可以帮助请编程:

我尝试添加括号等但没有用。

#include <stdio.h>
#include <stdlib.h>

typedef struct nodo{
    int dato;
    struct nodo *next;
}nodo_t;

typedef nodo_t *Ptr_nodo;

int main(){
    Ptr_nodo testa,temp;
    int q;
    temp=NULL;
    testa=NULL;
    temp=malloc(sizeof(nodo_t));
    if(temp){
        q=0;
        while(q!=-1){
            printf("Inserire valore: ");
            scanf("%d",&q);
            if(q!=-1){
                temp->dato=q;
                temp->next=testa;
                testa=temp;
            }
        }
        while(testa!=NULL){
            printf("%d",testa->dato);
            if(testa->next!=NULL)
                printf(" -> ");
            else
                printf(" -|");
            testa=testa->next;
        }
    }
    else
        printf("Errore allocazione memoria"),
    free(temp);
    return 0;
}

【问题讨论】:

  • 嘿!欢迎到这里。对于此类问题,我们希望提问者使用调试器单步调试他们的代码。无论如何,学习如何使用调试器将使您的编程生活变得更加轻松,甚至有一个密切的理由“这个问题没有调试尝试/调试器输出”。
  • 您的代码只调用一次malloc()

标签: c list pointers while-loop printing


【解决方案1】:

我尝试了您的代码并重复打印了指向自身的一个节点。似乎缺少的是当输入更多数据时,需要创建新节点并为每个节点分配内存。考虑到这一点,以下是您的代码的重构版本。

#include <stdio.h>
#include <stdlib.h>

typedef struct nodo{
    int dato;
    struct nodo *next;
}nodo_t;

typedef nodo_t *Ptr_nodo;

int main(){
    Ptr_nodo testa,temp;
    int q;
    temp=NULL;
    testa=NULL;
    temp=malloc(sizeof(nodo_t));
    testa = temp;
    if(temp){
        q=0;
        while(q!=-1){
            printf("Insert Value: ");
            scanf("%d",&q);
            if(q!=-1){
                temp->dato=q;
                temp->next=malloc(sizeof(nodo_t));  /* Created new node and allocate memory */
                temp=temp->next;
                temp->next = NULL;                  /* Make sure this latest node has the NULL terminator */
            }
        }
        while(testa!=NULL){
            printf("%d",testa->dato);
            if(testa->next!=NULL)
                printf(" -> ");
            else
                printf(" -|");
            testa=testa->next;
        }
        printf("
");                               /* Just added this for appearances on the terminal output */
    }
    else
        printf("Memory allocation error"),
    free(temp);
    return 0;
}

需要指出的一些点。

  • 当从用户收到另一个值时,需要创建一个新节点,并使用通过“malloc”函数分配的内存。
  • 创建新节点后,需要更新前一个节点和新节点的指针引用。
  • 在新节点中,需要将其“next”指针设置为NULL,以保证后续的列表打印不会陷入死循环。

添加这些位后,以下是终端的示例输出。

@Vera:~/C_Programs/Console/MultiNode/bin/Release$ ./MultiNode 
Insert Value: 4
Insert Value: 1998
Insert Value: 22
Insert Value: 45
Insert Value: 88
Insert Value: -1
4 -> 1998 -> 22 -> 45 -> 88 -> 0 -|

我看到的一个小副作用是最终节点的值为零。我不知道这是否是一个理想的结果,但我会把它留作可能需要改进的地方。

试一试,看看它是否符合您项目的精神。

【讨论】:

    【解决方案2】:

    画出来:

    temp = malloc(...);  // creates a node
    
             +------------+
    temp --> |  data: ??  |
             |  next      | --> ?? points nowhere right now
             +------------+
    
    testa --> NULL
    

    scanf你的数据,就叫它5

    scanf("%d",&q); // user inputs 5
    if(q!=-1){
       // set data
       temp->dato=q;
       temp->next=testa;
             +------------+
    temp --> |  data: 5   |
             |  next      | --> testa, which is NULL right now
             +------------+
    
       testa=temp;  // results in testa and temp pointing to the same node
    
              +------------+
    temp  --> |  data: 5   |
    testa --> |  next      | --> NULL
              +------------+
    }
    

    现在你绕回来

    scanf("%d",&q); // user inputs 8
    if(q!=-1){
       // whoops, didn't create a new node, so it's still the same temp from before
       temp->dato=q;
       temp->next=testa;
       testa=temp;
    
              +------------+
    temp  --> |  data: 8   |
    testa --> |  next      | --> testa, which is the same as temp -----+
              +------------+                                           |
                     ^                                                 |
                     |                                                 |
                     +-------------------------------------------------+
    

    您有一个指向自身的节点。您需要在每个用户条目上创建一个新节点,我相信其他答案会向您展示如何做到这一点。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-02-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-12
      • 1970-01-01
      • 2018-05-07
      • 2019-04-19
      相关资源
      最近更新 更多