jiahao42 / blog

Personal blog

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Are you happy with people discuss with your blog?

randomwangran opened this issue · comments

http://idle.systems/posts/taste-of-code.html

I read your blog, and I found that you like Feynman as well.

So I would like to discuss the things interested to you.

假设此时我们的链表中有5个结点,其内容分别是1/2/3/4/5,有一head指针指向头结点,我们希望删除结点2。

I am interested to discuss with you, but not sure are you happy with it or not?

Yes, sure, I am happy to receive comments, especially from people who like Feynman :)

Thanks for pointing the error out, the number of nodes should be 6, I will fix it shortly.

I took a second look at the text you quoted, I think it may be better if we remove the head node in the graph, like:

假设此时我们的链表中有5个结点,其内容分别是1/2/3/4/5,我们希望删除结点2。

                          entry -+
                             |
           +-------+     +-------+     +-------+     +-------+     +--------+
           | 1 |   |---->| 2 |   |---->| 3 |   |---->| 4 |   |---->| 5 |NULL|
           +-------+     +-------+     +-------+     +-------+     +--------+

The reason is that, if the linked list actually have a dummy head node, then it will be fairly easy to remove a certain node without extra if statement, just do

void remove_list_entry(linked_list* entry) {
    linked_list* prev = head;
    linked_list* walk = head->next;

    //Walk the list 
    while (walk != entry) {
        prev = walk;
        walk = walk->next;
    }
    prev->next = entry->next;
}

Really apprecite it for answering my question. Thank you.

People who alsoa are interested in linked list: https://github.com/mkirchner/linked-list-good-taste