Link public class Link private Object mdata private
Link מחלקה שמייצגת חוליה public class Link { private Object m_data; private Link m_next; public Link(Object data, Link next) { m_data = data; m_next = next; } public Link(Object data) { this(data, null); } public Object get. Data() { return m_data; } public void set. Data(Object data) { m_data = data; } public Link get. Next() { return m_next; } public void set. Next(Link next) { m_next = next; } } 3 Link Data Next
Linked. List מחלקה שמייצגת רשימה משורשרת public class Linked. List { private Link m_first; public Linked. List (){ m_first = null; } public Linked. List (Object data){ m_first = new Link(data, null); } . . . } 4
Linked. List מחלקה שמייצגת רשימה משורשרת Linked. List first data 5 next data next null
: נוסיף למחלקה את השיטה public void add. Last(Object data) { Link new. Link = new Link(data, null); if (is. Empty()) { m_first = new. Link; } else { Link link. Pointer = m_first; while (link. Pointer. get. Next() != null) { link. Pointer = link. Pointer. get. Next(); } link. Pointer. set. Next(new. Link); } } 7
דוגמת שימוש Linked. List lst = new Linked. List(); lst. add. Last( "Hello" ); lst. add. Last( "Great" ); lst. add. Last( "World!"); Linked. List first null data next 8 data next null
המשך דוגמת שימוש lst. remove. First(); Linked. List first data next "Hello" 10 data next "Great" data next "World!" null
המשך דוגמת שימוש lst. remove. First(); Linked. List first data next "Great" 11 data next "World!" null
המשך דוגמת שימוש lst. remove. First(); Linked. List first data next "World!" 12 null
המשך דוגמת שימוש lst. remove. First(); Linked. List first null 13
: reverse() נוסיף את השיטה : Link. List במחלקה public void reverse(){ If (! is. Empty()) m_first = m_first. reverse(); } 14
Link במחלקה public Link reverse(){ Link ans; If (m_next==null) ans = this; else{ ans = m_next. reverse(); m_next. set. Next(this); m_next = null; } return ans; 15 }
get. Middle public Object get. Middle(){ Object ans; if(is. Empty()) { ans = null; } else { Link current = first; Link jumper = first; while( (jumper != null) && && (jumper. get. Next() != null) ){ current = current. get. Next(); jumper = jumper. get. Next(); } ans = current. get. Data(); } return ans; } 17
… while( (jumper != null) && (jumper. get. Next() != null) ){ current = current. get. Next(); jumper = jumper. get. Next(); } ans = current. get. Data(); … 18
public boolean contains. Circles() { boolean ans; if(is. Empty()) { ans = false; } else { Link current = first; Link jumper = first; boolean start = true; while(jumper != null && ( current != jumper || start) ){ start = false; current = current. get. Next(); jumper = jumper. get. Next(); if (jumper != null) jumper = jumper. get. Next(); } ans = (jumper != null); } return ans; } 20
while(jumper != null && ( current != jumper || start) ){ start = false; current = current. get. Next(); jumper = jumper. get. Next(); if (jumper != null) jumper = jumper. get. Next(); } 21
while(jumper != null && ( current != jumper || start) ){ start = false; current = current. get. Next(); jumper = jumper. get. Next(); if (jumper != null) jumper = jumper. get. Next(); } 22
while(jumper != null && ( current != jumper || start) ){ start = false; current = current. get. Next(); jumper = jumper. get. Next(); if (jumper != null) jumper = jumper. get. Next(); } 23
while(jumper != null && ( current != jumper || start) ){ start = false; current = current. get. Next(); jumper = jumper. get. Next(); if (jumper != null) jumper = jumper. get. Next(); } 24
while(jumper != null && ( current != jumper || start) ){ start = false; current = current. get. Next(); jumper = jumper. get. Next(); if (jumper != null) jumper = jumper. get. Next(); } 25
while(jumper != null && ( current != jumper || start) ){ start = false; current = current. get. Next(); jumper = jumper. get. Next(); if (jumper != null) jumper = jumper. get. Next(); } 26
Iterator 27
public interface Iterator { /** * Returns true if the iteration has more elements. * @return true if the iterator has more elements. */ boolean has. Next(); /** * Returns the next element in the iteration. * @return the next element in the iteration. * @exception No. Such. Element. Exception iteration has no more elements. */ Object next(); /** * Removes from the underlying collection the last element returned by the * iterator (optional operation) * @exception Unsupported. Operation. Exception if the "remove" * operation is not supported by this Iterator. * * @exception Illegal. State. Exception if the "next" method has not * yet been called, or the "remove" method has already * been called after the last call to the "next" * method. */ void remove(); } 31
Iterator- שימוש ב , Iterator- כך שתתמוך ב Linked. List בהמשך נראה כיצד להפוך את המחלקה שמתקבל Iterator- ועל סמך זאת נעשה שימוש ב , כרגע נניח שהמחלקה כבר תומכת בו . Iterable מממשת את Linked. List- שמובטחת לנו מכיוון ש , iterator() מהשיטה public static void main(String[] args) { Linked. List lst = new Linked. List(); lst. add. Last("Shnitsels"); lst. add. Last(“Are"); lst. add. Last("Tasty"); Iterator it = lst. iterator(); while (it. has. Next()) { Object current. Data = it. next(); System. out. print(current. Data); if (it. has. Next()) System. out. print(", "); } System. out. println(); } 32
דוגמת שימוש while (it. has. Next()) { Object current. Data = it. next(); System. out. print(current. Data); if (it. has. Next()) System. out. print(", "); } System. out. println(); List. Iterator Shnitsels, Are, Tasty current. Linked. List first data next 33 data next null
Linked. List במחלקה Iterator- מימוש ה public class List. Iterator implements Iterator { private Link current. Link; public List. Iterator(Link first) { current. Link = first; } public boolean has. Next() { return ( current. Link != null ); } public Object next() { if (!has. Next()) { throw new No. Such. Element. Exception(); } Object data = current. Link. get. Data(); current. Link = current. Link. get. Next(); return data; } public void remove() { throw new Unsupported. Operation. Exception(); } } 35
Linked. List במחלקה Iterable- מימוש ה את לממש הוא לעשות שנותר מה כל • עכשיו ב Iterable הממשק של iterator() השיטה : Linked. List public class Linked. List implements Iterable{ private Link first; public Linked. List (){ first = null; } … // All methods remain the same public Iterator iterator(){ return new List. Iterator(first); } } 36
public interface Filter { /** * accept – defines the filtration criterion. * @param o the object to be examined. * @return true if the object should not be * filtered out. */ public boolean accept(Object o); } : ריקות הלא המחרוזות את רק המשאיר , String המחלקה עבור פשוט Filter נממש אנו public class Empty. String. Filter implements Filter { public boolean accept(Object o) { boolean ans = false; if (o instanceof String) { String s = (String) o; ans = s. length() > 0; } return ans; } } 40
המקבלת מסנן , Linked. List היינו רוצים לממש שיטה במחלקה . ומחזירה רשימה מסוננת : כך שנוכל להשתמש בה באופן הבא Linked. List lst = new Linked. List(); lst. add. Last( "Hello" ); lst. add. Last( "" ); lst. add. Last( "Great" ); lst. add. Last( "World" ); Linked. List ans = lst. filter(new Empty. String. Filter()); //ans = ["Hello" , "Great" , "World" ] 41
: Linked. List- כל שנותר הוא לממש את אלגוריתם הסינון ב ומחזיר רשימה חדשה שתכיל את כל אברי , האלגוריתם מקבל כקלט מסנן . הרשימה הנוכחית שעברו את המסנן public Linked. List filter(Filter filter) { Linked. List filtered. List = new Linked. List() ; Iterator it = this. iterator(); while (it. has. Next()) { Object data = it. next(); if(filter. accept(data)) { filtered. List. add. Last(data) ; } } return filtered. List; } 42
Linked. List lst = new Linked. List(); lst. add. Last( "Hello" ); lst. add. Last( "" ); lst. add. Last( "Great" ); lst. add. Last( "World" ); Linked. List ans = lst. filter(new Empty. String. Filter()); //ans = ["Hello" , "Great" , "World" ] 43
public interface Queue{ /** * is. Empty - checks if the queue is empty or not. * @return true if the queue is empty */ public boolean is. Empty(); /** * dequeue - removes an object from the head of the queue. * (FIFO order) * @return the next object in the queue. */ public Object dequeue(); /** * enqueue - inserts an object into the queue. * @param o the object to be enqueued. */ public void enqueue(Object o); } 45
מימוש תור ע"י רשימה מקושרת public class Queue. As. List implements Queue { private Linked. List lst; public Queue. As. List() { lst = new Linked. List(); } public boolean is. Empty() { return lst. is. Empty(); } public Object dequeue() { return lst. remove. First(); } public void enqueue(Object o) { lst. add. Last(o); } } 46
המשך import java. util. Scanner; … public static void main(String args[]) { Scanner sc = new Scanner(System. in); Queue q = new Queue. As. List(); System. out. println("Insert few lines … "); while (sc. has. Next. Line()) { String line = sc. next. Line(); q. enqueue( line ); } System. out. println("Printing all the lines back! "); while (!q. is. Empty()) { System. out. println(q. dequeue()); } } 48
public static Player simulate. Tournament( Linked. List players. List) { Queue q = new Queue. As. List(); Iterator it = players. List. iterator(); Player winner = null; while (it. has. Next()) { q. enqueue( it. next() ); } while ( ! q. is. Empty() ) { Player first = (Player)q. dequeue(); if (q. is. Empty()) { winner = first; } else { Player second = (Player)q. dequeue(); Player match. Winner = get. Winner(first, second); q. enqueue( match. Winner ); } } return winner; } 56
ממשק public interface Stack { /** * push - adds an element to the stack. * @param o the elemented to be inserted to the stack. */ public void push (Object o); /** * pop - removes an element form the stack (LIFO order). * @return the element from the top of the stack. */ public Object pop (); /** * is. Empty - checks if the stack is empty or not. * @return true if there is no more elements in the stack. */ public boolean is. Empty(); } 58
- Slides: 59