Download presentation
Presentation is loading. Please wait.
Published byMary Ward Modified over 9 years ago
1
U n i v e r s i t y o f H a i l 1 ICS 202 2011 spring Data Structures and Algorithms
2
U n i v e r s i t y o f H a i l 2 Outline 1. Ordered Lists Array Implementation Linked-List Implementation Applications 2. Sorted Lists Array Implementation Linked-List Implementation Applications ICS 202 2009 Summer Data Structures and Algorithms Amani Mahajoub Omer
3
U n i v e r s i t y o f H a i l Lec 1
4
U n i v e r s i t y o f H a i l 4 Comparable Container SearchableContainer OrderedList SortedList AbstractObject AbstractContainer OrderedListAsArray OrderedListAsLinkedList SortedListAsArray SortedListAsLinkedList AbstractSearchableContainer
5
U n i v e r s i t y o f H a i l 5 1. Ordered List An ordered list is a list in which the order of the items is significant. The items in an ordered list are not necessarily sorted. It is possible to change the order of items and still have a valid ordered list. public interface OrderedList extends SearchableContainer { Comparable get (int i); // to access the object at a given position in the ordered list. Cursor findPosition (Comparable object); } // to find the position of an object in the ordered list.
6
U n i v e r s i t y o f H a i l 6 1. Ordered List A searchable container is a container that supports the following additional operations: insert, used to put objects into the container; withdraw, used to remove objects from the container; find, used to locate objects in the container; isMember, used to test whether a given object instance is in the container.
7
U n i v e r s i t y o f H a i l 7 1. Ordered List public interface Cursor { Comparable getDatum (); // to access the object in the ordered list at the current cursor position void insertAfter (Comparable object); // to insert an object into the ordered list after the current cursor position void insertBefore (Comparable object); // to insert an object into the ordered list before the current cursor position void withdraw (); // to remove from the ordered list the object at the current cursor position }
8
U n i v e r s i t y o f H a i l 8 1. Ordered List: Array Implementation public class OrderedListAsArray extends AbstractSearchableContainer implements OrderedList { protected Comparable[] array; // the OrderedListAsArray inherits the field count from AbstractContainer //... }
9
U n i v e r s i t y o f H a i l 9 1. Ordered List: Array Implementation public class OrderedListAsArray extends AbstractSearchableContainer implements OrderedList { protected Comparable[] array; public OrderedListAsArray (int size) { array = new Comparable [size]; } public void insert (Comparable object) { if (count == array.length) throw new ContainerFullException (); array [count] = object; ++count; } //... }
10
U n i v e r s i t y o f H a i l 10 1. Ordered List: Array Implementation public class OrderedListAsArray extends AbstractSearchableContainer implements OrderedList { protected Comparable[] array; public boolean isMember (Comparable object) { // tests weather object is member of the ordered list. for (int i = 0; i < count; ++i) if (array [i] == object) return true; return false; } public Comparable find (Comparable arg) { // locates an element in the list that matches arg for (int i = 0; i < count; ++i) if (array [i].isEQ (arg)) return array [i]; return null; }
11
U n i v e r s i t y o f H a i l 11 1. Ordered List: Array Implementation Comparable object1 = new Int (57); Comparable object2 = new Int (57); List list = new OrderedListAsArray (1); List.insert(object1); List.isMember(object1); // returns true List.isMember(object2); // returns false Comparable object3 = list.find (object1); // object3 = object1
12
U n i v e r s i t y o f H a i l 12 1. Ordered List: Array Implementation public class OrderedListAsArray extends AbstractSearchableContainer implements OrderedList { protected Comparable[] array; public void withdraw (Comparable object) { if (count == 0) throw new ContainerEmptyException (); int i = 0; while (i < count && array [i] != object) ++i; if (i == count) throw new IllegalArgumentException ( "object not found"); for ( ; i < count - 1; ++i) array [i] = array [i + 1]; array [i] = null; --count; }
13
U n i v e r s i t y o f H a i l 13 1. Ordered List: Array Implementation Comparable object1 = new Int (57); Comparable object2 = new Int (57); List list = new OrderedListAsArray (1); List.insert(object1); List.withdraw(object1); // removes the object1 from the ordered list List.withdraw(object2); // displays object not found
14
U n i v e r s i t y o f H a i l 14 1. Ordered List: Array Implementation public class OrderedListAsArray extends AbstractSearchableContainer implements OrderedList { protected Comparable[] array; protected class MyCursor implements Cursor // inner class { int offset; MyCursor (int offset) { this.offset = offset; } public Comparable getDatum () { if (offset = count) throw new IndexOutOfBoundsException (); return array [offset]; }
15
U n i v e r s i t y o f H a i l 15 1. Ordered List: Array Implementation public class OrderedListAsArray extends AbstractSearchableContainer implements OrderedList { protected Comparable[] array; public Cursor findPosition (Comparable object) { int i = 0; while (i < count && array [i].isNE (object)) ++i; return new MyCursor (i); } public Comparable get (int offset) { if (offset = count) throw new IndexOutOfBoundsException (); return array [offset]; }
16
U n i v e r s i t y o f H a i l 16 1. Ordered List: Array Implementation 271828 2713288 offset Cursor.insertAfter(3) offset
17
U n i v e r s i t y o f H a i l 17 1. Ordered List: Array Implementation public class OrderedListAsArray extends AbstractSearchableContainer implements OrderedList { protected Comparable[] array; protected class MyCursor implements Cursor { int offset; public void insertAfter (Comparable object) { if (offset = count) throw new IndexOutOfBoundsException (); if (count == array.length) throw new ContainerFullException (); int insertPosition = offset + 1; for (int i = count; i > insertPosition; --i) array [i] = array [i - 1]; array [insertPosition] = object; ++count; }
18
U n i v e r s i t y o f H a i l 18 1. Ordered List: Array Implementation 271828 2718283 offset Cursor.insertBefore(3) offset
19
U n i v e r s i t y o f H a i l 19 1. Ordered List: Question What modification of insertAfter method must be added to implement the insertBefore method?
20
U n i v e r s i t y o f H a i l 20 1. Ordered List: Array Implementation 271328 271828 offset Cursor.withdraw() offset 8
21
U n i v e r s i t y o f H a i l 21 1. Ordered List: Array Implementation public class OrderedListAsArray extends AbstractSearchableContainer implements OrderedList { protected Comparable[] array; protected class MyCursor implements Cursor { int offset; public void withdraw () // delete the item at the position specified by the cursor { if (offset = count) throw new IndexOutOfBoundsException (); if (count == 0) throw new ContainerEmptyException (); int i = offset; while (i < count - 1) { array [i] = array [i + 1]; ++i; } array [i] = null; // delete the last element --count; }
22
U n i v e r s i t y o f H a i l Lec 2
23
U n i v e r s i t y o f H a i l 23 1. Ordered List: Linked-List Implementation public class OrderedListAsLinkedList extends AbstractSearchableContainer implements OrderedList { protected LinkedList linkedList; // no limit of the number of items //... }
24
U n i v e r s i t y o f H a i l 24 1. Ordered List: Linked-List Implementation public class OrderedListAsLinkedList extends AbstractSearchableContainer implements OrderedList { protected LinkedList linkedList; // no limit of the number of items public OrderedListAsLinkedList () { linkedList = new LinkedList (); } public void insert (Comparable object) { linkedList.append (object); // add at the end of the ordered list ++count; } public Comparable get (int offset) { if (offset = count) throw new IndexOutOfBoundsException (); LinkedList.Element ptr = linkedList.getHead (); for (int i = 0; i < offset && ptr != null; ++i) ptr = ptr.getNext (); return (Comparable) ptr.getDatum (); // return the item at the position ptr }
25
U n i v e r s i t y o f H a i l 25 1. Ordered List: Linked-List Implementation public class OrderedListAsLinkedList extends AbstractSearchableContainer implements OrderedList { protected LinkedList linkedList; // no limit of the number of items public boolean isMember (Comparable object) { for (LinkedList.Element ptr = linkedList.getHead (); ptr != null; ptr = ptr.getNext ()) if ((Comparable) ptr.getDatum () == object) return true; return false; } public Comparable find (Comparable arg) { for (LinkedList.Element ptr = linkedList.getHead (); ptr != null; ptr = ptr.getNext ()) { Comparable object = (Comparable) ptr.getDatum (); if (object.isEQ (arg)) return object; } return null; }
26
U n i v e r s i t y o f H a i l 26 1. Ordered List: Linked-List Implementation public class OrderedListAsLinkedList extends AbstractSearchableContainer implements OrderedList { protected LinkedList linkedList; // no limit of the number of items public void withdraw (Comparable object) { if (count == 0) throw new ContainerEmptyException (); linkedList.extract (object); --count; }
27
U n i v e r s i t y o f H a i l 27 1. Ordered List: Linked-List Implementation public class OrderedListAsLinkedList extends AbstractSearchableContainer implements OrderedList { protected LinkedList linkedList; // no limit of the number of items protected class MyCursor implements Cursor { LinkedList.Element element; // reference to an item in the linked list MyCursor (LinkedList.Element element) { this.element = element; } public Comparable getDatum () { return (Comparable) element.getDatum (); }
28
U n i v e r s i t y o f H a i l 28 1. Ordered List: Linked-List Implementation public class OrderedListAsLinkedList extends AbstractSearchableContainer implements OrderedList { protected LinkedList linkedList; // no limit of the number of items public Cursor findPosition (Comparable arg) { LinkedList.Element ptr; for (ptr = linkedList.getHead (); ptr != null; ptr = ptr.getNext ()) { Comparable object = (Comparable) ptr.getDatum (); if (object.isEQ (arg)) break; } return new MyCursor (ptr); // returns the reference of arg }
29
U n i v e r s i t y o f H a i l 29 1. Ordered List: Linked-List Implementation public class OrderedListAsLinkedList extends AbstractSearchableContainer implements OrderedList { protected LinkedList linkedList; // no limit of the number of items protected class MyCursor implements Cursor { LinkedList.Element element; public void insertAfter (Comparable object) { element.insertAfter (object); // provided by LinkedList class ++count; }
30
U n i v e r s i t y o f H a i l 30 1. Ordered List: Linked-List Implementation public class OrderedListAsLinkedList extends AbstractSearchableContainer implements OrderedList { protected LinkedList linkedList; // no limit of the number of items protected class MyCursor implements Cursor { LinkedList.Element element; public void withdraw () { linkedList.extract (element.getDatum ()); --count; }
31
U n i v e r s i t y o f H a i l ICS 202 2007-2 Data Structures and Algorithms Dr. Youssef Harrath 31 2. Sorted List A sorted list is like an ordered list: it is a serchable container that holds a sequence of objects. The position of an items in a sorted list is not arbitrary. public interface SortedList extends SearchableContainer { Comparable get (int i); // to access the object at a given position in the sorted list. Cursor findPosition (Comparable object); } // to find the position of an object in the sorted list.
32
U n i v e r s i t y o f H a i l 32 2. Sorted List: Array Implementation public class SortedListAsArray extends OrderedListAsArray implements SortedList { // no additional field is required, the fields provided by the base class // OrderedListAsArray are suffcient //... }
33
U n i v e r s i t y o f H a i l 33 2. Sorted List: Array Implementation 11359 112593 insert(2)
34
U n i v e r s i t y o f H a i l 34 2. Sorted List: Array Implementation public class SortedListAsArray extends OrderedListAsArray implements SortedList { public void insert (Comparable object) { if (count == array.length) throw new ContainerFullException (); int i = count; while (i > 0 && array [i - 1].isGT (object)) { array [i] = array [i - 1]; --i; } array [i] = object; ++count; }
35
U n i v e r s i t y o f H a i l 35 2. Sorted List: Array Implementation public class SortedListAsArray extends OrderedListAsArray implements SortedList { protected int findOffset (Comparable object) { // binary search algorithm to find an item in a sorted list int left = 0; int right = count - 1; while (left <= right) { int middle = (left + right) / 2; if (object.isGT (array [middle])) left = middle + 1; else if (object.isLT (array [middle])) right = middle - 1; else return middle; } return -1; }
36
U n i v e r s i t y o f H a i l 36 2. Sorted List: Array Implementation public class SortedListAsArray extends OrderedListAsArray implements SortedList { public Comparable find (Comparable object) { int offset = findOffset (object); if (offset >= 0) return array [offset]; else return null; } public Cursor findPosition (Comparable object) { return new MyCursor (findOffset (object)) { public void insertAfter (Comparable object) { throw new InvalidOperationException (); } public void insertBefore (Comparable object) { throw new InvalidOperationException (); } }; }
37
U n i v e r s i t y o f H a i l 37 2. Sorted List: Array Implementation public class SortedListAsArray extends OrderedListAsArray implements SortedList { public void withdraw (Comparable object) { if (count == 0) throw new ContainerEmptyException (); int offset = findOffset (object); if (offset < 0) throw new IllegalArgumentException ( "object not found"); int i; for (i = offset; i < count - 1; ++i) array [i] = array [i + 1]; // move one position to the left array [i] = null; --count; }
38
U n i v e r s i t y o f H a i l 38 2. Sorted List: Linked-List Implementation public class SortedListAsLinkedList extends OrderedListAsLinkedList implements SortedList { // No additional field is required }
39
U n i v e r s i t y o f H a i l 39 2. Sorted List: Linked-List Implementation public class SortedListAsLinkedList extends OrderedListAsLinkedList implements SortedList { public void insert (Comparable arg) { LinkedList.Element ptr; LinkedList.Element prevPtr = null; for (ptr = linkedList.getHead (); ptr != null; ptr = ptr.getNext ()) { // to find the element that is greater than arg Comparable object = (Comparable) ptr.getDatum (); if (object.isGE (arg)) break; prevPtr = ptr; } if (prevPtr == null) linkedList.prepend (arg); else prevPtr.insertAfter (arg); // insert arg in the right position ++count; }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.