Linked List Implementation of the Deque

Slides:



Advertisements
Similar presentations
Linked Lists.
Advertisements

Data Structures ADT List
CS Winter 2010 Linked Lists - Part 2 Deque with Double Links and Sentinel, Bag.
418115: II. Linked List A linked list can be thought of a chain of linked list elements. A linked list element contains a single data item, and contains.
Double-Linked Lists and Circular Lists
Linked List Variations
David Weinberg presents Linked Lists: The Background  Linked Lists are similar to ArrayLists in their appearance and method of manipulation  They do.
Chapter 6 The Collections API. Simple Container/ Iterator Simple Container Shape [] v = new Shape[10]; Simple Iterator For( int i=0 ; i< v.length ; i++)
Queue Definition Ordered list with property: –All insertions take place at one end (tail) –All deletions take place at other end (head) Queue: Q = (a 0,
Queues. Queue Definition Ordered list with property: All insertions take place at one end (tail) All insertions take place at one end (tail) All deletions.
Chapter 17 Linked List Saurav Karmakar Spring 2007.
Doubly Linked Lists. One powerful variation of a linked list is the doubly linked list. The doubly linked list structure is one in which each node has.
CSC 212 – Data Structures. Using Stack Stack Limitations  Great for Pez dispensers, JVMs,& methods  All of these use most recent item added only 
CS 307 Fundamentals of Computer Science 1 Linked Lists many slides taken from Mike Scott, UT Austin.
CS Fall 2009 Linked Lists - Part 2 Deque with Double Links and Sentinel, Bag.
Linked Lists. Example We would like to keep a list of inventory records – but only as many as we need An array is a fixed size Instead – use a linked.
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.
Complex Structures Nested Structures Self referential structures A structure may have Data variables Internal structures/unions Pointer links Function.
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.
CSE 131 Computer Science 1 Module 9: Linked Lists Using references to link objects Basic operations on linked lists Implementing a linked list of integers.
CSE 12 – Basic Data Structures Cynthia Bailey Lee Some slides and figures adapted from Paul Kube’s CSE 12 CS2 in Java Peer Instruction Materials by Cynthia.
CS 307 Fundamentals of Computer ScienceLinked Lists 1 Topic 14 Linked Lists "All the kids who did great in high school writing pong games in BASIC for.
Today’s Agenda  Linked Lists  Double ended Linked Lists  Doubly Linked Lists CS2336: Computer Science II.
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.
Generic lists Vassilis Athitsos. Problems With Textbook Interface? Suppose that we fix the first problem, and we can have multiple stacks. Can we have.
Chapter 5 Linked Lists II
1 Today’s Material List ADT –Definition List ADT Implementation: LinkedList.
Recursive Objects (Part 2) 1. Adding to the front of the list  adding to the front of the list  t.addFirst('z') or t.add(0, 'z') 2 'a' 'x' LinkedList.
CS 201 Data Structures and Algorithms Chapter 3: Lists, Stacks, and Queues - I Text: Read Weiss, §3.1 – 3.5 1Izmir University of Economics.
CS261 Data Structures Linked Lists - Introduction.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 18 List ADT Animated Version.
Dynamic Array Queue and Deque
CS-2851 Dr. Mark L. Hornick 1 Linked-List collections Structure and Implementation.
CS Fall 2009 Linked List Introduction. Characteristics of Linked Lists Elements are held in objects termed Links Links are 1-1 with elements, allocated.
Lists List Implementations. 2 Linked List Review Recall from CMSC 201 –“A linked list is a linear collection of self- referential structures, called nodes,
Linked Lists Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin.
 2015, Marcus Biel, Linked List Data Structure Marcus Biel, Software Craftsman
Week 4 - Monday CS221.
Linked List Introduction
QueueStack CS1020.
Lectures linked lists Chapter 6 of textbook
UNIT – I Linked Lists.
Linked List Stacks, Linked List Queues, Dequeues
Doubly Linked List Review - We are writing this code
Data Structures and Algorithms
Linked Lists with Tail Pointers
Notes on Assignment 1 Your code will have several classes, most notably the class that represents the entire list data structure, and the class.
Doubly linked lists.
Topic 16 Queues Adapted from Mike Scott’s materials.
CSC215 Homework Homework 11 Due date: Dec 19, 2016.
Linked node problem 3 What set of statements turns this picture:
Circularly Linked Lists
Linked Lists: Implementation of Queue & Deque
Recitation 5 CS0445 Data Structures
Doubly Linked Lists or Two-way Linked Lists
Doubly Linked Lists Lecture 21 Tue, Mar 21, 2006.
A List Implementation That Links Data
Data Structures and Algorithms
Header and Trailer Sentinels
Dynamic Array: Implementation of Queue & Deque
A List Implementation That Links Data
Stacks and Linked Lists
Data Structures & Programming
A List Implementation that Links Data
Queue, Deque, and Priority Queue Implementations
Presentation transcript:

Linked List Implementation of the Deque 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. 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.

Linked List Deque What if we want to add and remove elements from both front and back? tail List head link link link Right side is the ‘back’ of our queue ….remember why! Try to remove from the back…what was the problem? How can we fix that? DOUBLE LINKS! val links … head tail

Modification #3: Double Links prev 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 next lastLink List firstLink prev prev prev prev Link Link … Link next next next next

linkedListDeque Struct struct linkedList { int size; struct dlink * frontSentinel; struct dlink * backSentinel; }; Why make front/back pointers? Makes the notation more consistent in that they’ll be treated like the others…ie. lnk->next and won’t need to check for special case and do lnk.next should write the code by hand! – do the init?

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; } backSent List frontSent prev next Sentinel Sentinel

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) We ALWAYS have a front and back. A B A X B

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) We ALWAYS have a front and back. backSent List frontSent prev prev prev prev Link … Link next next next next

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

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

Your Turn… Worksheet #19: _addBefore, _removeLink DynArrDeque best, ave, worst LLDeque addLast O(1),O(1+), O(N) O(1),O(1),O(1) removeLast O(1), O(1),O(1) addFirst removeFirst