Differences between one-way and two-way linked lists

Source: Internet
Author: User

A node in a singly linked list has only a Next reference. each node is referenced by a preceeding node (including the first node, which will be referenced by the list Head or from the last node in the case of a circularly linked list ). in order to remove a node, the preceeding node must be made to reference the removed node's following node.

However, you cannot find the preceeding node directly; you have to search the list until you find it.

For example, suppose you want to remove node4 from this list, given only a reference to node4:

Head-> node1.next-> node2.next-> node3.next-> node4.next-> node5.next-> null

You need to update the 'Next' reference of node3 to reference node5:

Head-> node1.next-> node2.next-> node3.next-> node5.next-> null

In order to find node3 to update it's 'Next' reference, you wocould need to start at 'head' and work your way through the list until you reach node4, remembering the previous node at each iteration. that is an O (N) operation.

Conversely, with a doubly linked list:

Head-> node1.next-> node2.next-> node3.next-> node4.next-> node5.next-> null
<-. Prev

If you want to delete a node, you just need to do this:

Class Node
{
Node Next;
Node Prev;
}

DeleteNode (Node node)
{
Node. Prev. Next = node. Next;
}

Which is an O (1) operation.


Summary:

A doubly-linked listMakes it very easy (code wise) to delete or insert entries. if you have a pointer to a node, any node, and you want to either delete that one or else insert another before or after it, then having access to both "next" and "prev" node pointers makes this very, very easy code. and fast, too.

A singly-linked listOnly has links going in one direction (forward or backward can't really be told apart in this case) and if you want to just delete a node where you have only the pointer to it, you are kind of in trouble. you need to start at the beginning of the linked list, search through it to find the node that points to the node you are supposed to delete, and then you can do it. as you can imagine, this is more time consuming and it is more code, as well. but it's very easy to insert a node at the head of a singly-linked list





References:



Http://answers.yahoo.com/question/index? Qid = 20130729013025 AAzeInE


Http://social.msdn.microsoft.com/Forums/vstudio/en-US/270bebdb-9032-4fc1-97c6-bc017d7e0a45/when-to-use-single-linked-list-and-when-to-use-double-linked-list? Forum = csharpgeneral


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.