Linked Lists: Implementation of Queue & Deque

Slides:



Advertisements
Similar presentations
Queues Printer queues Several jobs submitted to printer Jobs form a queue Jobs processed in same order as they were received.
Advertisements

Queues and Linked Lists
1111 Abstract Data Types Cpt S 223. School of EECS, WSU.
CS Winter 2010 Linked Lists - Part 2 Deque with Double Links and Sentinel, Bag.
Queue & List Data Structures & Algorithm Abstract Data Types (ADTs) ADT is a mathematically specified entity that defines a set of its instances,
Stacks, Queues, and Deques. 2 A stack is a last in, first out (LIFO) data structure Items are removed from a stack in the reverse order from the way they.
1 Queues (5.2) CSE 2011 Winter May Announcements York Programming Contest Link also available from.
Chapter 5 Queues Modified. Chapter Scope Queue processing Comparing queue implementations 5 - 2Java Software Structures, 4th Edition, Lewis/Chase.
CS Fall 2009 Linked Lists - Part 2 Deque with Double Links and Sentinel, Bag.
Queue, Deque, and Priority Queue Implementations.
Queue, Deque, and Priority Queue Implementations Chapter 11 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Chapter 6.6, (event-driven simulation) Queues 1CSCI 3333 Data Structures.
Chapter 4 Linked Structures – Stacks Modified. Chapter Scope Object references as links Linked vs. array-based structures Managing linked lists Linked.
Stacks and Linked Lists. Abstract Data Types (ADTs) An ADT is an abstraction of a data structure that specifies – Data stored – Operations on the data.
1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,
CS261 – Data Structures Iterator ADT Dynamic Array and Linked List.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Linked Lists part 1 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University.
Queues. Like Stacks, Queues are a special type of List for storing collections of entities. Stacks are Lists where insertions (pushes) and deletions (pops)
Linked List Implementation of the Deque
“The desire for safety stands against every great and noble enterprise.” – Tacitus Thought for the Day.
Dynamic Array Queue and Deque
CS Fall 2009 Linked List Introduction. Characteristics of Linked Lists Elements are held in objects termed Links Links are 1-1 with elements, allocated.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 240 Abstract Data Types Queues Dale Roberts, Lecturer
Linked Structures - Stacks. Linked Structures An alternative to array-based implementations are linked structures A linked structure uses object references.
Review Array Array Elements Accessing array elements
Cpt S 122 – Data Structures Abstract Data Types
Week 4 - Monday CS221.
Linked List Introduction
QueueStack CS1020.
Lectures linked lists Chapter 6 of textbook
Queues Rem Collier Room A1.02
Linked List Stacks, Linked List Queues, Dequeues
Doubly Linked List Review - We are writing this code
Data Structures and Algorithms
Queues Queues Queues.
Stack and Queue APURBO DATTA.
Linked lists Motivation: we can make arrays, but their functionality is slightly limited and can be difficult to work with Biggest issue: size management.
Notes on Assignment 1 Your code will have several classes, most notably the class that represents the entire list data structure, and the class.
Wednesday Questions How do I debug? We love to teach this at IPL/OH!
Abstract Data Types (ADTs)
Queue, Deque, and Priority Queue Implementations
Linked node problem 3 What set of statements turns this picture:
Queue, Deque, and Priority Queue Implementations
Queues, Deques and Priority Queues
Circularly Linked Lists
Chapter 18: Linked Lists.
List Data Structure.
Queue and Priority Queue Implementations
Chapter 14: Queue and Priority Queue Implementations
Recitation 5 CS0445 Data Structures
Doubly Linked List Implementation
A List Implementation That Links Data
Data Structures and Algorithms
Queues CSC212.
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
Using a Queue Chapter 8 introduces the queue data type.
Using a Queue Chapter 8 introduces the queue data type.
CS210- Lecture 6 Jun 13, 2005 Announcements
Dynamic Array: Implementation of Queue & Deque
Queues cont. Chapter 8 © 2011 Pearson Addison-Wesley. All rights reserved.
Queues: Implemented using Linked Lists
Stacks, Queues, and Deques
Wednesday Questions How do I debug? We love to teach this at IPL/OH!
Doubly Linked List Implementation
A List Implementation That Links Data
Stacks and Linked Lists
A List Implementation that Links Data
Queue, Deque, and Priority Queue Implementations
Presentation transcript:

Linked Lists: Implementation of Queue & Deque CS 261 – Data Structures Linked Lists: Implementation of Queue & Deque

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?

Link List Queue: Illustration tail List head link link … link

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

SEE CODE!!!

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; }

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; }

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

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.

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

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

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

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

Your Chance – worksheet time!! Write _addLink and _removeLink in a moment.