Ch7. List and Iterator ADTs

Slides:



Advertisements
Similar presentations
© 2004 Goodrich, Tamassia Node-Lists1 6.2 Node Lists.
Advertisements

Lists1 © 2010 Goodrich, Tamassia. Position ADT The Position ADT models the notion of place within a data structure where a single object is stored It.
1 Array-based Implementation An array Q of maximum size N Need to keep track the front and rear of the queue: f: index of the front object r: index immediately.
CSC 212 – Data Structures Lecture 22: PositionList.
Iterators and Sequences1 © 2010 Goodrich, Tamassia.
© 2004 Goodrich, Tamassia Vectors1. © 2004 Goodrich, Tamassia Vectors2 The Vector ADT (“Vector” = “Array List” in §6.1) The Vector ADT extends the notion.
© 2004 Goodrich, Tamassia Sequences and Iterators1.
Queues 4/14/2017 5:24 PM 5.2 Queues Queues Dr Zeinab Eid.
Data Structures Lecture 4 Fang Yu Department of Management Information Systems National Chengchi University Fall 2010.
Lists and Iterators CSC311: Data Structures 1 Chapter 6 Lists and Iterators Objectives Array Lists and Vectors: ADT and Implementation Node Lists: ADT.
Lists and Iterators (Chapter 6) COMP53 Oct 22, 2007.
© 2004 Goodrich, Tamassia Vectors1 Lecture 03 Vectors, Lists and Sequences Topics Vectors Lists Sequences.
Doubly Linked Lists1 © 2014 Goodrich, Tamassia, Goldwasser Presentation for use with the textbook Data Structures and Algorithms in Java, 6 th edition,
CS 221 Analysis of Algorithms Data Structures Vectors, Lists.
CH 6. VECTORS, LISTS, AND SEQUENCES ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH,
Lecture5: Linked Lists Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
© 2004 Goodrich, Tamassia Vectors1 Vectors and Array Lists.
Array Lists1 © 2010 Goodrich, Tamassia. Array Lists2 The Array List ADT  The Array List ADT extends the notion of array by storing a sequence of arbitrary.
© 2004 Goodrich, Tamassia Lists1. © 2004 Goodrich, Tamassia Lists2 Position ADT (§ 5.2.2) The Position ADT models the notion of place within a data structure.
List Interface and Linked List Mrs. Furman March 25, 2010.
Welcome to CSCE 221 – Data Structures and Algorithms
CH 6 : VECTORS, LISTS AND SEQUENCES ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH,
CH 6 : VECTORS, LISTS AND SEQUENCES ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH,
Vectors, Lists, and Sequences. Vectors: Outline and Reading The Vector ADT (§6.1.1) Array-based implementation (§6.1.2)
Lists1 © 2010 Goodrich, Tamassia. Position ADT  The Position ADT models the notion of place within a data structure where a single object is stored 
CH 1-4 : INTRODUCTION ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA AND.
© 2004 Goodrich, Tamassia Queues. © 2004 Goodrich, Tamassia Stacks2 The Queue ADT The Queue ADT stores arbitrary objects Insertions and deletions follow.
Linked List, Stacks Queues
Hash Tables 1/28/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M.
CSE373: Data Structures & Algorithms Priority Queues
Lists and Iterators 5/3/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia,
Queues 5/11/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H.
Double-Ended Queues Chapter 5.
Vectors 5/31/2018 9:25 AM Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and.
Ch7. List and Iterator ADTs
Sequences 6/3/2018 9:11 AM Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia,
Doubly Linked Lists 6/3/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia,
Sequences and Iterators
Linked Lists Linked Lists 1 Sequences Sequences 07/25/16 10:31
COMP9024: Data Structures and Algorithms
© 2013 Goodrich, Tamassia, Goldwasser
Hash Tables 3/25/15 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M.
Lists and Sequences 9/21/2018 7:21 PM Sequences Sequences
LINKED LISTS CSCD Linked Lists.
Array Lists, Node Lists & Sequences
Lists and Iterators 3/9/15 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia,
Sequences 11/22/2018 3:25 AM Doubly-Linked Lists Doubly-Linked Lists.
Vectors 11/29/2018 6:45 PM Vectors 11/29/2018 6:45 PM Vectors.
" A list is only as strong as its weakest link. " - Donald Knuth
Array-Based Sequences
CH 9.2 : Hash Tables Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich, Tamassia and.
Doubly Linked Lists or Two-way Linked Lists
Lists and Sequences 12/8/2018 2:26 PM Sequences Sequences
CS212D: Data Structures Week 5-6 Linked List.
CS2013 Lecture 4 John Hurley Cal State LA.
Problem Understanding
CH 9.2 : Hash Tables Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich, Tamassia and.
Recall What is a Data Structure Very Fundamental Data Structures
Vectors, Lists and Sequences
Copyright © Aiman Hanna All rights reserved
Linked Lists & Iterators
Vectors, Lists, and Sequences
Vectors 4/26/2019 8:32 AM Vectors 4/26/2019 8:32 AM Vectors.
Lists CSE 373 Data Structures.
CS210- Lecture 6 Jun 13, 2005 Announcements
CS210- Lecture 7 Jun 14, 2005 Agenda Practice Session Vector
Vectors and Array Lists
Stacks and Linked Lists
Problem Understanding
Presentation transcript:

