Why we use doubly linked list instead of singly linked list?

Insertion is clearly less work in a singly-linked list, as long as you are content to always insert at the head or after some known element. [That is, you cannot insert before a known element, but see below.]

Deletion, on the other hand, is trickier because you need to know the element before the element to be deleted.

One way of doing this is to make the delete API work with the predecessor of the element to be deleted. This mirrors the insert API, which takes the element which will be the predecessor of the new element, but it's not very convenient and it's hard to document. It's usually possible, though. Generally speaking, you arrive at an element in a list by traversing the list.

Of course, you could just search the list from the beginning to find the element to be deleted, so that you know what its predecessor was. That assumes that the delete API includes the head of the list, which is also inconvenient. Also, the search is stupidly slow.

The way that hardly anyone uses, but which is actually pretty effective, is to define a singly-linked list iterator to be the pointer to the element preceding the current target of the iterator. This is simple, only one indirection slower than using a pointer directly to the element, and makes both insertion and deletion fast. The downside is that deleting an element may invalidate other iterators to list elements, which is annoying. [It doesn't invalidate the iterator to the element being deleted, which is nice for traversals which delete some elements, but that's not much compensation.]

If deletion is not important, perhaps because the datastructures are immutable, singly-linked lists offer another really useful property: they allow structure-sharing. A singly-linked list can happily be the tail of multiple heads, something which is impossible for a doubly-linked list. For this reason, singly-linked lists have traditionally been the simple datastructure of choice for functional languages.

Although this question has already been answered, I am somehow not satisfied with the answer [no offense meant], so here's how I would reply to it:

What to use - Singly or Doubly linked list depends on what you intend to achieve and system limitations, if any.

Singly-linked list:

Pros: Simple in implementation, requires relatively lesser memory for storage, assuming you need to delete/insert [at] next node – deletion/insertion is faster.

Cons: Cannot be iterated in reverse, need to maintain a handle to the head node of the list else, the list will be lost in memory. If you’re deleting previous node or inserting at previous node, you will need to traverse list from head to previous node to be able to carry out those operations – O[N] time.

--So, this should be used when you have lesser memory and your main goal is insertion/deletion and not searching elements.

Doubly-linked list:

Pros: Can be iterated in forward as well as reverse direction. In case of needing to delete previous node, there is no need to traverse from head node, as the node to be deleted can be found from ‘.previous’ pointer.

Cons: Relatively complex to implement, requires more memory for storage [1 ‘.previous’ pointer per node]. Insertions and deletions are relatively more time consuming [assigning/reassigning ‘.previous’ pointer for neighbor nodes]

--This should be used when you have no or minimal limitations on memory, and your main goal is to search for elements.

If there are some more pros and cons to any, please feel free to add, reply in comments. Thanks!

Difference Between Doubly linked list vs Singly linked list

  • The singly linked list and doubly linked list is a type of linked list to arrange memory and information.
  • The singly linked list and doubly linked list is part of dynamic data structure to avoid memory wastage and traverse using element in the list.
  • The doubly linked list is a function that contains data, next node, and previous node simultaneously.
  • The singly linked list is a function that contains data and the next node only.
  • The singly linked list is a simple linked list to traverse one way from the first node to the next node.
  • The doubly linked list is a complex linked list to traverse both ways from one node to another and vice versa.
  • The singly linked list contains two parts, such as memory and pointer, but the last pointer becomes null.
  • The doubly linked list contains three parts such as a previous pointer, memory node, and next pointer but the initial and last pointer becomes null.

Head to Head Comparison Between Doubly linked list vs Singly linked list [Infographics]

Below are the top differences between Doubly linked list vs Singly linked list

Key differences between Doubly linked list vs Singly linked list

