【问题标题】:Insertion time in arrays and linked lists [closed]数组和链表中的插入时间[关闭]
【发布时间】:2025-12-31 23:05:02
【问题描述】:

为什么说链表的插入和删除时间为 O(1),而数组的这些操作时间为 O(n)?

要将一个元素插入到链表中,我们是否不必检查它之前的所有元素才能找到插入位置,花费 O(n) 时间?

【问题讨论】:

  • “他”是谁?请更具体
  • 对不起,我的意思是作者说的。
  • 哪个作家?这不是很有帮助
  • 我认为这意味着:如果我们将 List 存储在数组中(天真的方式),我们需要 O(n),但在链表中我们只需要 O(1)。跨度>
  • 您已经描述了搜索 (O(n)),然后是插入 (O(1))。在数组中,搜索是 O(1),插入是 O(n)。如果您已经引用了要插入的节点,则插入时间为 O(1)。如果你不这样做,那么你必须先搜索它是正确的,这需要 O(n) 次操作。通常,链表中的插入和删除发生在其两端(对于堆栈和/或队列),在这种情况下,操作是 O(1) 时间。

标签: arrays algorithm data-structures linked-list


【解决方案1】:

如果c中的节点是这样的

// A linked list Node
struct Node {
    int data;
    struct Node* next;
};

在链表前面插入一个新节点如下!

void push(struct Node** head_ref, int new_data)
{
    /* 1. allocate node */
    struct Node* new_node = (struct Node*) malloc(sizeof(struct Node));
  
    /* 2. put in the data  */
    new_node->data  = new_data;
  
    /* 3. Make next of new node as head */
    new_node->next = (*head_ref);
  
    /* 4. move the head to point to the new node */
    (*head_ref)    = new_node;
}

你可以看到没有循环,也不需要遍历列表,因为我们有一个指向插入位置的指针(它是列表的开头或结尾)。 更多参考请见Linked List Inserting a node

在 Array 中的特定位置插入元素的含义如下: 下面是一个在数组中特定位置插入元素的 C 程序,它需要大约 n/2 操作等于复杂度 o(n)

#include <stdio.h>

int main()
{
    int arr[100] = { 0 };
    int i, x, pos, n = 10;

    // initial array of size 10
    for (i = 0; i < 10; i++)
        arr[i] = i + 1;

    // print the original array
    for (i = 0; i < n; i++)
        printf("%d ", arr[i]);
    printf("\n");

    // element to be inserted
    x = 50;

    // position at which element
    // is to be inserted
    pos = 5;

    // increase the size by 1
    n++;

    // shift elements forward
    for (i = n-1; i >= pos; i--)
        arr[i] = arr[i - 1];

    // insert x at pos
    arr[pos - 1] = x;

    // print the updated array
    for (i = 0; i < n; i++)
        printf("%d ", arr[i]);
    printf("\n");

    return 0;
}

更多参考请见Insert an element in an Array

注意在链表中需要的位置插入一个Node也需要n/2操作,也是O(n),如下:

void insertPos(Node** current, int pos, int data)
{
    // This condition to check whether the
    // position given is valid or not.
    if (pos < 1 || pos > size + 1)
        cout << "Invalid position!" << endl;
    else {
 
        // Keep looping until the pos is zero
        while (pos--) {
 
            if (pos == 0) {
 
                // adding Node at required position
                Node* temp = getNode(data);
 
                // Making the new Node to point to
                // the old Node at the same position
                temp->next = *current;
 
                // Changing the pointer of the Node previous
                // to the old Node to point to the new Node
                *current = temp;
            }
            else
              // Assign double pointer variable to point to the
              // pointer pointing to the address of next Node
              current = &(*current)->next;
        }
        size++;
    }
}

这两个最后的算法都需要循环,这意味着o(n)

更多参考请见Insert a node at a specific position in a linked list

【讨论】: