Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why would I ever use a DoublyLinkedList in PHP?

I've recently come across some of the PHP-SPL data structures and the I've been looking over the first one, the doubly linked list. I've a rough idea what a linked list is and now I can see what a doubly linked list is but my question is: What in the world would I do with this?

I seems like it would be just as easy to use an array. Can some Computer Science type enlighten me?

like image 427
Will Avatar asked Dec 31 '10 19:12

Will


People also ask

What is a good reason to use doubly linked lists?

The most common reason to use a doubly linked list is because it is easier to implement than a singly linked list. While the code for the doubly linked implementation is a little longer than for the singly linked version, it tends to be a bit more “obvious” in its intention, and so easier to implement and debug.

When would you use a doubly linked list?

Doubly linked list can be used in navigation systems where both forward and backward traversal is required. It can be used to implement different tree data structures. It can be used to implement undo/redo operations.

What is the reason to use a doubly linked list rather than a singly linked list to implement the queue ADT?

If we need better performance while searching and memory is not a limitation in this case doubly linked list is more preferred. As singly linked list store pointer of only one node so consumes lesser memory. On other hand Doubly linked list uses more memory per node(two pointers).

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

Singly linked list is preferred when we have memory limitation(we can't use much memory) and searching is not required. Doubly linked list is preferred when we don't have memory limitation and searching is required(we need to perform search operation on the linked list).


1 Answers

Unlike a singly-linked list, a doubly linked list can walk the list in either direction, and do object insertion and deletion in the middle of the list in O(1) (provided you already has access to spot in the list where it's going to happen, unlike a singly linked list. That said, doubly linked lists are inferior in other ways and are defiantly not something you'll come across that often in practice.

like image 125
Tyler Eaves Avatar answered Sep 27 '22 21:09

Tyler Eaves