Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lists (2). Circular Doubly-Linked Lists with Sentry Node Head.

Similar presentations


Presentation on theme: "Lists (2). Circular Doubly-Linked Lists with Sentry Node Head."— Presentation transcript:

1 Lists (2)

2 Circular Doubly-Linked Lists with Sentry Node Head

3 Doubly-Linked Lists Each list node contains three fields –Data –Prev link: points to the previous node –Next link: points to the next node

4 Circular Doubly-Linked List The next link of the last node points to the first node The previous link of the first node points to the last node

5 The Sentry Node The first node of the list Is pointed by the head link The three fields –Data: null –Prev link: points to the last node of the linked list –Next link: points to the first data node

6 Circular Doubly-linked Lists with Sentry Node Always contains at least one node – the sentry node Is defined as empty if it contains only the sentry node, which is pointed by head The head link is never equal to null The sentry node is transparent to the user

7 Practices Draw a circular doubly-linked list with sentry node that contains three data nodes Draw a circular doubly-linked list with sentry node that contains two data nodes Draw a circular doubly-linked list with sentry node that contains one data node Draw an empty circular doubly-linked list with sentry node

8 Traverse Node p = head; while (p != null) { process p.data p = p.next; } Does this algorithm still work?

9 Traverse Node p = head.next; while (p != head) { process p.data p = p.next; }

10 Critical Thinking Does the algorithm work for a circular doubly-linked list with sentry node that contains two data nodes? Does the algorithm work for a circular doubly-linked list with sentry node that contains one data node? Does the algorithm work for an empty circular doubly-linked list with sentry node?

11 InsertAfter newNode p

12 InsertAfter (Step 1) newNode p

13 InsertAfter (Step 2) newNode p

14 InsertAfter (Step 3) newNode p

15 InsertAfter (Step 4) newNode p

16 Critical Thinking Does it work for an empty circular doubly- linked list with sentry node (i.e., p is equal to head)?

17 Critical Thinking Do you need insertFirst at all? If you don’t, how do you insert a node and make it the first node?

18 Remove (Step 0) p

19 Critical Thinking Does it work for the list that contains one data node? Does it work for an empty list?


Download ppt "Lists (2). Circular Doubly-Linked Lists with Sentry Node Head."

Similar presentations


Ads by Google