Presentation is loading. Please wait.

Presentation is loading. Please wait.

Doubly Linked Lists Lecture 21 Tue, Mar 21, 2006.

Similar presentations


Presentation on theme: "Doubly Linked Lists Lecture 21 Tue, Mar 21, 2006."— Presentation transcript:

1 Doubly Linked Lists Lecture 21 Tue, Mar 21, 2006

2 Topics Doubly Linked Lists Doubly Linked List Nodes
Validity Requirements Inserting and Deleting

3 Doubly Linked Lists A doubly linked list is a linked list in which each node has two links A forward pointer to its successor A backwards pointer to its predecessor.

4 Doubly Linked List Nodes
A DoublyLinkedListNode has three data members: T data - Value stored in the node. DoublyLinkedListNode* prev - Pointer to the previous node. DoublyLinkedListNode* next - Pointer to the next node.

5 The Data Members A DoublyLinkedList object has three data members:
int size - Number of elements in the list. DoublyLinkedListNode* head - Pointer to the first node. DoublyLinkedListNode* tail - Pointer to the last node.

6 Chasing Pointers We can move both forwards and backwards in the list.
When chasing pointers, it is not necessary to keep a pointer to the previous node.

7 Validity Requirements
size >= 0. If size == 0, then head == NULL tail == NULL If size >= 1, then head != NULL tail != NULL

8 Validity Requirements
If size == 1, then head == tail If size >= 1, then The next pointer in node size is NULL. The prev pointer in node 1 is NULL.

9 Validity Requirements
If size >= 2, then For every i from 1 to size - 1, the next pointer in node i is not NULL. For every i from 2 to size, the prev pointer in node i is not NULL. The prev pointer of node 2 equals head. The next pointer of node size - 1 equals tail.

10 Validity Requirements
If size >= 3, then For every i from 2 to size - 1, the next pointer of node i - 1 equals the prev pointer of node i + 1.

11 Inserting into a Doubly Linked List
Locate the position for the insertion. Create the new node. Update the forward pointers. Update the backward pointers. Increment the size of the list. Do we need both the prev and curr pointers?

12 Deleting from a Doubly Linked List
Locate the position for the deletion. Update the forward pointer. Update the backward pointer. Delete the node. Decrement the size of the list.

13 The DoublyLinkedList Class
Example doublylinkedlistnode.h doublylinkedlist.h ListTest.cpp


Download ppt "Doubly Linked Lists Lecture 21 Tue, Mar 21, 2006."

Similar presentations


Ads by Google