Doubly Linked Lists Lecture 21 Section 11 3
Doubly Linked Lists Lecture 21 Section 11. 3 Mon, Mar 19, 2007
Doubly Linked Lists n A doubly linked list is a linked list in which each node has two links A forward pointer to its successor n A backwards pointer to its predecessor. n
Doubly Linked List Nodes n A Doubly. Linked. List. Node has three data members: T data - Value stored in the node. n Doubly. Linked. List. Node* prev - Pointer to the previous node. n Doubly. Linked. List. Node* next - Pointer to the next node. n
The Data Members n A Doubly. Linked. List object has three data members: int m. Size - Number of elements in the list. n Doubly. Linked. List. Node* head - Pointer to the first node. n Doubly. Linked. List. Node* tail - Pointer to the last node. n
Chasing Pointers n We can move both forwards and backwards in the list. n When chasing pointers, it is not necessary to keep a pointer to the previous node.
Validity Requirements n size >= 0. n If size == 0, then n head == NULL n tail == NULL n If size >= 1, then n head != NULL n tail != NULL
Validity Requirements n If size == 1, then n head == tail n If size >= 1, then n The next pointer in node size is NULL. n The prev pointer in node 1 is NULL.
Validity Requirements n If size >= 2, then n For every i from 1 to size - 1, the next pointer in node i is not NULL. n For every i from 2 to size, the prev pointer in node i is not NULL. n The prev pointer of node 2 equals head. n The next pointer of node size - 1 equals tail.
Validity Requirements n If size >= 3, then n For every i from 2 to size - 1, the next pointer of node i - 1 equals the prev pointer of node i + 1.
Inserting into a Doubly Linked List n Locate the position for the insertion. n Create the new node. n Update the forward pointers. n Update the backward pointers. n Increment the size of the list.
Deleting from a Doubly Linked List n Locate the position for the deletion. n Update the forward pointer. n Update the backward pointer. n Delete the node. n Decrement the size of the list.
The Doubly. Linked. List Class n Example n n n doublylinkedlistnode. h doublylinkedlist. h List. Test. cpp
- Slides: 12