Linked List Deque

Download Report

Transcript Linked List Deque

CS 261 - Winter 2010
Linked Lists - Part 2
Deque with Double Links and
Sentinel, Bag
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
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;
};
Picture of List with double link
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
Picture of List with Sentinel
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++)
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;
};
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
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.
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); }
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
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
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.
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);
}
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?
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
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)
Now your chance
• Questions?
• Doubly linked list is used in
programming assignment 3. Mainly you
need to write addLink and removeLink
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 … )