【问题标题】:insertion in a singly linked list at a specific position在特定位置插入单链表
【发布时间】:2021-04-23 03:10:06
【问题描述】:

我刚刚浏览了单链表..我从参考书中复制了这段代码..但我不知道如何在 main 中运行它..我应该在 struct node * 中作为参数传递什么新的?我不知道..如果可能的话请帮忙....

struct node *insertatspecificposition(  struct node *new,int n){
    struct node *pred = head;
    if(n<=1){
        new->next = head;
        return new;
    }
    while(--n && pred != NULL ){
        pred = pred->next;
    }
    if(pred == NULL){
        return NULL;
    }
    new->next = pred->next;
    pred->next = new;
    return head;
}

【问题讨论】:

  • 强烈建议:1) 复制/粘贴一个完整示例以获得单链表。 2)通过调试器下的代码来了解它是如何工作的。 不要跳过这个。在调试器中遍历代码可以说是最强大的理解技术。 3) 一旦你理解了一般的“单链表”,那么(并且ONLY然后)将你的注意力转回“insertatspecificposition()”。注意:如果没有MCVE,我们将无法为您提供帮助
  • @Lakshit Chawla 我建议将这本书换成另一本书,因为这本书很糟糕。:)
  • 您是否尝试在此特定代码 sn-p 之前或之后阅读参考书的其余部分?我很难相信没有其他代码可以从这个非常不完整的部分制作一个完整的示例。具体来说,struct node 和全局变量head 的定义,至少。找到显示这些内容的部分很可能会引导您找到如何使用它们的示例。
  • 好吧,谢谢 vlad 的代码 sn-p.. 你的其他演示代码现在对我来说有点复杂.. 但是当我有点习惯数据结构时,我一定会尝试它..现在,我只是一个初学者..

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


【解决方案1】:

呈现的函数存在逻辑错误。

当位置设置为等于1 时,由于此代码sn-p,函数会在当前head 节点之前插入一个新节点

if(n<=1){
    new->next = head;
    return new;
}

因此,例如,如果您有一个空列表(head 等于 NULL),则调用类似的函数

head = insertatspecificposition( a_new_node_with_value_1, 1 );

你会得到一个看起来像的列表

1 -> null

现在逻辑上是一致的,如果在下一次调用函数时指定位置2,您将获得如下所示的列表

1 -> 2 -> null

不过这样的电话

head = insertatspecificposition( one_more_new_node_with_value_2, 2 );

返回NULL,而不是返回指针head的当前值。

这是一个演示程序。

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

struct node
{
    int data;
    struct node *next;
} *head;

struct node * create( int data )
{
    struct node *new = malloc( sizeof( struct node ) );
    
    if ( new != NULL )
    {
        new->data = data;
        new->next = NULL;
    }
    
    return new;
}

struct node *insertatspecificposition(  struct node *new,int n){
    struct node *pred = head;
    if(n<=1){
        new->next = head;
        return new;
    }
    while(--n && pred != NULL ){
        pred = pred->next;
    }
    if(pred == NULL){
        return NULL;
    }
    new->next = pred->next;
    pred->next = new;
    return head;
}

void display( void )
{
    for ( struct node *current = head; current != NULL; current = current->next )
    {
        printf( "%d -> ", current->data );
    }
    
    puts( "null" );
}

int main(void) 
{
    display();
    
    head = insertatspecificposition( create( 1 ), 1 );
    
    display();
    
    head = insertatspecificposition( create( 2 ), 2 );
    
    display();
    
    return 0;
}

程序输出是

null
1 -> null
null

而不是预期的输出

null
1 -> null
1 -> 2 -> null

还要注意该函数应该检查传递的指针new是否不等于NULL

当函数依赖于全局变量时,这不是一个好的设计。

就我而言,我会按照下面的演示程序所示的方式编写函数。

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

struct node
{
    int data;
    struct node *next;
};

struct node * create( int data )
{
    struct node *new = malloc( sizeof( struct node ) );
    
    if ( new != NULL )
    {
        new->data = data;
        new->next = NULL;
    }
    
    return new;
}

int insertatspecificposition(  struct node **head, int data, size_t n )
{
    struct node *new_node = create( data );
    int success = new_node != NULL;
    
    if ( success )
    {
        while ( n-- && *head != NULL )
        {
            head = &( *head )->next;
        }
        
        new_node->next = *head;
        
        *head = new_node;
    }
    
    return success;
}

FILE * display( const struct node *head, FILE *fp )
{
    for ( ; head != NULL; head = head->next )
    {
        fprintf( fp, "%d -> ", head->data );
    }
    
    fputs( "null", fp );
    
    return fp;
}

int main(void) 
{
    struct node *head = NULL;
    
    putc( '\n', display( head, stdout ) );
    
    insertatspecificposition( &head, 1, 0 );
    
    putc( '\n', display( head, stdout ) );
    
    insertatspecificposition( &head, 3, 1 );
    
    putc( '\n', display( head, stdout ) );
    
    insertatspecificposition( &head, 2, 1 );
    
    putc( '\n', display( head, stdout ) );
    
    insertatspecificposition( &head, 4, 3 );
    
    putc( '\n', display( head, stdout ) );
    
    return 0;
}

程序输出是

null
1 -> null
1 -> 3 -> null
1 -> 2 -> 3 -> null
1 -> 2 -> 3 -> 4 -> null

如果指定位置大于或等于列表中的节点数,则将新节点附加到列表的尾部。

在 C 中作为数组索引的位置从 0 开始。

当然,您至少还需要一个函数来清除列表,该列表将释放所有已分配的内存。

【讨论】:

  • @4386427 没什么难的,这本书是由低素质的程序员写的,仅此而已。至少指定位置的参数应该是无符号整数类型。我怀疑这本书是在印度出版的。
猜你喜欢
  • 1970-01-01
  • 2022-10-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-08
  • 1970-01-01
  • 2021-05-01
相关资源
最近更新 更多