简体   繁体   English

在单个链表中的任何索引处插入新节点

[英]insert new node at any index within a single linked list

how would i go about creating a function that will allow me to insert a new node at any index within a linked list? 我将如何创建一个函数,使我可以在链表中的任何索引处插入新节点? here's the struct: 这是结构:

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

here's the function, note there's only a double pointer, index, and data parameter. 这是函数,请注意只有一个双指针,索引和数据参数。

void insertN(struct node** headRef, int index, int data);

and here's what the result should look like after calling insertN: 这是调用insertN之后的结果:

[ HEAD ] -> [ 0 ] -> [ 15 ] -> [ 10 ] -> [ 5 ] -> [ NULL ]
insertN( &head, 3, -44);
[ HEAD ] -> [ 0 ] -> [ 15 ] -> [ 10 ] -> [ -44 ] -> [ 5 ] -> [ NULL ]
insertN( &head, 4, -55);
[ HEAD ] -> [ 0 ] -> [ 15 ] -> [ 10 ] -> [ -44 ] -> [-55 ] -> [ 5 ] -> [ NULL ]
insertN( &head, 0, -66);
[ HEAD ] -> [ -66 ] -> [ 0 ] -> [ 15 ] -> [ 10 ] -> [ 5 ] -> [ NULL ]

i know how to add a new node to the head, but not at any point. 我知道如何在头上添加一个新节点,但是在任何时候都不知道。 the way i was thinking was 我的想法是

void insertN(struct node** headRef, int index, int data) {
    struct node* new;
    int i;
    for (i = 0; i <= index; i++) {
        if (i == index) {
            /* move what was here to next node and put in new node */
        }
    }

    return;
}

i just am unsure how to go about doing all this, because if something was in the node, i have to move all subsequent nodes as well. 我只是不确定如何去做所有这一切,因为如果节点中有东西,我也必须移动所有后续节点。

As you can see in the image below, you need to insert the node between two nodes. 如下图所示,您需要在两个节点之间插入节点。

The other 3 cases are 其他3例是

  • Inserting at the start of the list 在列表的开头插入
  • Inserting in the middle of list 插入列表中间
  • Inserting at the end of the list. 插入列表的末尾。

Maintain a count and loop through all the elements in the list. 保持计数并遍历列表中的所有元素。 This count will help you keep track of the index. 此计数将帮助您跟踪索引。

Once you reach the node, where you have to insert the new node 到达节点后,必须在其中插入新节点

  • Create the new node 创建新节点
  • Point the next pointer of the prev node to new node. 将上一个节点的下一个指针指向新节点。
  • Point the next pointer of the new node to current node. 将新节点的下一个指针指向当前节点。

Full Source Code available here 完整的源代码在这里

在此处输入图片说明

you have to deal with one special situation when index == 0, the headRef will need to be updated. 当index == 0时,您必须处理一种特殊情况,那么headRef将需要更新。 or if index is greater than the number of elements in the list. 或者index大于列表中的元素数。 The insert will fail. 插入将失败。 Otherwise check out the example code below. 否则,请查看下面的示例代码。

int insertN(struct node** headRef, int index, int data) {
    /* invalid headRef */
    if (!headRef) {
        return 0;
    }
    /* insert node at index */
    int i;
    struct node* new = (struct node *)malloc(sizeof(*node));
    struct node *scan = *headRef;
    new->data = data;
    new->next = 0;
    /* new head of list */
    if (scan == NULL && index == 0) {
         /* new head node */
         new->next = *headRef;
         *headRef = new;
         return 0;
    }
    /* while not at end of list and not at index */
    for (i = 0; scan != NULL && i <= index; i++) {
        if (i == index) {
            /* move what was here to next node and put in new node */
            new->next = scan->next;
            scan->next = new;
        } else {
            /* advance to next entry in list */
            scan = scan->next;
        }
    }
    /* scan == NULL indicates reached end of list before index */
    return (scan != NULL);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM