170 likes | 263 Views
public interface List { public boolean isEmpty(); public boolean addAt(int index, Object add_me); public Object set(int index, Object set_me); public boolean contains(Object am_i_here); public Iterator iterator(); public Object elementAt(int index);
E N D
public interface List { public boolean isEmpty(); public boolean addAt(int index, Object add_me); public Object set(int index, Object set_me); public boolean contains(Object am_i_here); public Iterator iterator(); public Object elementAt(int index); public Object remove(int index); public Object remove(Object remove_me); public boolean removeAll(Object remove_me); }
first נממש רשימה באמצעות רשימה משורשרת עם זקיף (sentinel) זקיף נתון ראשון נתון שני
נוסיף למחלקה Link שתי שיטות סטטיות ושיטת equals. public boolean equals(Object other) { if(other instanceof Link) return equals((Link) other); return false; } public boolean equals(Link other) { if (other == null) return false; if (data() == null) return other == null; return ((data().equals(other.data())) && (next() == other.next())); }
public static Object add_next(Link l, Object add_me) { return l.set_next(new Link(add_me,l.next())); } public static Object remove_next(Link l) { if (l.next() == null) return null; Object out = l.next().data(); l.set_next(l.next().next()); return out; }
public class Minimal_linked_list { /* does not implement List */ private Link first; public Minimal_linked_list() { first = new Link("Off left",null); } public Minimal_linked_list(Link first) { this.first = first; } public Link first_link() { return first.next();} public boolean isEmpty() {return first_link() == null;} public boolean addAtHead(Object add_me) { Link.add_next(first,add_me); return true; }
public Object head() { if (isEmpty()) return null; return first_link().data(); } public Object removeHead() { if (isEmpty()) return null; return Link.remove_next(first); } public Object setHead(Object set_me) { if (isEmpty()) return null; return first_link().set_data(set_me); } public boolean headIs(Object obj) { if (isEmpty()) return false; if (first_link().data() == null) return obj == null; return first_link().data().equals(obj); }
public boolean equals(Object other) { if (other == null) return false; if (! (other instanceof Minimal_linked_list)) return false; if (isEmpty()) return ((Minimal_linked_list)other).isEmpty(); return first_link(). equals(((Minimal_linked_list)other).first_link()); } }
public class Linked_list extends Minimal_linked_list implements List { public Linked_list() { super(); } public Linked_list(Link first) { super(first); } private Linked_list tail() { return new Linked_list(first_link()); } public boolean addAt(int index, Object add_me) { if (index == 0) return addAtHead(add_me); if (isEmpty()) return false; return tail().addAt(index-1, add_me); } public Object elementAt(int index) { if (isEmpty()) return null; if (index == 0) return head(); return tail().elementAt(index-1); } public Object remove(int index) { if (isEmpty()) return null; if (index == 0) return removeHead(); return tail().remove(index - 1); } public Object remove(Object remove_me) { if (isEmpty()) return null; if (headIs(remove_me)) return removeHead(); return tail().remove(remove_me); } public boolean removeAll(Object remove_me) { return rec_removeAll(remove_me,false); } private boolean rec_removeAll(Object remove_me,boolean flag){ if (isEmpty()) return flag; if (headIs(remove_me)) { removeHead(); return rec_removeAll(remove_me,true); } return tail().rec_removeAll(remove_me,flag); }
public boolean addAt(int index, Object add_me) { if (index == 0) return addAtHead(add_me); if (isEmpty()) return false; return tail().addAt(index-1, add_me); } public Object elementAt(int index) { if (isEmpty()) return null; if (index == 0) return head(); return tail().elementAt(index-1); } public Object remove(int index) { if (isEmpty()) return null; if (index == 0) return removeHead(); return tail().remove(index - 1); } public Object remove(Object remove_me) { if (isEmpty()) return null; if (headIs(remove_me)) return removeHead(); return tail().remove(remove_me); } public boolean removeAll(Object remove_me) { return rec_removeAll(remove_me,false); } private boolean rec_removeAll(Object remove_me,boolean flag){ if (isEmpty()) return flag; if (headIs(remove_me)) { removeHead(); return rec_removeAll(remove_me,true); } return tail().rec_removeAll(remove_me,flag); }
public Object remove(Object remove_me) { if (isEmpty()) return null; if (headIs(remove_me)) return removeHead(); return tail().remove(remove_me); } public boolean removeAll(Object remove_me) { return rec_removeAll(remove_me,false); } private boolean rec_removeAll(Object remove_me,boolean flag){ if (isEmpty()) return flag; if (headIs(remove_me)) { removeHead(); return rec_removeAll(remove_me,true); } return tail().rec_removeAll(remove_me,flag); }
public Object set(int index, Object set_me) { if (isEmpty()) return null; if (index == 0) return setHead(set_me); return tail().set(index-1,set_me); } public boolean contains(Object obj) { if (isEmpty()) return false; if (headIs(obj)) return true; return tail().contains(obj); } public Iterator iterator() { return (Iterator) new Linked_list_iterator(); }
public void print() { Iterator iter = iterator(); while(iter.hasNext()) System.out.print(iter.next()+" "); System.out.println(" "); } public class Linked_list_iterator implements Iterator { private Linked_list running; public Linked_list_iterator() { running = new Linked_list(new Link("off left",first_link())); } public boolean hasNext() { return (! running.isEmpty());} public Object next() { Object head = running.removeHead(); return head; } }
public class List_driver { public static void main(String[] args) { List lst = new Linked_list(); StringTokenizer tokens = new StringTokenizer("0 1 2 3 4 5 6 7 8 9 10"); while (tokens.hasMoreTokens()) lst.addAt(0,tokens.nextToken()); ((Linked_list) lst).print(); System.out.println(lst.addAt(6,"6")); ((Linked_list) lst).print(); System.out.println(lst.removeAll("6")); ((Linked_list) lst).print(); System.out.println(lst.set(6,"x")); ((Linked_list) lst).print(); System.out.println(lst.contains("10")+ " "+ lst.contains("uuuu")); } } פלט 10 9 8 7 6 5 4 3 2 1 0 true 10 9 8 7 6 5 6 4 3 2 1 0 true 10 9 8 7 5 4 3 2 1 0 x 10 9 8 7 5 4 x 2 1 0 true false
public class SortableList extends Linked_list{ public SortableList(){ super(); } private SortableList(Link first) {super(first);} private SortableList tail() { return new SortableList(first_link()); } protected boolean comparableWith(Object data){ if (data == null) return false; else if (isEmpty()) return data instanceof Comparable; else return data.getClass().isInstance(head()); }
public boolean addAtHead(Object data){ if (comparableWith(data)) return super.addAtHead(data); else return false; } public boolean insert(Comparable data){ // add data before the first element which is bigger than it if (! comparableWith(data)) return false; if (isEmpty()) return addAtHead(data); if (data.compareTo(head()) < 0) return addAtHead(data); return ((SortableList)tail()).insert(data); }
public void sort(){ if (!isEmpty()){ Object head = removeHead(); sort(); insert((Comparable) head); } } }
import java.util.StringTokenizer; public class SortableList_driver { public static void main(String[] args) { SortableList lst = new SortableList(); StringTokenizer tokens = new StringTokenizer("0 1 2 3 4 5 6 7 8 9 10"); while (tokens.hasMoreTokens()) lst.addAt(0,tokens.nextToken()); ((Linked_list) lst).print(); lst.sort(); ((Linked_list) lst).print(); } } פלט 10 9 8 7 6 5 4 3 2 1 0 0 1 10 2 3 4 5 6 7 8 9