Linear Data Structures: Doubly Linked Nodes Flashcards
What are the components of a doubly linked node chain?
- A data item
- A reference to the next node
- A reference to the previous node
The first node has its ‘previous’ node reference set to null
The last node’s ‘next’ reference is set to null.
How is the structure of a doubly linked list different from a single-linked node chain?
A doubly linked list node has a reference to both its previous node and next node. A single-linked node chain only has a reference to the next node.
The overall structure includes a list object that manages the nodes.
Updates to do doubly-linked node insertion at the beginning
- The new node’s “next” reference is set to the current head node
- The head reference is updated to point to the new node
- The previous “head” node’s “previous” reference is updated to point to the new node
The new node’s ‘next’ reference is set to the current head node.
Updates to do doubly-linked node insertion at the end
- The current last node’s “next” reference is set to the new node
- The new node’s “previous” reference is set to the current tail node
- The tail reference in the list object is updated to point to the new node
The new node’s ‘previous’ reference is set to the current tail node.
Updates to do doubly-linked node insertion at the middle
- Update the ‘next’ reference of node before insertion point (P)
- Update the ‘previous’ reference of node after insertion point (Q)
- Set new node’s ‘next’ reference to Q
- Set new node’s ‘previous’ reference to P
What is one disadvantage of doubly linked lists?
They use more space than singly linked lists, because of the additional node reference
Each node stores an additional reference.
What are two advantages of doubly linked lists?
- Constant time insertion and deletion at both ends
- Support for cursors and iterators that can move both forwards and backwards
How can a doubly-linked list improve the time complexity for insertion into the middle list?
It can reduce from O(n) to O(1) due to the ability to find the previous node in constant time
What does extending the LinkedIterator class for doubly linked lists enable?
It enables both forward and backward movement
Methods must be added to support backward movement.