2de250f1f7d54bfc9dff63994cb90dde.ppt
- Количество слайдов: 10
Абстрактные типы данных 1. Абстрактная дата Date dt 1, dt 2; dt 1 = new Date(1, Date. MARCH, 2006); dt 2 = (Date)dt 1. clone(); dt 2. add(300); // 1. 03. 2006 + 300 дней while (dt 2. after(dt 1)) dt 2. add(0, -1, 0); Cloneable Comparable clone() compare. To() Date before() after(). . . Date 1 Date 2 clone() compare. To() before(). . .
Описание абстрактной даты public interface Date extends Cloneable, Comparable { // Cloneable redefinition Object clone(); // Comparing int compare. To(Date d); boolean before(Date d); boolean after(Date d); // Access to attributes int year(); int month(); int date(); int day(); // Date arithmetic void add(int days); void add(int years, int months, int days); Date clone. And. Add(int years, int months, int days); int date. Diff(Date dt); }
Реализация даты public class Date 1 int year, // month, // date; // implements Date { from 0 to 3000 from 0 to 11 from 1 to 31 // Constructors public Date 1(int year, int month, int date) { this. year = year; this. month = month; this. date = date; } public Date 1(Date src) { this(src. get. Year(), src. get. Month(), src. get. Date()); } // Cloneable redefinition public Object clone() { return new Date 1(this); } // Comparing public int compare. To(Date d) { return ((year - d. get. Year()) << 9) + ((month - d. get. Month()) << 5) + (date - d. get. Date()); }. . . }
2. Абстрактный список 1 2 3 … first n last public interface List { // Elements counting boolean is. Empty(); int size(); // Access to elements Object first(); Object last(); // Changes in list Object add. First(Object o); Object add. Last(Object o); Object remove. First(); Object remove. Last(); } } // List iteration void iterate(Visitor visitor); Iterator iterator();
Возможные реализации списков Связанный список Массив first = 0 first 1 last 2 3 last n
Реализация в виде связанного списка public class Linked. List implements List { private static class List. Node { Object info; List. Node next, pred; List. Node(Object o) { this(o, null); } List. Node(Object o, List. Node n, List. Node p) { info = o; next = n; pred = p; } } List. Node first = null, last = null; int count = 0; public Linked. List() {} public Object first() { return this. first. info; } public Object last() { return this. last. info; } public boolean is. Empty() { return count == 0; } public int size() { return count; } public Object add. First(Object obj) { List. Node new. Node = new List. Node(obj, first, null); if (first == null) last = new. Node; else first. pred = new. Node; first = new. Node; count++; return obj; } }
Реализация в виде массива public class Array. List implements List { private Object[] array = null; private int ptr. Start = 0; private int ptr. End = 0; private int count = 0; public Array. List() { array = new Object[10]; } public Object first() { return array[ptr. Start]; } public Object last() { return array[ptr. End == 0 ? array. length-1 : ptr. End-1]; } public boolean is. Empty() { return count == 0; } public int size() { return count; } public Object add. Last(Object o) { if (count == array. length) enlarge(10); if (ptr. End == array. length) ptr. End = 1; else ptr. End++; array[ptr. End-1] = o; count++; return null; } public Object remove. Last() { if (--ptr. End < 0) ptr. End = array. length-1; count--; return array[ptr. End]; } }
Итерация элементов сложной структуры Внутренний итератор: public interface Visitor { void visit(Object node); } public class Printer implements Visitor { public void visit(Object node) { System. out. print(node); } } public static void main(String[] args) { List lst =. . . ; lst. iterate(new Printer()); } public interface List {. . . void iterate(Visitor visitor); } public class Linked. List implements List {. . . public void iterate(Visitor visitor) { for (List. Node current = first; current != null; current = current. next) { visitor. visit(current. info); } } }
Внешний итератор: public static void main(String[] args) { List lst =. . . ; Iterator it = lst. iterator(); for (Object next. Obj = it. begin(); next. Obj != it. end(); next. Obj = it. next()) { System. out. print(" " + next. Obj); } } public static void main(String[] args) { List lst =. . . ; for (Iterator it = lst. iterator(); it. has. Next(); ) { Object next. Obj = it. next(); System. out. print(" " + next. Obj); } } public interface Iterator { boolean has. Next(); void next(); void remove(); } public interface List {. . . Iterator iterator(); }
Реализация внешнего итератора для связанного списка: public class Linked. List implements List {. . . public Iterator iterator() { return new List. Iterator(); } private class List. Iterator implements Iterator { List. Node current = first; List. Node to. Remove = null; public boolean has. Next() { return current != null; } public Object next() { if (current == null) throw new No. Such. Element. Exception("List. Iterator"); to. Remove = current; Object ret = current. info; current = current. next; return ret; } public void remove() { if (to. Remove == null) throw new Illegal. State. Exception("List. Iterator"); List. Node pred = to. Remove. pred; if (pred == null) first = current; else pred. next = current; if (current != null) current. pred = pred; to. Remove = null; count--; } } }
2de250f1f7d54bfc9dff63994cb90dde.ppt