CS261 – Data Structures Iterator ADT Dynamic Array and Linked List.

Slides:



Advertisements
Similar presentations
Chapter 23 Organizing list implementations. This chapter discusses n The notion of an iterator. n The standard Java library interface Collection, and.
Advertisements

Transparency No. 1 Java Collection API : Built-in Data Structures for Java.
1 Linked lists Sections 3.2, 3.3, 3.5 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors.
1111 Abstract Data Types Cpt S 223. School of EECS, WSU.
Chapter 24 Lists, Stacks, and Queues
Lists and the Collection Interface Chapter 4. Chapter Objectives To become familiar with the List interface To understand how to write an array-based.
Lists: An internal look
Copyright © 2013 by John Wiley & Sons. All rights reserved. HOW TO CREATE LINKED LISTS FROM SCRATCH CHAPTER Slides by Rick Giles 16 Only Linked List Part.
Processing Data in Collections Chapter Object Wrappers Collections can only hold objects. Primitive types ( int, double, float, etc.) are not objects.
Data Structures A data structure is a collection of data organized in some fashion that permits access to individual elements stored in the structure This.
CS18000: Problem Solving and Object-Oriented Programming.
Programming Paradigms Introduction. 6/15/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L1:
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++)
1 Abstract Data Types. Objectives To appreciate the concept and purpose of abstract data types, or ADTs To understand both the abstract behavior and the.
CSC 212 – Data Structures Lecture 22: PositionList.
Abstract Data Types Data abstraction, or abstract data types, is a programming methodology where one defines not only the data structure to be used, but.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 10 Using arrays to create collections.
1 Chapter 24 Lists Stacks and Queues. 2 Objectives F To design list with interface and abstract class (§24.2). F To design and implement a dynamic list.
CSE 143 Lecture 22: Advanced List Implementation (ADTs; interfaces; abstract classes; inner classes; generics; iterators)
1 Problem Solving Abstraction Oftentimes, different real-world problems can be modeled using the same underlying idea Examples: Runtime storage, Undo operation.
1 Lecture 26 Abstract Data Types –III Overview  Creating and manipulating Linked List.  Linked List Traversal.  Linked List Traversal using Iterator.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 10 Using arrays to create collections.
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 17: Linked Lists.
CS Winter 2011 Abstract Data Types. Container Classes Over the years, programmers have identified a small number of different ways of organizing.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 10 Using arrays to create collections.
Basic Concepts Chapter 1 Objectives
1 Collection, Iterable, and Iterator Interfaces The Collection Interface and its Hierarchy The Iterable and Iterator Interfaces For-each Loops with Iterable.
Iterator COMP 401, Spring 2013 Lecture 07 1/31/2013.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Chapter 3 Introduction to Collections – Stacks Modified
Linked Lists. RHS – SOC 2 Linked lists We can already store collec- tions of objects in arrays and array lists – why would we need other data structures…?
CS261 Data Structures Dynamic Arrays Part 2: Implementation.
Chapter 9: The Iterator Pattern
30 May Stacks (5.1) CSE 2011 Winter Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An.
1 Chapter 7 The Linked List as a Data Structure. 2 The List ADT A list is a list of elements. The list of elements consist of the data acted upon by list.
CSE 143 Lecture 24 Advanced collection classes (ADTs; abstract classes; inner classes; generics; iterators) read 11.1, 9.6, , slides.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
CS261 Data Structures Maps (or Dictionaries). Goals Introduce the Map(or Dictionary) ADT Introduce an implementation of the map with a Dynamic Array.
CSS446 Spring 2014 Nan Wang.  To understand the implementation of linked lists and array lists  To analyze the efficiency of fundamental operations.
Ordered Linked Lists using Abstract Data Types (ADT) in Java Presented by: Andrew Aken.
CSC 212 Sequences & Iterators. Announcements Midterm in one week  Will cover through chapter 5 of book  Midterm will be open book, open note (but, closed.
CS 367 Introduction to Data Structures Lecture 5.
Linked List Implementation of the Deque
Week 2 - Friday.  What did we talk about last time?  Computing Big Oh.
List Interface and Linked List Mrs. Furman March 25, 2010.
Iterators ITI 1121 N. El Kadri. Motivation Given a (singly) linked-list implementation of the interface List, defined as follows, public interface List.
Chapter 5 Array-Based Structures © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
CS261 Data Structures Linked Lists - Introduction.
Chapter 17: Linked Lists. Objectives In this chapter, you will: – Learn about linked lists – Learn the basic properties of linked lists – Explore insertion.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 18 List ADT Animated Version.
CSE 501N Fall ‘09 10: Introduction to Collections and Linked Lists 29 September 2009 Nick Leidenfrost.
CS Fall 2009 Linked List Introduction. Characteristics of Linked Lists Elements are held in objects termed Links Links are 1-1 with elements, allocated.
Linked List Introduction
G64ADS Advanced Data Structures
Lectures linked lists Chapter 6 of textbook
Introduction to Data Structures
Abstract Data Types (ADTs)
Stack ADT & Modularity 2 implementations of the Stack abstract data type: Array Linked List Program design: modularity, abstraction and information hiding.
Linked Lists: Implementation of Queue & Deque
CSE 143 Lecture 27: Advanced List Implementation
Lecture 26: Advanced List Implementation
[Chapter 4; Chapter 6, pp ] CSC 143 Linked Lists [Chapter 4; Chapter 6, pp ]
"First things first, but not necessarily in that order." -Dr. Who
Dynamic Data Structures and Generics
"First things first, but not necessarily in that order " -Dr. Who
Introduction to Data Structure
Software Design Lecture : 39.
Dynamic Array: Implementation of Stack
Abstract Data Types (ADTs)
Presentation transcript:

CS261 – Data Structures Iterator ADT Dynamic Array and Linked List

Goals Why do we need iterators? Iterator ADT Linked List and Dynamic Array Iterators

Iterator Concept Problem: How do you provide a user of a container access to the elements, without exposing the inner structure? Think of two developers: one writing the container (that’s you!!!), the other using the container (that’s someone using your library to build an application where they need, for example, a stack implementation)

Traversing a Container – as Developer For example, within the Linked List container you (the developer) wrote a loop such as the following: struct LinkedList *list; struct Link *l;... /* Initialize list. */ l = list->frontSentinel->next; while(l!=list->backSentinel){ …do something… l=l->next; This is fine within the container library code itself, but we don ’ t want users of the container library to have to know about links…or worse yet, be able to manipulate links!

Encapsulation Chapter 5: Hide the implementation details behind a simple and easy to remember interface (ie. abstraction mechanism) Users should not know about links, arrays, size, capacity, etc. Users should know and use: push, pop, contains, remove, etc.

Iterators to the Rescue So, how do we allow them to loop through the data without manipulating links? Provide a “facilitator object” Maintain encapsulation – Hide the details away from the user. Allow them to work at an abstract level. Also helps with code maintenance: a loop is essentially the same for a user whether using a LinkedList or DynamicArray

Iterator ADT Solution: define an interface that provides methods for writing loops void createIter(container *l); int hasNextIter(struct Iter *itr); TYPE nextIter(struct Iter *itr); void removeIter(struct Iter *itr); void changeIter(struct Iter *itr, TYPE val); void addIter(struct Iter *itr, TYPE val);

Iterator: Typical Usage TYPE cur; /* current collection val */ struct linkedList *list; linkedListIterator *itr; list = createLinkedList(…) itr = createLinkedListIter(list) while (hasNextListIter(itr)) { cur = nextListIter(itr); if (cur...) removeListIter(itr); }

Simplifying Assumptions & Properties of the Iterator ADT Function next and hasNext must be interleaved Call remove after next Cannot call remove twice in a row without a calling hasNext

LinkedListIterator struct linkedListIterator { struct linkedList *lst; struct dlink *currentLink; } struct linkedListIterator *createLinkedListIterator ( struct linkedList *lst) { struct linkedListIterator *itr; itr = malloc(sizeof(struct linkedListIterator)); itr->lst = lst; itr->current = lst->frontSentinel; }

After Initialization Link … List Link next list frontSent next prev front next prev Itr backSent current

Strategy HasNext – Returns T (F) to the user if there is (is not) another value remaining to be enumerated – Updates current Next – Return the next value to be enumerated Remove – Removes the value (and link) that was last returned by call to Next()

After Initialization Link … List Link next list frontSent next prev front next prev Itr backSent current

After One Iteration:hasNext, next 5 List 15 next list frontSent next prev front next prev Itr backSent current 10

After Two Iterations 5 List 15 next list frontSent next prev front next prev Itr backSent current 10

After Three Iterations 5 List 15 next list frontSent next prev front next prev Itr backSent current 10 We’ve enumerated all elements Subsequent calls to HasNext will evaluate to false

After Two Iterations – then Remove 5 List 15 next list frontSent next prev front next prev Itr backSent current 10 Remove the last value enumerated (10) Where should current be after the removal?

After Two Iterations – then Remove 5 List 15 next list frontSent next prev front next prev Itr backSent current Remove the last value enumerated (10) Where should current be after the removal?

Your Turn Worksheet#24 Linked List Iterator