CS261 Data Structures. Iterator ADT Dynamic Array and Linked List

Similar documents
CS261 Data Structures. Iterator ADT Dynamic Array and Linked List

Big- (O): c.) binary search O(logn)

CS 261. List Bag List Queue List Deque. by Tim Budd Ron Metoyer Sinisa Todorovic

Lecture 8: Iterators and More Mutation

CS261 Data Structures. Ordered Array Dynamic Array Implementation

CS261 Data Structures. Ordered Bag Dynamic Array Implementa;on

I. Methode. File Bag.h. #ifndef _BAG_H #define _BAG_H typedef int TElem; typedef struct _Iterator Iterator; //forward declaration for the iterator

CS 261: Data Structures. Dynamic Arrays. Introduction

CMSC 341. Linked Lists, Stacks and Queues. CMSC 341 Lists, Stacks &Queues 1

Lesson 8: Linked List Deque

The combination of pointers, structs, and dynamic memory allocation allow for creation of data structures

Basic Data Structures

CMSC 341 Lecture 7 Lists

The fringe of a binary tree are the values in left-to-right order. For example, the fringe of the following tree:

What is an Iterator? An iterator is an abstract data type that allows us to iterate through the elements of a collection one by one

An Introduction to Data Structures

CSE-505: Programming Languages. Lecture 18 Existential Types. Zach Tatlock 2016

Linear Structures. Linear Structure. Implementations. Array details. List details. Operations 4/18/2013

Linear Structures. Linear Structure. Implementations. Array details. List details. Operations 2/10/2013

lecture14: Tree Traversals

Lecture 10 Notes Linked Lists

Worksheet 28: Skip Lists

Programming Language Concepts: Lecture 6

Dot and Scope Resolution Operator

CS 261. Dynamic Arrays Introduction by Tim Budd Ron Metoyer Sinisa Todorovic

Lists (Section 5) Lists, linked lists Implementation of lists in C Other list structures List implementation of stacks, queues, priority queues

CMPT 125: Practice Midterm Answer Key

Lecture 10 Notes Linked Lists

Lists and Iterators. CSSE 221 Fundamentals of Software Development Honors Rose-Hulman Institute of Technology

Basic Data Structures 1 / 24

CS 261: Data Structures. Dynamic Array Queue

Design to interfaces. Favor composition over inheritance Find what varies and encapsulate it

List Iterator Implementation

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find

Notes on access restrictions

Stacks. Common data structures - useful for organizing data for specific tasks Lists Stacks - an Abstract Data Type

TEMPLATES AND ITERATORS

ITEC2620 Introduction to Data Structures

Pointers, Arrays and Parameters

Lab. Lecture 26: Concurrency & Responsiveness. Assignment. Maze Program

Dynamic Data Structures and Generics

Computer Science 302 Spring 2007 Practice Final Examination: Part I

ITI Introduction to Computing II

Lecture 9: Lists. MIT-AITI Kenya 2005

COMP 161 Lecture Notes 16 Analyzing Search and Sort

Abstract Data Types. CS 234, Fall Types, Data Types Abstraction Abstract Data Types Preconditions, Postconditions ADT Examples

EECS 211 Lab 6. Classes and Abstractation Winter Getting the code. Encapsulation and Abstraction. Classes

Lecture No.04. Data Structures

lecture09: Linked Lists

CS : Data Structures Michael Schatz. Sept Lecture 5: Iterators

CS 367: Introduction to Data Structures Midterm Sample Questions

What is an algorithm?

+ Abstract Data Types

Introduction to pthreads

ECE 2400 Computer Systems Programming Fall 2018 Topic 7: Concrete Data Types

Java Foundations. 7-2 Instantiating Objects. Copyright 2015, Oracle and/or its affiliates. All rights reserved.

An Activation Record for Simple Subprograms. Activation Record for a Language with Stack-Dynamic Local Variables

APT Session 6: Compilers

11-1. Collections. CSE 143 Java. Java 2 Collection Interfaces. Goals for Next Several Lectures

COLLECTIONS & ITERATORS cs2420 Introduction to Algorithms and Data Structures Spring 2015

CS21: INTRODUCTION TO COMPUTER SCIENCE. Prof. Mathieson Fall 2017 Swarthmore College

Data Structures and Algorithms for Engineers

CS 2113 Software Engineering

package weiss.util; // Fig , pg // Fig 6.16,6.17, pg package weiss.util;

Implementing Dynamic Data Structures

CS18000: Programming I

ITI Introduction to Computing II

CS 261. Dynamic Array Queue by Tim Budd Ron Metoyer Sinisa Todorovic

COL106: Data Structures and Algorithms. Ragesh Jaiswal, IIT Delhi

Data Structures and Algorithms

CS 261 Data Structures. Priority Queue ADT & Heaps

Abstraction עזאם מרעי המחלקה למדעי המחשב אוניברסיטת בן-גוריון

CS S-05 Abstract Data Types and Lists 1

ITI Introduction to Computing II

//instance variables //methods. Foo x = new Foo(); Interface: also a type of objects public interface Bar {

4.1 Ordered Lists Revisited

CS261 Data Structures. Maps (or Dic4onaries)

From Bing.com on Nov

Last time s big ideas

APT Session 7: Compilers

Recursive Objects. Singly Linked List (Part 2)

Wednesday, November 5

