Iterators - PowerPoint PPT Presentation

1 / 7
About This Presentation
Title:

Iterators

Description:

Allow the programmer to traverse the list and modify the ... Traverse cur's neighbors for a vertex u with minimum counterclockwise angle ?(preVertex, cur, u) ... – PowerPoint PPT presentation

Number of Views:17
Avg rating:3.0/5.0
Slides: 8
Provided by: csUi
Category:

less

Transcript and Presenter's Notes

Title: Iterators


1
Iterators
  • Why use iterators?

Tom
M
  • How many female employees?
  • Output those employees younger than 35.
  • Raise 10K employees salary that is lt 100K.
  • We only discuss List iterators today.

2
Iterators
  • Allow the programmer to traverse the list and
    modify the list during iteration .

Linked list
current
List iterator 1
current
List iterator 2
3
Implement singly linked list iterator
  • class ListIterator //(Textbook P.237)
  • private Link current // current link
  • private Link previous // previous link
  • private LinkList ourList // parent list
  • public ListIterator( LinkList list)
    //constructor
  • outList list
  • current list.getFirst()
  • public Link getCurrent() // returns current
    link
  • public void nextLink() // go to next link
  • public boolean atEnd() // true if last link
  • public void deleteCurrent() //delete item at
    current
  • public void insertAfter( long dd) // insert
    after current link

Exercise atEnd, deleteCurrent
4
Generate an iterator from a list
class LinkList Link first . public
ListIterator getIterator() // return an iterator
referring to this list return new
ListIterator(this)
5
Use iterators
// Increase wages of all employees who are paid
minimum by amount void increaseWages(LinkList
empolyees, long minimum, long amount)
6
Project 2.3 GPSR
  • Greedy Perimeter Stateless Routing (GPSR)
  • Implementation public String GPSR(String s,
    String t)
  • Idea
  • path the path created from GPSR.
  • len length of the path.
  • cur the current vertex being checked.
  • local_min current perimeter.
  • preVertex the vertex added to path before cur.
  • Initialization add s to path. cur s local_min
    dist(s, t) Initial mode is greedy mode.
    preVertex null
  • while (cur is NOT the destination t), do
  • Traverse curs neighbors for a vertex
    min_vertex closest to t. Suppose dmin
    dist(min_vertex, t).
  • If dmin lt local_min, then
  • Enter greedy mode.
  • Add min_vertex to path. Local_min dmin. Set
    cur min_vertex.
  • Else
  • Enter perimeter mode.
  • If last mode is greedy mode
  • Traverse curs neighbors for a vertex u with
    minimum counterclockwise angle

Correction made on Nov 15th!
7
How to compute ?(t, x, u) in 0,2p)
(0, 2 p
?(t, x, u)
p
?(t, x, v)
  • Counter clockwise angle ?(t, x, u)
  • cosAngle cosine(t, x, u) // As in project2.2
    solution
  • theta Math.acos(cosAngle) //Here theta is in
    0, p, if theta 0.0, set theta 2 p.
  • Check if direction t gt x gt u is counter
    clockwise. If so, adjust theta 2Math.PI
    theta.
  • If ( ) gt 0, then t gt x gt u is counter
    clockwire
  • Return theta.

cosine(x, t, u)
(posxtposyx-posxxposyt posxxposyu-
posxuposyx posxuposyt-posxtposyu)
2signed area of triangle(txu)
Write a Comment
User Comments (0)
About PowerShow.com