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

cs261 data structures
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

CS261 Data Structures

Linked List Implementation

  • f the Deque
slide-2
SLIDE 2

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.

Deque Interface (Review)

slide-3
SLIDE 3
  • What if we want to add and remove elements

from both front and back?

val links val links … head links val tail link link List link head tail

Linked List Deque

slide-4
SLIDE 4
  • Point forward to the next element
  • Point backwards to the previous element

struct DLink { TYPE val; struct DLink *next; /* Link to prev node. */ struct DLink *prev; /* Link to next node. */ }; Link Link … List Link lastLink firstLink

next next next prev prev prev

Modification #3: Double Links

Link

next prev next prev

slide-5
SLIDE 5

linkedListDeque Struct

struct linkedList { int size; struct dlink * frontSentinel; struct dlink * backSentinel; };

slide-6
SLIDE 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; }

List

next

backSent frontSent

prev

Sentinel Sentinel

How can we test for an empty deque?

slide-7
SLIDE 7
  • Consider a deque, with two sentinels A,B :

– 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

A B

This is similar to most standard library Deque implementations (Java LinkedList)

Advantage of Sentinels

AddBefore(nodeB, X)

A B X

slide-8
SLIDE 8
  • 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

Link … List Link

next

backSent frontSent

next next prev prev prev next prev

This is similar to most standard library Deque implementations (Java LinkedList)

Advantage of Sentinels

slide-9
SLIDE 9

void addBackListDeque(struct ListDeque *q,TYPE val) { _addBefore(q->backSent, val); } void addFrontListDeque(struct ListDeque *q,TYPE val) { _addBefore(q->frontSent->next, val); } Link … List Link

next

backSent frontSent

next next next prev prev prev prev next prev

Adding to the LL Deque

slide-10
SLIDE 10

void addBackListDeque(struct ListDeque *q,TYPE val) { _addBefore(q->backSent, val); } void addFrontListDeque(struct ListDeque *q,TYPE val) { _addBefore(q->frontSent->next, val); } Link … List Link

next

backSent frontSent

next next next prev prev prev prev next prev

Adding to the LL Deque

slide-11
SLIDE 11

void removeFirstListDeque(struct ListDeque *q) { assert(!isEmptyListDeque(q)); _removeLink(q->frontSent->next); } void removeLastListDeque(struct ListDeque *q) { assert(!isEmptyListDeque(q)); _removeLink (q->backSent->prev); } Link … List Link

next

backSent frontSent

next next next prev prev prev prev next prev

Removing from the LL Deque

slide-12
SLIDE 12

Worksheet #19: _addBefore, _removeLink

Your Turn…

DynArrDeque best, ave, worst LLDeque 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)

slide-13
SLIDE 13

void _addBefore(struct DLink *link, TYPE val) { struct DLink *newLink = malloc(…); assert(newLink != 0); newLink->val = val; newLink->next = link; newLink->prev = link->prev; link->prev->next = newLink; link->prev = newLink; }

_addBefore

Link … List Link

next

backSent frontSent

next next next prev prev prev prev next prev struct Dlink { TYPE val; struct DLink *next; struct DLink *prev; }