Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 261 - Fall 2009 Linked Lists - Part 2 Deque with Double Links and Sentinel, Bag.

Similar presentations


Presentation on theme: "CS 261 - Fall 2009 Linked Lists - Part 2 Deque with Double Links and Sentinel, Bag."— Presentation transcript:

1 CS 261 - Fall 2009 Linked Lists - Part 2 Deque with Double Links and Sentinel, Bag

2 Two New Variations Today we are going to continue to explore Linked Lists with three new variations Double links - links both forwards and backwards Sentinel - a Special marker link at end Keeping size explicitly Close to the way standard libs do it

3 Double Links Double links point both forwards and backwards. Allow access to both next and previous link struct link { EleType value; struct link * next; struct link * previous; };

4 Picture of List with double link

5 2nd Variation: Sentinels A Sentinel is a special marker node at front or back Has no value, is never removed Can remove special case code, since pointer is never null An “Empty” list still has a sentinel node

6 Picture of List with Sentinel

7 If one Sentinel is good Just to make the LinkedList slightly more interesting, we will add a sentinel to BOTH front and back Eliminates even more special cases most “Real” LinkedList use two sentinels, double links, and more or less combines all the interfaces. (Java, C++)

8 Third variation - explicit size Keep size in the header structure. Can anybody explain why this is good? struct linkedList { int size; struct link * frontSentinel; struct link * backSentinel; };

9 Initialization code /* these functions are written for you */ void LinkedListInit (struct linkedList *q) { q->frontSentinel = (struct dlink *) malloc(sizeof(struct dlink)); assert(q->frontSentinel != 0); q->backSentinel = (struct dlink *) malloc(sizeof(struct dlink)); assert(q->backSentinel); q->frontSentinel->next = q->backSentinel; q->backSentinel->prev = q->frontSentinal; q->size = 0; }

10 What characterizes an empty collection? Int listDequeIsempty (struct list *q) { }

11 Deque Why would you use a sentinel? Consider a deque, with pointer to front and pointer to sentinel Add to front and add to back are now special cases of more general “add after” operation.

12 Deque code Void listDequeAddBack (struct LinkedList *q, EleType newValue) { _addLink(q, q->backSentinel->prev, newValue); } void listDequeAddFront (struct linkedList *q, EleType newValue) { _addLink(q, q->frontSentinel, newValue); }

13 Writing _addLink (adds AFTER the given link) Void _addLink (struct linkedList *lst, struct link * lnk, EleType e) { }

14 Getting at the elements EleType front (struct linkedList *lst) { } EleType back (struct linkedList *lst) { }

15 Removes also generalized void listRemoveFirst (struct list *q) { assert(! listIsEmpty(q)); _removeLink (q, q->frontSentinel->next); } void listRemoveLast (struct list *q) { assert(! listIsEmpty(q)); _removeLink (q, q->backSentinel>prev); }

16 How to write _removeLink Void _removeLink (struct linkedList *lst, struct link *lnk) { }

17 You will get your chance You will get your chance to write _addLink and _removeLink in a moment. The same removeLink idea can be used to implement the remove operation in the LinkedListBag Remember from yesterday, remove was the only complicated operation

18 Now your chance Questions? worksheet time


Download ppt "CS 261 - Fall 2009 Linked Lists - Part 2 Deque with Double Links and Sentinel, Bag."

Similar presentations


Ads by Google