Because doubly linked lists have immediate access to both the front and end of the list, they can insert data on either side at O(1) as well as delete data on either side at O(1).
Because doubly linked lists can insert data at the end in O(1) time and delete data from the front in O(1) time, they make the perfect underlying data structure for a queue.
We looked at Queues , and you’ll recall that they are lists of items in which data can only be inserted at the end and removed from the beginning. You learned there that queues are an example of an abstract data type, and that we were able to use an array to implement them under the hood.
Now, since queues insert at the end and delete from the beginning, arrays are only so good as the underlying data structure. While arrays are O(1) for insertions at the end, they’re O(N) for deleting from the beginning.
A doubly linked list, on the other hand, is O(1) for both inserting at the end and for deleting from the beginning. That’s what makes it a perfect fit for serving as the queue’s underlying data structure.
Code Implementation: Queue Built Upon a Doubly Linked List
Here’s a complete example of a queue that is built upon a doubly linked list:
| | class Node |
| | |
| | attr_accessor :data , :next_node , :previous_node |
| | |
| | def initialize (data) |
| | @data = data |
| | end |
| | |
| | end |
| | class DoublyLinkedList |
| | |
| | attr_accessor :first_node , :last_node |
| | |
| | def initialize (first_node= nil , last_node= nil ) |
| | @first_node = first_node |
| | @last_node = last_node |
| | end |
| | |
| | def insert_at_end (value) |
| | new_node = Node. new (value) |
| | |
| | # If there are no elements yet in the linked list: |
| | if !first_node |
| | @first_node = new_node |
| | @last_node = new_node |
| | else # If the linked list already has at least one node: |
| | new_node. previous_node = @last_node |
| | @last_node. next_node = new_node |
| | @last_node = new_node |
| | end |
| | end |
| | |
| | def remove_from_front |
| | removed_node = @first_node |
| | @first_node = @first_node. next_node |
| | return removed_node |
| | end |
| | |
| | end |
| | |
| | class Queue |
| | attr_accessor :queue |
| | |
| | def initialize |
| | @data = DoublyLinkedList. new |
| | end |
| | |
| | def enqueue (element) |
| | @data. insert_at_end (element) |
| | end |
| | |
| | def dequeue |
| | removed_node = @data. remove_from_front |
| | return removed_node. data |
| | end |
| | |
| | def read |
| | return nil unless @data. first_node |
| | return @data. first_node . data |
| | end |
| | end |
To make this work, we added a remove_from_front method to the DoublyLinkedList class that looks like this:
| | def remove_from_front |
| | removed_node = @first_node |
| | @first_node = @first_node. next_node |
| | return removed_node |
| | end |
As you can see, we effectively delete the first node by changing the list’s @first_node to be what is currently the second node. We then return the deleted node.
The Queue class implements its methods on top of our DoublyLinkedList . The enqueue method relies on the insert_at_end method of our DoublyLinkedList :
| | def enqueue (element) |
| | @data. insert_at_end (element) |
| | end |
Similarly, the dequeue method takes advantage of the linked list’s ability to delete from the front of the list:
| | def dequeue |
| | removed_node = @data. remove_from_front |
| | return removed_node. data |
| | end |
By implementing our queue with a doubly linked list, we can now both insert and delete from the queue at a speedy O(1). And that’s doubly awesome.