LinkedLists CS445 Data Structures
What is a list? A list is an object that is a sequence of objects. The positions in the list are numbered 0, 1, 2, … or may be numbered 1, 2, 3, … Examples: 45, 75, 31, 52, 27 Bob, Nick, Anne, Mary, Larry, Celia
LinkedList of Chairs
LinkedList Head
What is a LinkedList? A LinkedList is one of the many ways to implement a list Each object is stored in a node of the form object next node
Instantiating a LinkedList LinkedList Examples: LinkedList bigLst = new LinkedList (); LinkedList stringLst = new LinkedList ();
LinkedList Functionality add(aObject) – add aObject to end of list add(new BigInteger( )) add(aObject, position) – add aObject at the position size() – size of list contains(aObject) – does aObject belong to list get(position) – get object at position in list remove() – remove object at beginning of list remove(position) – remove object from position indexOf(aObject) - remove object at position
LinkedList list = new LinkedList (); list.add("Bob"); list.add("Al"); list.add("Carl"); list.add(1,"Sela"); list.add(0,"Morgan"); list.add(3,"Mona"); System.out.println("list = " + list); System.out.println("size = " + list.size() ); if( list.contains("Mona " ) ){ list.remove(list.indexOf("Mona")); System.out.println("deleting " + "\"Mona\""); } System.out.println("list = " + list); System.out.println("first item in list is " + "\"" + list.getFirst() + "\""); System.out.println("last item in list is " + "\"" + list.getLast() + "\"");
Maps
What is a Map? An object that maps keys to values A map cannot contain duplicate keys A map may contain duplicate values Each key can map to at most one value. keys values Jones, William Keller, Mary Madden, Matt Jones, William
HashMap Table consisting of two columns Column 1 contains the keys Column 2 contains the values Jones, William Keller, Mary Madden, Matt Jones, William KeysValues Like a one-dimensional array, where a key acts like an index to access the corresponding value
Instantiating a HashMap HashMap Examples: HashMap hmap = new HashMap (); HashMap > amap = new HashMap >();
HashMap Functionality get(aKey) – returns corresponding value get( ) = Keller, Mary put(aKey, aValue) – inserts a pair put( , Keller, Mary) containsKey(aKey) – does key belong containsValue(aValue) – does value belong remove(aKey) – remove a pair whose key is aKey
HashMap Class values() – returns the set of all values keySet() - returns the set of all keys A HashMap is an implementation of a data structure called a hash table
import java.util.*; public class testHashMap{ public static void main(String[] args){ HashMap table = new HashMap (); table.put("Bob",28.0); table.put("Al",28.0); table.put("Carl",38.0); System.out.println(table.get("Al")); System.out.println(table.get("Carl")); if(!table.containsKey("Al")) table.put("Al",28.0); if(!table.containsKey("Donna")) table.put("Donna",24.0); System.out.println(table.keySet()); System.out.println(table.values()); }
TreeMap Sorted table with respect to keys Column 1 contains the keys Column 2 contains the values Jones, William Keller, Mary Jones, William Madden, Matt KeysValues sorted
Instantiating a TreeMap TreeMap Examples: TreeMap hmap = new TreeMap (); TreeMap amap = new TreeMap ();
TreeMap Functionality get(aKey) – returns corresponding value get( ) = Keller, Mary put(aKey, aValue) – inserts a pair put( , Keller, Mary) containsKey(aKey) – does key belong containsValue(aValue) – does value belong remove(aKey) – remove a pair whose key is aKey
TreeMap Class values() – returns the set of all values keySet() - returns the set of all keys
TreeMap as a Tree A TreeMap is an implementation of a data structure called a binary search tree Jones, William Keller, Mary Madden, Matt Jones, William