Iterators in Data Structures and Algorithms - Lecture Slides | COP 3530, Study notes of Data Structures and Algorithms

Material Type: Notes; Class: DATA STRUC/ALGORITHMS; Subject: COMPUTER PROGRAMMING; University: University of Florida; Term: Unknown 1989;

Typology: Study notes

Pre 2010

Uploaded on 09/17/2009

koofers-user-wo0-1
koofers-user-wo0-1 🇺🇸

10 documents

1 / 14

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Iterators
An iterator permits you to examine
the elements of a data structure one
at a time.
Iterator Methods
Iterator ix = x.iterator();
constructs and initializes an iterator to
examine the elements of x;
constructed iterator is assigned to ix
you must define the method iterator
in the class for x
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe

Partial preview of the text

Download Iterators in Data Structures and Algorithms - Lecture Slides | COP 3530 and more Study notes Data Structures and Algorithms in PDF only on Docsity!

Iterators

An iterator permits you to examine

the elements of a data structure one

at a time.

Iterator Methods

Iterator ix = x.iterator();

constructs and initializes an iterator to

examine the elements of x;

constructed iterator is assigned to ix

you must define the method iterator

in the class for x

Iterator Methods

ix.hasNext()

returns true iff x has a next element

ix.next()

throws NoSuchElementException if

there is no next element

returns next element otherwise

Optional Iterator Method

ix.remove()

removes last element returned by

ix.next()

throws UnsupportedMethodException if

method not implemented

throws IllegalStateException if ix.next()

not yet called or did not return an

element

Java’s Array Linear List Class

java.util.ArrayList

Cadillac version of our

ArrayLinearListWithIterator

Linked Representation

• list elements are stored, in memory,

in an arbitrary order

• explicit information (called a link)

is used to go from one element to

the next

Memory Layout

a b c d e

c a e d b

A linked representation uses an arbitrary layout.

Layout of L = (a,b,c,d,e) using an array representation.

Linked Representation

pointer (or link) in e is null

c a e d b

use a variable firstNode to get to the

first element a

firstNode

Node Representation

package dataStructures;

class ChainNode { // package visible data members Object element; ChainNode next;

// constructors come here }

next element

Constructors Of ChainNode

ChainNode() {}

null null

null element

next element

ChainNode(Object element) {this.element = element;}

ChainNode(Object element, ChainNode next) {this.element = element; this.next = next;}

get(0)

checkIndex(0); desiredNode = firstNode; // gets you to first node return desiredNode.element;

a b c d e

null

firstNode

get(1)

checkIndex(1); desiredNode = firstNode.next; // gets you to second node return desiredNode.element;

a b c d e

null

firstNode

NullPointerException

desiredNode = firstNode.next.next.next.next.next.next; // gets the computer mad // you get a NullPointerException

a b c d e

null

firstNode

Remove An Element

remove(0)

a b c d e

null

firstNode

firstNode = firstNode.next;

a b d e

null

firstNode

c

remove(2)

first get to node just before node to be removed

cc

beforeNode = firstNode.next;

b

beforeNode

remove(2)

now change pointer in beforeNode

beforeNode.next = beforeNode.next.next;

beforeNode

a b c d e

null

firstNode

One-Step add(0,’f’)

a b c d e

null

firstNode

f

newNode

firstNode = new ChainNode( new Character(‘f’), firstNode);

add(3,’f’)

  • first find node whose index is 2

a b c d e

null

firstNode f

newNode

beforeNode

c

  • next create a node and set its data and link fields

ChainNode newNode = new ChainNode(new Character(‘f’),

beforeNode.next);

  • finally link beforeNode to newNode beforeNode.next = newNode;

Two-Step add(3,’f’)

beforeNode = firstNode.next.next;

beforeNode.next = new ChainNode(new Character(‘f’),

beforeNode.next);

a b c d e

null

firstNode f

newNode

beforeNode

c