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

Slides:



Advertisements
Similar presentations
List Implementations That Use Arrays
Advertisements

Lists Chapter 8 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Lists Chapter 4 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X.
Stacks Chapter 21 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Dictionaries Chapter Chapter Contents Specifications for the ADT Dictionary Entries and methods Using the ADT Dictionary English Dictionary Telephone.
Dictionary Implementations Chapter 18 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
A List Implementation That Links Data Chapter 6 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Faster Sorting Methods Chapter 12 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Creating Classes from Other Classes Chapter 2 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Linked Lists Dr. Tim Margush University of Akron © 2009.
Completing the Linked Implementation of a List Chapter 7 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Lists Chapter 4. 2 Chapter Contents Specifications for the ADT List Redefining the Specifications Using the ADT List Using a List Is Like Using a Vending.
List Implementations That Use Arrays Chapter 5 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X Announcements.
List Implementations That Use Arrays Chapter 5. 2 Chapter Contents Using a Fixed-Size Array to Implement the ADT List An Analogy The Java Implementation.
Iterators Chapter 8 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Queue, Deque, and Priority Queue Implementations Chapter 24 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Iterators Chapter 7. Chapter Contents What is an Iterator? A Basic Iterator Visits every item in a collection Knows if it has visited all items Doesn’t.
The Efficiency of Algorithms Chapter 9 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Stack Implementations Chapter 22 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Searching Chapter 16 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Inheritance and Lists Chapter 14 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
List Implementations That Link Data Chapter 6. 2 Chapter Contents Linked Data Forming a Chains The Class Node A Linked Implementation Adding to End of.
Mutable, Immutable, and Cloneable Objects Chapter 15 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Sorted Lists and Their Implementations Chapter 12 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Chapter 3 Array-Based Implementations CS Data Structures Mehmet H Gunes Modified from authors’ slides.
An Introduction to Sorting Chapter 11 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Hashing as a Dictionary Implementation Chapter 20 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Graph Implementations Chapter 31 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
A Binary Search Tree Implementation Chapter 27 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Dictionaries Chapter 17 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Lists Chapter 4. 2 Chapter Contents Specifications for the ADT List Redefining the Specifications Using the ADT List Java Class Library: The Interface.
Lists Chapter 4 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Lists Chapter 4. 2 Chapter Contents Specifications for the ADT List Redefining the Specifications Using the ADT List Using a List Is Like Using a Vending.
Chapter 5 Linked Lists II
Stack Implementations Chapter 6 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Course: Object Oriented Programming - Abstract Data Types Unit2: ADT ListsSlide Number 1 Principles for implementing ADTs ADT operations as “walls” between.
Iterators (short version) Chapter 15 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Array-Based Implementations Chapter 3 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
A Bag Implementation that Links Data Chapter 3 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions.
Iterator Summary Slides by Entesar Al-Mosallam adopted from Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 18 List ADT Animated Version.
Sorted Lists Chapter Chapter Contents Specifications for the ADT Sorted List Using the ADT Sorted List A Linked Implementation The Method add The.
1 CS162: Introduction to Computer Science II Abstract Data Types.
CS Data Structures Chapter 8 Lists Mehmet H Gunes
Lists Chapter 4.
An Introduction to Sorting
Queue, Deque, and Priority Queue Implementations
A Bag Implementation that Links Data
Queue, Deque, and Priority Queue Implementations
Chapter 12 Sorted Lists and their Implementations
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Recitation 5 CS0445 Data Structures
Slides by Steve Armstrong LeTourneau University Longview, TX
List Implementations Chapter 9.
Slides by Steve Armstrong LeTourneau University Longview, TX
Iterators (partial) Chapter 8 Slides by Nadia Al-Ghreimil
A List Implementation That Links Data
Sorted Lists and Their Implementations
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Stack Implementations
Queues CSC212.
Lecture 6: Linked Data Structures
Recitation 2 CS0445 Data Structures
A List Implementation That Links Data
Copyright ©2012 by Pearson Education, Inc. All rights reserved
© 2016 Pearson Education, Ltd. All rights reserved.
A List Implementation that Uses An Array
A List Implementation that Links Data
© 2016 Pearson Education, Ltd. All rights reserved.
Presentation transcript:

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

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

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 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

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 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

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 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 … 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

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 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

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

Efficiency List Sorted List direct using list Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved 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)

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved 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