INTERFACES IN JAVA. Prof. Chris Jermaine

Solution for Data Structure

Project Compiler. CS031 TA Help Session November 28, 2011

CS24 Week 4 Lecture 2

Heap Arrays. Steven R. Bagley

CSC148H Week 3. Sadia Sharmin. May 24, /20

Lecture 5: Implementing Lists, Version 1

CSE 143 Lecture 26. Advanced collection classes. (ADTs; abstract classes; inner classes; generics; iterators) read 11.1, 9.6, ,

Computer Science 210 Data Structures Siena College Fall Topic Notes: Trees

Adam Blank Lecture 5 Winter 2015 CSE 143. Computer Programming II

Java Collections. Readings and References. Collections Framework. Java 2 Collections. CSE 403, Spring 2004 Software Engineering

Cosc 241 Programming and Problem Solving Lecture 9 (26/3/18) Collections and ADTs

Linked Lists. College of Computing & Information Technology King Abdulaziz University. CPCS-204 Data Structures I

Graphs: Graph Data Structure:

CS313D: ADVANCED PROGRAMMING LANGUAGE

CISC-124. Casting. // this would fail because we can t assign a double value to an int // variable

Computer Science E-119 Fall Problem Set 4. Due prior to lecture on Wednesday, November 28

Data Structures & Algorithms

Transcription:

CS261 Data Structures Iterator ADT Dynamic Array and Linked List

Goals Why do we need iterators? Iterator ADT Linked List and Dynamic Array Iterators The lecture will focus on link list iterators

Interface of Bag ADT void initbag (struct Bag *da, int cap); void freebag (struct Bag *da); void addbag (struct Bag *da, TYPE val); int containsbag (struct Bag *da, TYPE val); void removebag (struct Bag *da, TYPE val); int sizebag (struct Bag *da); What if the user wants to loop through the bag to print out of filter contents?

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->; while(l!=list->backsentinel){ do something l=l->; 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 the public abstractions: push, pop, contains, remove, etc.

How do we abstract away loops for(i = 0; i < sizedynarr; i++) val = getdynarr(i); while( cur!= backsent) do something cur = cur -> do { something cur = cur->; while (cur!= backsent);

Iterator ADT Solution: define an interface that provides methods for writing loops int TYPE hasnextiter(struct Iter *itr); Iter(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 = ListIter(itr); if (cur...) removelistiter(itr);

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->currentlink = lst->frontsentinel; return itr;

After Initialization list Itr current List frontsent backsent Link Link front

Strategy HasNext Returns T (F) to the user if there is (is not) another value remaining to be enumerated Next Return the value to be enumerated and updates current to point to that value (assumes HasNext was called and returned T) Remove Removes the value (and link) that was last returned by call to Next() NOTE: Some designs will use HasNext to update the current and Next just returns current.

After Initialization list Itr while (hasnextlistiter(itr)) { curval = ListIter(itr); current List frontsent backsent Link Link front

After One Iteration:hasNext, list Itr while (hasnextlistiter(itr)) { curval = ListIter(itr); current List frontsent 5 backsent 10 15 front

After Two Iterations list Itr while (hasnextlistiter(itr)) { curval = ListIter(itr); current List frontsent 5 backsent 10 15 front

After Three Iterations list Itr current while (hasnextlistiter(itr)) { curval = ListIter(itr); We ve enumerated all elements Subsequent calls to HasNext will evaluate to false List frontsent 5 backsent 10 15 front

After Two Iterations then Remove Remove the last value enumerated (10) Where should current be after the removal? list Itr current while (hasnextlistiter(itr)) { curval = ListIter(itr); if(curval == 10) removelistiter(itr); List frontsent 5 backsent 10 15 front

After Two Iterations then Remove list Itr Remove the last value enumerated (10) Where should current be after the removal? current List frontsent 5 backsent 15 front

Your Turn Worksheet#24 Linked List Iterator

Is there another element? // Return T (F) if there is (is not) a element int hasnextlistiter (struct linkedlistiterator *itr) { return itr->currentlink->!= itr->lst->backsentinel; struct linkedlistiterator { struct linkdlist *lst; struct DLink *currentlink;

Return and update the element /* Returns element and updates that element to be the current element */ TYPE ListIter (struct LinkedlistIterator *itr) { itr->currentlink = itr->currentlink->; return itr->currentlink->value; struct linkedlistiterator { struct linkdlist *lst; struct DLink *currentlink;

Remove an Element (the most recent one returned by ) Assume you have following function: // removes lnk from the list it is part of void _removelink (struct DLink *lnk) void removeiter (struct listiter *itr) { itr->currentlink = itr->currentlink->; _removelink(itr->currentlink->); struct linkedlistiterator { struct linkdlist *lst; struct DLink *currentlink;

Remove Link void _removelink (struct dlink *lnk) { db backsentinel frontsentinel Link lnk Link

Worksheet 24 void _removelink (struct DLink *lst) { lnk->-> = lnk->; db backsentinel frontsentinel Link lnk Link

Worksheet 24 void _removelink (struct DLink *lst) { lnk->-> = lnk->; lnk->-> = lnk->; db backsentinel frontsentinel Link lnk Link

Worksheet 24 void _removelink (struct linkedlist *lst) { lnk->-> = lnk->; lnk->-> = lnk->; free(lnk); db backsentinel frontsentinel Link Link