Inserting a node into a linked list in constant-time?

前端 未结 7 925
别跟我提以往
别跟我提以往 2021-01-24 09:09

I\'m working on an assignment that is telling me to assume that I have a singly linked list with a header and tail nodes. It wants me to insert an item y before position p. Ca

相关标签:
7条回答
  • 2021-01-24 09:34

    Hint: insertion into a linked list is only constant when position n = 0, or the head of the list. Otherwise, the worst-case complexity is O(n). That's not to say that you cannot create a reasonably efficient algorithm, but it will always have at least linear complexity.

    0 讨论(0)
  • 2021-01-24 09:34

    What you are not doing is linking the element that was before p prior to insertion of y to y. So while y is inserted before p, no one is pointing to y now (at-least not in the code snipped you showed).

    You can only insert in constant time if you know the positions of the elements between which you have to insert y. If you have to search for that position, then you can never have a constant time insertion in a single link list.

    0 讨论(0)
  • 2021-01-24 09:35

    The reason why the header and tail node is given in the question is to the update the header and tail reference if the the replacement node that your creating happens to become the header or tail. In other is words, the given previous node is either a header or tail.

    0 讨论(0)
  • 2021-01-24 09:36

    Just write it down if you get stuck with an algorithm:

    // First we have a pointer to a node containing element (elm) 
    // with possible a next element.
    // Graphically drawn as:
    // p -> [elm] -> ???
    
    tmp = new Node();
    // A new node is created. Variable tmp points to the new node which 
    // currently has no value.
    // p   -> [elm] -> ???
    // tmp -> [?]
    
    tmp.element = p.element;
    
    // The new node now has the same element as the original.
    // p   -> [elm] -> ???
    // tmp -> [elm]
    
    tmp.next = p.next;
    
    // The new node now has the same next node as the original.
    // p   -> [elm] -> ???
    // tmp -> [elm] -> ???
    
    p.element = y;
    
    // The original node now contains the element y.
    // p   -> [y] -> ???
    // tmp -> [elm] -> ???
    
    p.next = tmp;
    
    // The new node is now the next node from the following.
    // p   -> [y] -> [elm] -> ???
    // tmp -> [elm] -> ???
    

    You have the required effect, but it can be more efficient and I bet you can now find out yourself.

    It is more clear to write something like:

    tmp = new Node();
    tmp.element = y;
    tmp.next = p;
    p = tmp;
    

    Which of course does not work if p is not mutable. But your algorithm fails if p == NULL.

    But what I meant to say, is, if you have problems with an algorithm, just write the effects out. Especially with trees and linked lists, you need to be sure all pointers are pointing to the righ direction, else you get a big mess.

    0 讨论(0)
  • 2021-01-24 09:37
    create a node ptr
    ptr->info = item //item is the element to be inserted...
    ptr->next = NULL
    if (start == NULL) //insertion at the end...
        start = ptr
    else
        temp = ptr
        while (temp->next != NULL)
            temp = temp->next
        end while 
    end if
    if (start == NULL) //insertion at the beginning...
        start = ptr
    else
        temp = start
        ptr->info = item
        ptr->next = start
        start = ptr
    end if
    temp = start //insertion at specified location...
    for (i = 1; i < pos-1; i++)
        if (start == NULL)
            start = ptr
        else
            t = temp
            temp = temp->next
        end if
    end for
    t->next = ptr->next
    t->next = ptr
    
    0 讨论(0)
  • 2021-01-24 09:55

    How about using code that is already there? LinkedHashMap, LinkedList, LinkedHashSet. You can also check out the code and learn from it.

    0 讨论(0)
提交回复
热议问题