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

cs261 data structures
SMART_READER_LITE
LIVE PREVIEW

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

CS261 Data Structures Linked List Implementation of the Queue Review: Linked List Stack Time complexity of ListStack operations: Push: O(1) always List Pop: O(1) always Top: O(1) always link link link How would this compare


slide-1
SLIDE 1

CS261 Data Structures

Linked List Implementation

  • f the Queue
slide-2
SLIDE 2

Time complexity of ListStack operations:

– Push: O(1) always – Pop: O(1) always – Top: O(1) always

How would this compare to a DynArr (a dynamic array

implementation of a stack)?

– Push: O( 1+ ) average, O(n) worse, O(1) best – Pop : O(1) always – Top : O(1) always – In practice, dynamic array is slightly faster in real timings

Review: Linked List Stack

link link … List link

slide-3
SLIDE 3

Linked List Queue

  • Could we use our linked list as is, to

implement a queue?

link link … List link firstLink (or head)

slide-4
SLIDE 4

link link … List link firstLink (or head) lastLink (or tail)

Which side should we make the ‘front’ of the queue?

Modification#1: Tail Pointer

link

Back Front

slide-5
SLIDE 5
  • A sentinel is a special marker at the front and/or back of the list
  • Has no value and never removed
  • Helps us avoid special cases in the code associated with null

references since it’s never null (e.g. first/last never point to null)

  • Simplifies some operations
  • An empty list always has a sentinel

List firstLink lastLink s Link … List Link

next

lastLink firstLink

next next next

Sentinel

Modification#2: Sentinel

slide-6
SLIDE 6

struct listQueue { struct Link *firstLink;/* Always pts to Sent */ struct Link *lastLink; }

listQueue struct

Link … List Link

next

lastLink firstLink

next next next

After additions

slide-7
SLIDE 7

ListQueueInit

void listQueueInit (struct listQueue *q) { struct link *lnk = malloc(sizeof(struct link)); assert(lnk != 0); /* lnk is the sentinel */ lnk->next = 0; q->firstLink = q->lastLink = lnk; } List firstLink lastLink s Initially

next

slide-8
SLIDE 8

/* No Sentinel */ void addBackListQueue (struct listQueue *q, TYPE e) { struct Link * lnk = ... assert(lnk != 0); lnk->next = 0; lnk->value = e; /* lastLink may be null!! */ if(!isEmptyListQueue(q)){ q->lastLink->next = lnk; q->lastLink = lnk; }else q->firstLink = q->lastLink = lnk; }

Sentinel vs. No Sentinel

List firstLink lastLink Empty Case? Link … List Link

next

lastLink firstLink

next next next

slide-9
SLIDE 9

addBackListQueue (Enqueue)

/* Sentinel */ void addBackListQueue (struct listQueue *q, TYPE e) { struct Link * lnk = malloc(….) assert(lnk != 0); lnk->next = 0; lnk->value = e;

/* we know it has a lastLink. */

q->lastLink->next = lnk; q->lastLink = lnk; } List firstLink lastLink s Empty Case?

next

slide-10
SLIDE 10
  • Worksheet #18

– Linked List Queue Implementation

Your Turn