


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Exam; Class: DATA STRUCTURES; Subject: Computer Science; University: Oregon State University; Term: Spring 2003;
Typology: Exams
1 / 4
This page cannot be seen from the preview
Don't miss anything!



jump directly to the n th^ element
temporary data buffer
public class SortedList implements Sorted, Bag, FindMin { private LinkedList list = new LinkedList(); protected Comparator test; public SortedList(Comparator t) { test = t; } public SortedList() { test = new DefaultComparator; } ... }
// Implementation of the Bag interface. public void addElement(Object obj) { DoubleLink link = list.head; while (link != list.tail) { if (test.compare(obj, link.val) < 0) { // Skip smaller elements. link.insert(obj); return; } link = link.next } link.insert(obj); }
// Implementation of the FindMin interface. public Object getFirst() { return list.getFirst(); } public void removeFirst() { list.removeFirst(); }
Merge Sort
B: 1 - 1 - 3 - 7 - 9 - 13 - 14
2
A 4 5 8 10 sentinel Call insert at …
Merge Sort
public void merge(Sorted data) { DoubleLink link = list.head; Enumeration e = data.elements(); while (e.hasMoreElements()) { Object obj = e.nextElement(); while (link != list.tail && test.compare(link.val, obj) < 0) link = link.next; link.insert(obj); } }
List Variations (cont.)
List Variations: Self-Organizing List
Self-Organizing List public class SelfOrgList extends LinkedList { public boolean containsElement(Object obj) { for (DoubleLink l = head; l != tail; l = l.next) if (obj.equals(l.val)) { l.remove(); // Move to front of list. addFirst(obj); return true; } return false; } }
List Variations: Skip List
Skip List: Example
8 17 23 44
5 8 12 17 23 32 44 tail
Skip List: Algorithmic Complexity – Insert
Skip List: Bag Interface – Search & Remove public boolean containsElement(Object obj) { Doublelink p = top; while (p != null) { p = slideLeft(p, obj); if (p.val != null && obj.equals(p.val)) return true; if (p instanceof SkipLink) p = ((SkipLink)p).down; else p = null; } return false; }
Complexity Comparison
SkipList O(log n ) O(log n ) O(log n )
SortedList O( n ) O( n ) O( n )
LinkedList O(1) O( n ) O( n )
SortedVector O( n ) O(log n ) O( n )
Container Insert Search Remove