Some of the key differences between Doubly linked list vs Singly linked list are given below:

  • The doubly linked list is bidirectional because of two address pointer. Therefore, a singly linked list is unidirectional because of the one address pointer.
  • The doubly linked list has occurred more memory space than the singly linked list.
  • The singly linked list simple, whereas the doubly linked list, is a complex dynamic data structure of the list.
  • The doubly linked list provides an empty head and tail pointer. Hence, a singly linked list provides an empty tail only.
  • The doubly linked list more efficient than the singly list.
  • The doubly linked list contains three parameters, and the singly linked list contains two parameters.
  • The singly linked list image is given below.

  • The doubly linked list image is given below.

  • The doubly linked list gives time complexity O[1], whereas the singly linked list gives time complexity O[n].

Comparison table

  • The doubly linked list is a complex function, and the singly linked list is a simple data structure.
  • The comparison table is displayed features and descriptions of the singly linked list and doubly linked list.
  • The below table is showing similarities and differences of the type of the linked list.
Features doubly linked list singly linked list
Definition The doubly linked list is a complex linked list to manage memory with previous and next pointer. The singly linked list is a simple linked list to manage memory with the next pointer.
Function Organize dynamic data structure or values of the list. Organize dynamic data structure or values of the list.
Parameter
  • Memory node
  • Previous node
  • Next node
Algorithm The doubly linked list algorithm is below.

1] set Pointer = null

2] set New node = pointer

3] set Pointer = pointer -> next

4] set New node -> data = value

5] set New node -> previous  = null

6] set New node -> next = start

7] set New head -> previous = New node

8] set New head = New node

[continue procedure until last pointer]

9] last pointer -> null = tail

10] exit

The singly linked list algorithm is below.

1] set Pointer = null

2] set New node = pointer

3] set Pointer = pointer -> next

4] set New node -> data = value

5] set New node -> next = new head

6] set New head = New node  [continue procedure until last pointer]

7] last pointer -> null = tail

8] exit

Description The head pointer and tail are empty. Other nodes are including data. The tail pointer is empty. The head and other nodes are including data.
direction The node pointer addresses forward and reverses direction in the linked list. The doubly linked list supports bidirectional. The node pointer addresses only the forward direction because of the next node.  This linked list does not traverse the reverse direction. The doubly linked list supports unidirectional.
Memory space The doubly linked list contains two addresses of the node. This variable takes 8-byte memory space. The singly linked list contains one address of the node. This variable takes 4-byte memory spaces.
Time complexity The time complexity of basic operation such as insert and delete of the list is

O [1].

The time complexity of basic operation such as insert and delete of the list is

O [n].

complexity The doubly linked list is complex than a singly linked list to handle and operate data. It is difficult to manage data and its address. The singly linked list is simple than a doubly-linked list to handle and operate data. It is easy to manage data and its address.
Operation
  • Insert the data in the list.
  • Delete the data from the list.
  • Traversing from each element
  • Searching element from the linked list
  • Insert the data in the list.
  • Delete the data from the list.
  • Traversing from each element
  • Searching element from the linked list
Advantages and limitations
  • Random access to the data is possible.
  • The deletion is easy than the singly linked list.
  • Uses more memory than the singly linked list.
  • Easy to operate and handle any data.
  • Random access to the data is possible.
  • The insertion process is easy than the doubly linked list.
  • Use less memory.
  • Cannot handle previous data of the list.
Implementation
  • Stack
  • Hash table
  • Binary tree
  • Stack
  • Queue
  • Fibonacci heap
Real-time Example
  • Navigation system to get backward and forward place.
  • Operate Undo and Redo functionality in the application.
  • The queue of the people.
  • Utensil or plate dispenser in the buffet.

Conclusion

  • The singly linked list and doubly linked list make the application usable, handy, and manageable.
  • The singly linked list and doubly linked list helps to manage and operate a list of data.

Recommended Articles

This is a guide to the Doubly linked list vs Singly linked list. Here we discuss the Doubly linked list vs Singly linked list key differences with infographics and comparison table. You may also have a look at the following articles to learn more –

  1. Snapseed vs Lightroom
  2. Krita vs Photoshop
  3. Lumion vs V-Ray
  4. Lubuntu vs Xubuntu

Video liên quan

Chủ Đề