Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations

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

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

2 Three 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 Keep size as part of the structure 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 Keep size as a field in the header Maintain the size value explicitly in header. Can you think why we want to do it this way? struct linkedList { struct link * frontSentinel; struct link * backSentinel; int size; };

9 Deque Why would you use a sentinel? Consider a deque, with pointer to front and pointer to sentinel How do you access the front of the queue? How do you access the back? Draw pictures, name the values you are looking for

10 Generalize add What about adding values Remember, for a queue, can add either to the front OR to the back Add to front and add to back are now special cases of more general add After operation.

11 Deque code void listDequeAddBack (struct list *q, EleType newValue) { _addLinkAfter(q, q->frontSentinel, newValue); } void listDequeAddFront (struct list *q, EleType newValue) { _addLinkAfter(q, q->backSentinel->prev, newValue); }

12 Are there any special cases? Draw pictures Does this work even if the list is empty? Always try to imagine special cases, make sure your code works for them

13 Both a stack and a queue To use a deque as a stack, use addFront and removeFront To use as a queue, use addBack and removeFront (or vice versa) Works for both equally well

14 What does add do? Needs to add a new link to a chain, right before the link given as the argument. Draw a picture. Label the names of everything you know. Think through the steps. Why are we passing pointer to header? You will get to do this in the worksheet.

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

16 Again, draw pictures Draw a picture to see what you are doing. Label the names for things. What are the steps? Do they need to be done in a particular order? Why are we passing the header node to the removeLink routine?

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 Remember the Bag operations Add (already done) Contains (same as before, walk down the links, looking at every element) Remove (walk down the links, when you find the value you want, call removeLink) Size (this one is easy)

19 Now your chance Questions? Doubly linked list is used in programming assignment 3. Mainly you need to write addLink and removeLink

20 Oh, that exam on Tuesday? Questions on big-Oh (what is the running time of …) Implementation questions like we have been doing (how to implement … )

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

Similar presentations

Ads by Google