cs261 data structures
play

CS261 Data Structures Linked List Implementation of the Deque - PowerPoint PPT Presentation

CS261 Data Structures Linked List Implementation of the Deque Deque Interface (Review) void addFront(TYPE val); // Add value at front of deque. void addBack (TYPE val); // Add value at back of deque. void removeFront(); // Remove value at


  1. CS261 Data Structures Linked List Implementation of the Deque

  2. Deque Interface (Review) void addFront(TYPE val); // Add value at front of deque. void addBack (TYPE val); // Add value at back of deque. void removeFront(); // Remove value at front. void removeBack (); // Remove value at back. TYPE front(); // Get value at front of deque. TYPE back(); // Get value at back of deque.

  3. Linked List Deque • What if we want to add and remove elements from both front and back? tail List head link link link tail head … val val val links links links

  4. Modification #3: Double Links prev • Point forward to the next element Link next • Point backwards to the prev ious element struct DLink { TYPE val; struct DLink *next; /* Link to prev node. */ struct DLink *prev; /* Link to next node. */ }; lastLink List firstLink prev prev prev prev … Link Link Link next next next next

  5. linkedListDeque Struct struct linkedList { int size; struct dlink * frontSentinel; struct dlink * backSentinel; };

  6. linkedListDequeInit void LinkedListInit (struct linkedList *q) { q->frontSentinel = malloc(sizeof(struct dlink)); assert(q->frontSentinel != 0); q->backSentinel = malloc(sizeof(struct dlink)); assert(q->backSentinel); q->frontSentinel->next = q->backSentinel; q->backSentinel->prev = q->frontSentinal; q->size = 0; backSent } List frontSent prev next How can we test for an empty deque? Sentinel Sentinel

  7. Advantage of Sentinels • Consider a deque, with two sentinels A,B : This is similar to most standard library – Pointer to front sentinel: frontSent Deque implementations (Java LinkedList) – Pointer to back sentinel: backSent • Add to front and add to back are now special cases of more general “ add before ” operation AddBefore(nodeB, X) A B A X B

  8. Advantage of Sentinels • Consider a deque, with two sentinels: This is similar to most standard library – Pointer to front sentinel: frontSent Deque implementations (Java LinkedList) – Pointer to back sentinel: backSent • Add to front and add to back are now special cases of more general “ add before ” operation backSent List frontSent prev prev prev prev … Link Link next next next next

  9. Adding to the LL Deque void addBackListDeque(struct ListDeque *q,TYPE val) { _addBefore(q->backSent, val); } void addFrontListDeque(struct ListDeque *q,TYPE val) { _addBefore(q->frontSent->next, val); } backSent List frontSent prev prev prev prev prev … Link Link next next next next next

  10. Adding to the LL Deque void addBackListDeque(struct ListDeque *q,TYPE val) { _addBefore(q->backSent, val); } void addFrontListDeque(struct ListDeque *q,TYPE val) { _addBefore(q->frontSent->next, val); } backSent List frontSent prev prev prev prev prev … Link Link next next next next next

  11. Removing from the LL Deque void removeFirstListDeque(struct ListDeque *q) { assert(!isEmptyListDeque(q)); _removeLink(q->frontSent->next); } void removeLastListDeque(struct ListDeque *q) { assert(!isEmptyListDeque(q)); _removeLink (q->backSent->prev); } backSent List frontSent prev prev prev prev prev … Link Link next next next next next

  12. Your Turn… Worksheet #19: _addBefore, _removeLink DynArrDeque LLDeque best, ave, worst best, ave, worst addLast O(1),O(1+), O(N) O(1),O(1),O(1) removeLast O(1), O(1),O(1) O(1),O(1),O(1) addFirst O(1),O(1+), O(N) O(1),O(1),O(1) removeFirst O(1),O(1),O(1) O(1),O(1),O(1)

  13. _addBefore void _addBefore(struct DLink *link, TYPE val) { struct DLink *newLink = malloc (…); assert(newLink != 0); newLink->val = val; newLink->next = link; struct Dlink { newLink->prev = link->prev; TYPE val; struct DLink *next; struct DLink *prev; link->prev->next = newLink; } link->prev = newLink; } backSent List frontSent prev prev prev prev prev … Link Link next next next next next

Recommend


More recommend