Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


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:

1 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

2 General Sequences Iterators Solutions to selected exercises Outline

3 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

4 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 (5,4,3,2,1) General Sequences Bubble sorting

6 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( )

7 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( )

8 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

9 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

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

11 //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

12 //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

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

14 public Sequence reverse(Sequence S){ for(int i=0;i

15 public Sequence reverse(Sequence S){ Sequence S1=new Sequence(); Sequence S2=new Sequence(); for(int i=0;i

16 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


Ads by Google