Ch7. List and Iterator ADTs Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in Java, Goodrich, Tamassia and Goldwasser (Wiley 2016)

List ADT

Example A sequence of List operations:

Array Lists An obvious choice for implementing the list ADT is to use an array, 𝐴, where 𝐴 𝑖 stores (a reference to) the element with index 𝑖. With a representation based on an array 𝐴, the get(𝑖) and set(𝑖, 𝑒) methods are easy to implement by accessing 𝐴[𝑖] (assuming 𝑖 is a legitimate index). 𝐴 1 2 n i 𝑁−1

Insertion In an operation add(𝑖, 𝑜), we need to make room for the new element by shifting forward the 𝑛−𝑖 elements 𝐴 𝑖 , …,𝐴 𝑛−1 In the worst case (𝑖=0), this takes 𝑂 𝑛 time A 1 2 n i A 1 2 n i A 1 2 n o i

Element Removal In an operation remove(𝑖), we need to fill the hole left by the removed element by shifting backward the 𝑛−𝑖−1 elements 𝐴 𝑖+1 ,…,𝐴 𝑛−1 In the worst case (𝑖=0), this takes 𝑂 𝑛 time A 1 2 n o i A 1 2 n i A 1 2 n r

Performance In an array-based implementation of a dynamic list: The space used by the data structure is 𝑂 𝑛 Indexing the element (get/set) at 𝑖 takes 𝑂 1 time add and remove run in 𝑂 𝑛 time In an add operation, when the array is full, instead of throwing an exception, we can replace the array with a larger one …

Exercise: Implement the Deque ADT with the List ADT Deque ADT: first(), last(), addFirst(e), addLast(e), removeFirst(), removeLast(), size(), isEmpty() List functions: get(i), set(i, e), add(i, e), remove(i), size(), isEmpty()

List Summary Array Fixed-Size or Expandable List Singly or Doubly Linked add(i, e), remove(i) 𝑂(1) Best Case (𝑖=𝑛) 𝑂(𝑛) Worst Case 𝑂(𝑛) Average Case ? get(i), set(i, e) 𝑂(1) size(), isEmpty()

Interview Question 1 Write code to partition a list around a value x, such that all nodes less than x come before all nodes greater than or equal to x. Gayle Laakmann McDowell, "Cracking the Code Interview: 150 Programming Questions and Solutions", 5th edition, CareerCup publishing, 2011.

Interview Question 2 Implement a function to check if a list is a palindrome. Gayle Laakmann McDowell, "Cracking the Code Interview: 150 Programming Questions and Solutions", 5th edition, CareerCup publishing, 2011.

Positional Lists To provide for a general abstraction of a sequence of elements with the ability to identify the location of an element, we define a positional list ADT. A position acts as a marker or token within the broader positional list. A position 𝑝 is unaffected by changes elsewhere in a list; the only way in which a position becomes invalid is if an explicit command is issued to delete it. A position instance is a simple object, supporting only the following method: p.getElement(): Return the element stored at position 𝑝.

Positional List ADT Accessor methods:

Positional List ADT, 2 Update methods:

Example A sequence of Positional List operations:

Positional List Implementation prev next The most natural way to implement a positional list is with a doubly-linked list. element node trailer header nodes/positions elements

Insertion, e.g., addafter(p, e) B C p q A B C X p q A B X C

Remove(p) A B C D p A B C p D A B C

Performance Assume doubly-linked list implementation of Positional List ADT The space used by a list with 𝑛 elements is 𝑂(𝑛) The space used by each position of the list is 𝑂(1) All the operations of the List ADT run in 𝑂(1) time

Positional List Summary List Singly-Linked List Doubly- Linked first(), last(), addFirst(), addLast(), addAfter() 𝑂(1) addBefore(p, e), erase() 𝑂(𝑛) Worst and Average case 𝑂(1) Best case size( ), isEmpty()

Interview Question 3 When Bob wants to send Alice a message M on the internet, he breaks 𝑀 into 𝑛 data packets, numbers the packets consecutively, and injects them into the network. When the packets arrive at Alice's computer, they may be out of order, so Alice must assemble the sequence of 𝑛 packets in order before she can be sure she has the entire message. Using Positional Lists describe and analyze an algorithm for Alice to do this. Can you do better with a regular List?