Download presentation
Presentation is loading. Please wait.
Published byΣπύρος Αγγελίδης Modified over 6 years ago
1
Linked Lists: Implementation of Queue & Deque
CS 261 – Data Structures Linked Lists: Implementation of Queue & Deque
2
ADT: Queue Interface Specifies a FIFO (First-In, First-Out) interface
int isEmpty(); void addBack(TYPE val); // Add value at end of queue. TYPE front(); // Get value at front of queue. void removeFront(); // Remove value at front. Specifies a FIFO (First-In, First-Out) interface Conceptually similar to a line (queue) of waiting people: A person joins the queue by adding themselves at the end The next person is removed from the front of the queue Difference between addBack and push?
3
Link List Queue: Illustration
tail List head link link … link
4
Modification #1 : The Sentinel
A sentinel is a special marker at the front and/or back of the list Has no value and never removed Helps remove special cases due to null references since it’s never null An empty list always has a sentinel tail List tail head List head Link … Link next tail next next next List Sentinel head s
5
SEE CODE!!!
6
Comparison: listQueue with/without Sentinel
void initListQueue (struct listQueue *q) { struct Link *lnk = (struct Link *) malloc(sizeof(struct Link)); assert(lnk != 0); /*lnk is the sentinel*/ lnk->next = 0; q->tail = q->head = lnk; } /* No Sentinel */ void initListQueue(struct listQueue *q) { q->tail = q->head = 0; }
7
Comparison: listQueue with/without Sentinel
void addBackListQueue (struct listQueue *q, TYPE e) { struct Link * lnk = ... assert(lnk != 0); lnk->next = 0; lnk->value = e; /* we know it has a tail */ q->tail->next = lnk; q->tail = lnk; } /* No Sentinel */ void addBackListQueue (struct listQueue *q, TYPE e) { struct Link * lnk = ... assert(lnk != 0); lnk->next = 0; lnk->value = e; /* tail may be null!! */ if(!isEmptyListQueue(q)){ q->tail->next = lnk; q->tail = lnk; }else q->tail = q->head = lnk; }
8
Deque ADT? What if we want to add and remove elements from both front and back? Need to use links going both forward and backwards Makes adding a new link harder, as must maintain both forward and backward links. tail List head link link link tail head val val … val links links links
9
ADT: Deque Interface 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.
10
Variation #2: Double Links
Point forward to the next element Point backwards to the previous element struct DLink { TYPE val; struct DLink *next; /* Link to previous node. */ struct DLink *prev; /* Link to next node. */ }; tail List head prev prev prev prev Link Link … Link next next next next
11
Deque & Sentinels ? Why would you use a sentinel?
Consider a deque, with two sentinels: Pointer to front sentinel: head Pointer to back sentinel: tail 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) tail List head prev prev prev prev prev Link … Link next next next next next Sentinel
12
Adding to Deque: Implementation
void addBackListDeque(struct ListDeque *q, TYPE val) { _addBefore(q->tail, val); } void addFrontListDeque(struct ListDeque *q, _addBefore(q->head->next, val); tail List head prev prev prev prev prev Link … Link next next next next next Sentinel
13
Removing from Deque: Also Generalized
void removeFirstListDeque(struct ListDeque *q) { assert(!isEmptyListDeque(q)); _removeLink(q->head->next); } void removeLastListDeque(struct ListDeque *q) { _removeLink (q->tail->prev); tail List head prev prev prev prev prev Link … Link next next next next next Sentinel
14
Your Chance – worksheet time!!
Write _addLink and _removeLink in a moment.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.