Mohammad Amin Kuhail M.Sc. (York, UK) University of Palestine Faculty of Engineering and Urban planning Software Engineering Department Computer Science.

Presentation on theme: "Mohammad Amin Kuhail M.Sc. (York, UK) University of Palestine Faculty of Engineering and Urban planning Software Engineering Department Computer Science."— Presentation transcript:

Mohammad Amin Kuhail M.Sc. (York, UK) University of Palestine Faculty of Engineering and Urban planning Software Engineering Department Computer Science II Monday, 5 November 2007 Lecture 11 of Sequences

General Sequences Iterators Solutions to selected exercises Outline

A sequence is an ADT that supports all the methods of the ranked sequences and positional sequence following the method that provide bridging connections between ranks and positions: Position atRank(int r) int rankOf(Position p) General Sequences Definition

Sequences Comparing Sequence Implementations ListArrayOperations O(1) Size,isEmpty O(n)O(1)atRank, rankOf,elemAtRank O(1) first, last before, after O(1) O(n) O(1) replace,swap replaceElemAtRank O(n) insertElemAtRank, removeElemAtRank O(1) O(1) circular O(1) insertFirst, insertLast O(1)O(n)insertAfter, insertBefore, remove

(5,4,3,2,1) General Sequences Bubble sorting

public void static bubbleSort1(IntegerSequence S){ int n=s.size(); for(int i=0;is.atRank(j+1).element. intValue()) swap(s.positionAtRank(j),s.positionAtRank(j+1)); } General Sequences Bubble Sorting Array: O( ) List: O( )

public void static bubbleSort2(IntegerSequence S){ int n=s.size(); IntegerSequencePosition prec, succ; for(int i=0;isucc.element.intValue()) swap(prec,succ); else prec=succ; } General Sequences Bubble Sorting Array, List: O( )

An iterator is a software design pattern that abstracts the process of scanning through a collection of elements one element at a time. An iterator consists of a sequence S, a current position in S, and a way of stepping to a next position in S and making it the current position. Iterators and Enumerations Definition

Java provides a simplified version of an iterator through an enumeration ADT, which defines the following methods: boolean hasMoreElements() Object nextElement() Example: public PrintCollection(Vector vec){ Enumeration enum=vec.elements(); While(enum.hasMoreElements()) System.out.println(enum.nextElement()); } Iterators and Enumerations Definition

R4.4 R4.6 R4.7 C4.2 C4.4 Exercises

//when the method is invoked the node to be given shall be the first one public boolean containsInt(IntegerSequence S,NSNode node,int k){ if(node.element().intValue()==i)return true; else try{ return containsInt(S,i,S.after(p)); } Catch(BoundaryViolationException e){return false;} } Exercise 4.6

//Alternative solution, but it doesn’t preserve the sequence. public boolean containsInt(IntegerSequence S,int k){ if(S.size()>0){ if(S.remove(first())==i)return true; else containsInt(S,k); } else{ return false; } Exercise 4.6

public Position makeFirst(Sequence S,Position p){ return insertFirst(remove(p)); } Exercise 4.7

public Sequence reverse(Sequence S){ for(int i=0;i { "@context": "http://schema.org", "@type": "ImageObject", "contentUrl": "http://images.slideplayer.com/13/4113998/slides/slide_14.jpg", "name": "public Sequence reverse(Sequence S){ for(int i=0;i

public Sequence reverse(Sequence S){ Sequence S1=new Sequence(); Sequence S2=new Sequence(); for(int i=0;i { "@context": "http://schema.org", "@type": "ImageObject", "contentUrl": "http://images.slideplayer.com/13/4113998/slides/slide_15.jpg", "name": "public Sequence reverse(Sequence S){ Sequence S1=new Sequence(); Sequence S2=new Sequence(); for(int i=0;i

public Object potatoesGame(Sequence S){ if(S.size()==1)return S.elemAtRank(0); int k= Math.random()*S.size(); S.removeElemAtRank(k); potatoesGame(S); } Exercise C4.6

Download ppt "Mohammad Amin Kuhail M.Sc. (York, UK) University of Palestine Faculty of Engineering and Urban planning Software Engineering Department Computer Science."

Similar presentations