Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lists and Sorted Lists: various implementations Slides by Nadia Al-Ghreimil adopted from Steve Armstrong LeTourneau University Longview, TX  2007, 

Similar presentations


Presentation on theme: "Lists and Sorted Lists: various implementations Slides by Nadia Al-Ghreimil adopted from Steve Armstrong LeTourneau University Longview, TX  2007, "— Presentation transcript:

1 Lists and Sorted Lists: various implementations Slides by Nadia Al-Ghreimil adopted from Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall

2 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X Interfaces

3 Review – List Interface public interface ListInterface { public boolean add (T newEntry); public boolean add (int newPosition, T newEntry); public T remove (int givenPosition); public void clear (); public boolean replace (int givenPosition, T newEntry); public T getEntry (int givenPosition); public boolean contains (T anEntry); public int getLength (); public boolean isEmpty (); public boolean isFull (); public void display (); } // end ListInterface Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X public class Alist implements ListInterface {..... } // end AList public class ListClient { public static void main (... ) { testList (); } // end main public static void testList () { ListInterface runnerList = new AList (); // runnerList has only methods in ListInterface runnerList.add ("16"); // winner runnerList.add (" 4"); // second runnerList.add ("33"); // third runnerList.add ("27"); // fourth runnerList.display (); } // end testList } // end ListClient

4 List Interface – Implementation: as an Array and as a VectorList public interface ListInterface { public boolean add (T newEntry); public boolean add (int newPosition, T newEntry); public T remove (int givenPosition); public void clear (); public boolean replace (int givenPosition, T newEntry); public T getEntry (int givenPosition); public boolean contains (T anEntry); public int getLength (); public boolean isEmpty (); public boolean isFull (); public void display (); } // end ListInterface Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X public class Alist implements ListInterface { private T[ ] list; // array of list entries private int length;// current number of entries private static final int MAX_SIZE = 50; // p. 131 // Note: makeRoom and removeGap are needed // Note: use copyArray for expandable version } // end AList import java.util.Vector public class VectorList implements ListInterface { private Vector list; // entries in the list // no need for “length” use list.size() instead // no need for makeRoom nor removeGap } // end VectorList

5 List Interface – Implementation: as a chain public interface ListInterface { public boolean add (T newEntry); public boolean add (int newPosition, T newEntry); public T remove (int givenPosition); public void clear (); public boolean replace (int givenPosition, T newEntry); public T getEntry (int givenPosition); public boolean contains (T anEntry); public int getLength (); public boolean isEmpty (); public boolean isFull (); public void display (); } // end ListInterface Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X public class LList implements ListInterface { private Node firstNode; // ref. to 1 st Node private int length;// current number of entries // make clear() final – because it’s called within the constructor // p. 162 - … private Node getNodeAt( int givenPosition); // here we use an inner class Node private class Node { private T data; private Node next; // constructors } // ch.7 shows a separate class Node, same package // also adds a lastNode } // end LList

6 List vs Sorted List public interface ListInterface { // adds at end of list public boolean add (T newEntry); public boolean add (int newPosition, T newEntry); public T remove (int givenPosition); public void clear (); public boolean replace (int givenPosition, T newEntry); public T getEntry (int givenPosition); public boolean contains (T anEntry); public int getLength (); public boolean isEmpty (); public boolean isFull (); public void display (); } // end ListInterface Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X public interface SortedListInterface > { // adds in proper position public boolean add (T newEntry); public boolean add (int newPosition, T newEntry); public T remove (int givenPosition); public void clear (); public boolean replace (int givenPosition, T newEntry); public T getEntry (int givenPosition); public int getPosition (T anEntry); public boolean contains (T anEntry); public int getLength (); public boolean isEmpty (); public boolean isFull (); public void display (); } // end SortedListInterface Iterative – p.362 Recursive – p.364 Gives position where anEntry should go. -ve : not in the list +ve: it is in the list Gives position where anEntry should go. -ve : not in the list +ve: it is in the list

7 SortedList implementations public class SortedLinkedList > implements SortedListInterface { private Node firstNode; private int length; public SortedLinkedList () { firstNode = null; length = 0;} public boolean add (T newEntry) // iterative version { Node newNode = new Node (newEntry); Node nodeBefore = getNodeBefore (newEntry); // determine if add at beginning // or somewhere else // adjust pointers and length++ } // end add private Node getNodeBefore (T anEntry) { // initialization while ((currentNode != null) && (anEntry.compareTo (currentNode.getData ()) > 0)) { // adjust pointers } // end while return nodeBefore; } // end getNodeBefore private class Node { …} } // end SortedLinkedList public class SortedList > implements SortedListInterface { private ListInterface list; public SortedList () { list = new LList (); } public boolean add (T newEntry) { int newPosition = Math.abs (getPosition (newEntry)); return list.add (newPosition, newEntry); } // end add public int getPosition (T anEntry) { int position = 1; int length = list.getLength (); // find position of anEntry while ((position 0)) { position++; } // end while // see whether anEntry is in list, if not return –ve if ((position > length) || (anEntry.compareTo (list.getEntry (position)) != 0)) { position = -position; } // end if return position; } // end getPosition public T getEntry (int givenPosition) {return list.getEntry (givenPosition);} // end getEntry } // end SortedList

8 Efficiency List Sorted List direct using list Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X Operation ArrayLinkedArrayLinkedArrayLinked add(newEntry) add(newEntry,givenPosition) remove(anEntry) getPosition(anEntry) getEntry(givenPosition) contains(anEntry) remove(givenPosition) display() clear,getLength,isEmpty,isFull O(1) O(n) O(1) O(n) O(1) O(n) O(n) O(1) O(n) O(1) O(n) O(1) O(n) O(n) O(1) O(n) O(n) O(1) O(n) O(1) O(n 2 ) O(n 2 ) O(n) O(1)

9 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X Why is the performance so bad? When using ADT List instance:  you have to use the List’s operations  you can’t access the entries directly  it’s easy to write the implementation  but it is inefficient


Download ppt "Lists and Sorted Lists: various implementations Slides by Nadia Al-Ghreimil adopted from Steve Armstrong LeTourneau University Longview, TX  2007, "

Similar presentations


Ads by Google