【问题标题】:How can I insert a node in a singly linked list using recursion given: 1. a pointer to the head node 2. the index to insert the new node如何使用给定的递归在单链表中插入节点: 1. 指向头节点的指针 2. 插入新节点的索引
【发布时间】:2021-02-25 18:16:43
【问题描述】:

下面的函数是我正在尝试处理的函数。我遇到的问题是我不知道如何将指向原始头的指针“保留”到列表中,因为这是插入后我必须返回的。

没有驱动代码,所以一切都必须在这个函数中完成。

因为我必须递归地执行此操作,所以我不能只创建一个临时节点来指向原始头。我刚刚习惯了递归,我找不到解决方案。

注意:我的函数还有一些其他问题,因为我认为在链表的开头和结尾插入新节点时效果不佳,但我相信我可以解决这些边缘情况。

我要学习的主要内容是如何“存储”我列表的原始头部。

感谢所有帮助。

 Node* insert(Node* head, int index, int data)
{

if (head == NULL) return NULL; // if list is empty

if (index == 1) // if we have accessed node before insertion
{
    struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));
    new_node->next = head->next; // new_node->next now links to the node next in the list
    head->next = new_node; // head->next links to new node
    new_node->data = data; // assigns new node its data
    return head; // not sure how to return original head
}
return insert(head->next, index - 1, data);

}

【问题讨论】:

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


    【解决方案1】:

    对于初学者,指定必须插入节点的位置的参数应该是无符号整数类型,例如size_t。职位应从0开始。

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

    struct Node * insert( struct Node *head, size_t pos, int data )
    {
        if (head == NULL || pos == 0 )
        {
            struct Node *new_node = malloc( sizeof( struct Node ) );
            new_node->next = head;
            new_node->data = data;
            head = new_node;
        }
        else
        {
            head->next = insert( head->next, pos - 1, data );
        }
    
        return head;
    }
    

    这是一个演示程序

    #include <stdio.h>
    #include <stdlib.h>
    
    struct Node
    {
        int data;
        struct Node *next;
    };
    
    struct Node * insert( struct Node *head, size_t pos, int data )
    {
        if (head == NULL || pos == 0)
        {
            struct Node *new_node = malloc( sizeof( struct Node ) );
            new_node->next = head;
            new_node->data = data;
            head = new_node;
        }
        else
        {
            head->next = insert( head->next, pos - 1, data );
        }
    
        return head;
    }
    
    void print( const struct Node *head )
    {
        for (; head != NULL; head = head->next)
        {
            printf( "%d -> ", head->data );
        }
        puts( "null" );
    }
    
    int main( void )
    {
        struct Node *head = NULL;
    
        head = insert( head, 0, 3 );
        print( head );
    
        head = insert( head, 0, 0 );
        print( head );
    
        head = insert( head, 1, 1 );
        print( head );
    
        head = insert( head, 2, 2 );
        print( head );
    }
    

    程序输出是

    3 -> null
    0 -> 3 -> null
    0 -> 1 -> 3 -> null
    0 -> 1 -> 2 -> 3 -> null
    

    【讨论】:

    • 给定了函数及其参数,因此我无法更改它们,但我可以简单地代替 pos == 0 来执行 pos == 1 并获得相同的结果,对吗?
    • @CoolMcDougal 在这种情况下,您应该将 else 语句更改为 else if 语句 if ( head == NULL || pos == 1 ) { /*...*/ } else if ( pos > 1 ) { /*...*/ } 返回头;因为负位置对单链表没有意义。
    • @CoolMcDougal 虽然无论如何索引从 0 开始会更好。所以你可以写 if ( head == NULL || pos == 0 ) { /*...*/ } else if ( pos > 0 ) { /*...*/ } return head;
    • 好的,我明白了,谢谢。你介意向我解释一下这条线吗? head->next = insert(head->next, index - 1, data);
    • @CoolMcDougal 假设您已经附加了一个节点。即列表包含一个节点。它的数据成员 next 等于 NULL。然后,您需要附加第二个节点。你调用函数。头不等于 NULL。所以需要对head->next递归调用函数,然后用函数递归调用返回的新创建节点的指针设置数据成员head->next。
    【解决方案2】:
    Node *insertRecursive(Node* head,int pos,int val)
    {
        if(pos==0 || head==NULL)
        {
            Node *newNode= new Node(val);
            newNode->next=head;
            head=newNode;
            return head;
        }
        else
            head->next = insertRecursive(head->next,pos-1,val);
        
    }
    

    【讨论】:

    • 对您的代码的评论将有助于您的答案被接受,也有利于社区。​​span>
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-09-07
    • 2021-12-02
    • 1970-01-01
    • 2011-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多