【问题标题】:error in building link list in c [closed]在c中构建链接列表时出错[关闭]
【发布时间】:2016-12-20 09:03:30
【问题描述】:

我在while循环中写了条件但是出错了

curr undeclared(第一次在这个函数中使用)

虽然我在插入函数中使用了变量 curr。

insert(struct node **start)
         { struct node *temp;
           temp=(struct node *)malloc(sizeof(struct node);
           temp-> data=75;
           temp->next= NULL;
           if(*start==NULL)
           {*start= temp;} 
           else
           {struct node *curr=*start;}
           while(curr->next!=NULL)
           {curr= curr->next;
           curr-> next= temp;
                                  }

                       }

【问题讨论】:

  • 你的格式很糟糕。出于对所有美好事物的热爱,请阅读一些优秀的 C 代码并正确且一致地格式化您的代码。然后,我们可能会向您解释 scope 是如何工作的。但只有当你的块显示为 blocks.
  • 请注意,点. 和箭头-> 操作符绑定得非常紧密,两边都不应该有空格。编译器不介意,但为了那些阅读您的代码的人的理智。 (我也完全同意所显示的格式非常糟糕的评论。避免使用类似于 Pico style 的任何内容——这是 C,而不是 Pico——并改用 Allman 或 1TBS 变体。)
  • 来吧,看看这里大部分的sn-ps代码是怎么格式化的……
  • 您必须在 else 子句之外声明它。

标签: c pointers linked-list singly-linked-list


【解决方案1】:

当你在 C 中声明一个变量时,它的作用域是最近的周围块。

例如:

if (something) {
    /* foo is declared here and so it is limited to between these
     * {} brackets */
    int foo = 3;
    printf("%i\n", foo);
}
/* ERROR! foo is not declared here because we are outside the {} */
printf("%i\n", foo);

我认为问题在于您将结束 } 括号放入得太早了,而您实际上希望 while 循环位于 else 语句的块内。

如果您整理代码的缩进以使结构更清晰,这将有所帮助。我认为还有其他各种缺失的括号,例如关闭对malloc 的调用的括号,这会阻止代码被编译。

编辑:我认为函数的正确版本应该是这样的(注意,除了编译之外我没有测试过):

void
insert (struct node **start)
{
        struct node *temp;
        temp = (struct node *) malloc (sizeof (struct node));
        temp->data = 75;
        temp->next = NULL;

        if (*start == NULL) {
                *start = temp;
        }
        else {
                struct node *curr = *start;
                while (curr->next != NULL) {
                        curr = curr->next;
                }
                curr->next = temp;
        }
}

【讨论】:

    【解决方案2】:

    在这个 else 语句中

    else
               {struct node *curr=*start;}
    

    变量curr 具有作为else 子语句的复合语句的范围。

    {struct node *curr=*start;}
    

    因此它在下面的 while 语句中是不可见且不活跃的

          while(curr->next!=NULL)
           {curr= curr->next;
           curr-> next= temp;
                                  }
    

    我认为您的意思是 while 语句将包含在 else 语句的复合语句中。

    无论如何,您的插入功能太复杂了。它可以写得更简单,如下面的演示程序所示。

    #include <stdio.h>
    #include <stdlib.h>
    
    struct node
    {
        int data;
        struct node *next;
    };
    
    int insert( struct node **start, int data )
    {
        struct node *new_node = malloc( sizeof( struct node ) );
        int success = new_node != NULL;
    
        if ( success )
        {
            new_node->data = data;
            new_node->next = NULL;
    
            while ( *start ) start = &( *start )->next;
            *start = new_node;
        }
    
        return success;
    }
    
    void display( struct node *start )
    {
        for ( ; start; start = start->next ) printf( "%d ", start->data );
    }
    
    int main( void ) 
    {
        struct node *start = NULL;
        const int N = 10;
    
        int i = 1;
        while ( i <= N && insert( &start, i ) ) ++i;
    
        display( start );
    
        return 0;
    }
    

    程序输出是

    1 2 3 4 5 6 7 8 9 10 
    

    当然你需要编写其他方法,例如自己删除所有分配给列表的内存的方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-01
      • 1970-01-01
      • 2013-04-01
      • 1970-01-01
      • 2013-08-27
      • 2020-07-22
      相关资源
      最近更新 更多