1 / 11

CS261 Data Structures

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

ocean
Download Presentation

CS261 Data Structures

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS261 Data Structures Linked List Implementation of the Deque

  2. Deque Interface (Review) int isEmpty(); 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 • Point backwards to the previous element structDLink { TYPE val; structDLink *next; /* Link to prevnode. */ structDLink *prev; /* Link to next node. */ }; Link next lastLink List firstLink prev prev prev prev … Link Link Link next next next next

  5. linkedListDequeStruct structlinkedList { intsize; structdlink * frontSentinel; structdlink * backSentinel; };

  6. linkedListDequeInit void LinkedListInit (structlinkedList *q) { q->frontSentinel = malloc(sizeof(structdlink)); assert(q->frontSentinel != 0); q->backSentinel = malloc(sizeof(structdlink)); assert(q->backSentinel); q->frontSentinel->next = q->backSentinel; q->backSentinel->prev = q->frontSentinal; q->size = 0; } backSent List frontSent prev next Sentinel Sentinel

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

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

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

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

  11. Your Turn… Worksheet #19: _addBefore, _removeLink

More Related