cs 261 - winter 2010 linked lists - part 2 deque with double links and sentinel, bag
TRANSCRIPT
![Page 1: CS 261 - Winter 2010 Linked Lists - Part 2 Deque with Double Links and Sentinel, Bag](https://reader036.vdocuments.net/reader036/viewer/2022062312/5518d0e2550346991f8b5cb4/html5/thumbnails/1.jpg)
CS 261 - Winter 2010
Linked Lists - Part 2
Deque with Double Links and Sentinel, Bag
![Page 2: CS 261 - Winter 2010 Linked Lists - Part 2 Deque with Double Links and Sentinel, Bag](https://reader036.vdocuments.net/reader036/viewer/2022062312/5518d0e2550346991f8b5cb4/html5/thumbnails/2.jpg)
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
![Page 3: CS 261 - Winter 2010 Linked Lists - Part 2 Deque with Double Links and Sentinel, Bag](https://reader036.vdocuments.net/reader036/viewer/2022062312/5518d0e2550346991f8b5cb4/html5/thumbnails/3.jpg)
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;};
![Page 4: CS 261 - Winter 2010 Linked Lists - Part 2 Deque with Double Links and Sentinel, Bag](https://reader036.vdocuments.net/reader036/viewer/2022062312/5518d0e2550346991f8b5cb4/html5/thumbnails/4.jpg)
Picture of List with double link
![Page 5: CS 261 - Winter 2010 Linked Lists - Part 2 Deque with Double Links and Sentinel, Bag](https://reader036.vdocuments.net/reader036/viewer/2022062312/5518d0e2550346991f8b5cb4/html5/thumbnails/5.jpg)
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
![Page 6: CS 261 - Winter 2010 Linked Lists - Part 2 Deque with Double Links and Sentinel, Bag](https://reader036.vdocuments.net/reader036/viewer/2022062312/5518d0e2550346991f8b5cb4/html5/thumbnails/6.jpg)
Picture of List with Sentinel
![Page 7: CS 261 - Winter 2010 Linked Lists - Part 2 Deque with Double Links and Sentinel, Bag](https://reader036.vdocuments.net/reader036/viewer/2022062312/5518d0e2550346991f8b5cb4/html5/thumbnails/7.jpg)
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++)
![Page 8: CS 261 - Winter 2010 Linked Lists - Part 2 Deque with Double Links and Sentinel, Bag](https://reader036.vdocuments.net/reader036/viewer/2022062312/5518d0e2550346991f8b5cb4/html5/thumbnails/8.jpg)
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;
};
![Page 9: CS 261 - Winter 2010 Linked Lists - Part 2 Deque with Double Links and Sentinel, Bag](https://reader036.vdocuments.net/reader036/viewer/2022062312/5518d0e2550346991f8b5cb4/html5/thumbnails/9.jpg)
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
![Page 10: CS 261 - Winter 2010 Linked Lists - Part 2 Deque with Double Links and Sentinel, Bag](https://reader036.vdocuments.net/reader036/viewer/2022062312/5518d0e2550346991f8b5cb4/html5/thumbnails/10.jpg)
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.
![Page 11: CS 261 - Winter 2010 Linked Lists - Part 2 Deque with Double Links and Sentinel, Bag](https://reader036.vdocuments.net/reader036/viewer/2022062312/5518d0e2550346991f8b5cb4/html5/thumbnails/11.jpg)
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); }
![Page 12: CS 261 - Winter 2010 Linked Lists - Part 2 Deque with Double Links and Sentinel, Bag](https://reader036.vdocuments.net/reader036/viewer/2022062312/5518d0e2550346991f8b5cb4/html5/thumbnails/12.jpg)
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
![Page 13: CS 261 - Winter 2010 Linked Lists - Part 2 Deque with Double Links and Sentinel, Bag](https://reader036.vdocuments.net/reader036/viewer/2022062312/5518d0e2550346991f8b5cb4/html5/thumbnails/13.jpg)
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
![Page 14: CS 261 - Winter 2010 Linked Lists - Part 2 Deque with Double Links and Sentinel, Bag](https://reader036.vdocuments.net/reader036/viewer/2022062312/5518d0e2550346991f8b5cb4/html5/thumbnails/14.jpg)
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.
![Page 15: CS 261 - Winter 2010 Linked Lists - Part 2 Deque with Double Links and Sentinel, Bag](https://reader036.vdocuments.net/reader036/viewer/2022062312/5518d0e2550346991f8b5cb4/html5/thumbnails/15.jpg)
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);
}
![Page 16: CS 261 - Winter 2010 Linked Lists - Part 2 Deque with Double Links and Sentinel, Bag](https://reader036.vdocuments.net/reader036/viewer/2022062312/5518d0e2550346991f8b5cb4/html5/thumbnails/16.jpg)
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?
![Page 17: CS 261 - Winter 2010 Linked Lists - Part 2 Deque with Double Links and Sentinel, Bag](https://reader036.vdocuments.net/reader036/viewer/2022062312/5518d0e2550346991f8b5cb4/html5/thumbnails/17.jpg)
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
![Page 18: CS 261 - Winter 2010 Linked Lists - Part 2 Deque with Double Links and Sentinel, Bag](https://reader036.vdocuments.net/reader036/viewer/2022062312/5518d0e2550346991f8b5cb4/html5/thumbnails/18.jpg)
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)
![Page 19: CS 261 - Winter 2010 Linked Lists - Part 2 Deque with Double Links and Sentinel, Bag](https://reader036.vdocuments.net/reader036/viewer/2022062312/5518d0e2550346991f8b5cb4/html5/thumbnails/19.jpg)
Now your chance
• Questions?
• Doubly linked list is used in programming assignment 3. Mainly you need to write addLink and removeLink
![Page 20: CS 261 - Winter 2010 Linked Lists - Part 2 Deque with Double Links and Sentinel, Bag](https://reader036.vdocuments.net/reader036/viewer/2022062312/5518d0e2550346991f8b5cb4/html5/thumbnails/20.jpg)
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 … )