【问题标题】:loop for insertion in linked list not working插入链表的循环不起作用
【发布时间】:2020-08-21 06:25:17
【问题描述】:

我正在尝试在链表的末尾插入一些节点,但这段代码有问题。

在这里,我正在尝试创建一个循环,并且在此循环的帮助下,我希望用户输入列表的所有数字,但我想我遗漏了一些东西。

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

struct node{
int data;
struct node* next;

           };
void insert(struct node** ref)
 {   int n,i=0 ;

 printf("how many numers:\n");
scanf("%d",&n);
fflush(stdin);
for(i=0;i<n;i++)
{
    struct node* temp = (struct node*)malloc(sizeof(struct node));
    struct node* last = *ref;
    printf("enter the number:\n");
    scanf("%d",&(temp->data));
    fflush(stdin);
    temp->next = NULL;
    if(*ref==NULL)
       {
        *ref = temp;
        return;
       }
    while(last->next != NULL)
        last = last->next;

    last->next = temp;

}
  return;
  }

int main()
 {
   struct node* head=NULL;
   insert(&head);
   return 0;
}

【问题讨论】:

    标签: c linked-list singly-linked-list insertion function-definition


    【解决方案1】:

    当为一个空列表调用该函数时,它只在列表中插入一个元素并退出。

    if(*ref==NULL)
       {
        *ref = temp;
        return;
       }
    

    还要注意为stdin 调用fflush 具有未定义的行为。

    fflush(stdin);
    

    函数可以通过以下方式定义

    size_t insert( struct node **ref )
    {
        printf( "how many numbers: " );
    
        size_t n = 0;
        scanf( "%zu", &n );
    
        if ( n != 0 )
        {
            while ( *ref != NULL )
            {
                ref = &( *ref )->next;
            }
        }
    
        size_t i = 0;
    
        for ( ; i < n && ( *ref = malloc( sizeof( struct node ) ) ) != NULL; i++ )
        {
            ( *ref )->data = 0;
            ( *ref )->next = NULL;        
    
            printf( "enter the number: " );
            scanf( "%d", &( *ref )->data );
    
            ref = &( *ref )->next;
        }
    
        return i;
    }
    

    这是一个演示程序

    #include <stdio.h>
    #include <stdlib.h>
    
    struct node
    {
        int data;
        struct node* next;
    };
    
    size_t insert( struct node **ref )
    {
        printf( "how many numbers: " );
    
        size_t n = 0;
        scanf( "%zu", &n );
    
        if ( n != 0 )
        {
            while ( *ref != NULL )
            {
                ref = &( *ref )->next;
            }
        }
    
        size_t i = 0;
    
        for ( ; i < n && ( *ref = malloc( sizeof( struct node ) ) ) != NULL; i++ )
        {
            ( *ref )->data = 0;
            ( *ref )->next = NULL;        
    
            printf( "enter the number: " );
            scanf( "%d", &( *ref )->data );
    
            ref = &( *ref )->next;
        }
    
        return i;
    }
    
    void display( struct node *head )
    {
        for ( ; head != NULL; head= head->next )
        {
            printf( "%d -> ", head->data );
        }
    
        puts( "null" );
    }
    
    int main(void) 
    {
        struct node *head = NULL;
    
        size_t n = insert( &head );
    
        printf( "There are %zu nodes in the list. They are\n", n );
    
        display( head );
    
        return 0;
    }
    

    它的输出可能看起来像

    how many numbers: 5
    enter the number: 1
    enter the number: 2
    enter the number: 3
    enter the number: 4
    enter the number: 5
    There are 5 nodes in the list. They are
    1 -> 2 -> 3 -> 4 -> 5 -> null
    

    【讨论】:

    • 对不起,我还是不明白,请您详细说明。
    • 就像,为什么即使我删除了 return 语句,循环也没有运行两次
    • @Ayu91 最初指针 last 等于 NULL,因为 *ref 也等于 NULL。因此,如果您将删除 return 语句,则由于使用此表达式 ;ast->next 相当于 NULL->next,程序将具有未定义的行为。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-23
    • 2023-03-24
    • 2021-01-18
    • 1970-01-01
    • 2021-10-09
    • 2014-10-25
    相关资源
    最近更新 更多