Presentation is loading. Please wait.

Presentation is loading. Please wait.

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,

Similar presentations


Presentation on theme: "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,"— Presentation transcript:

1 LinkedLists CS445 Data Structures

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

3 LinkedList of Chairs

4 LinkedList Head

5 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

6 Instantiating a LinkedList LinkedList Examples: LinkedList bigLst = new LinkedList (); LinkedList stringLst = new LinkedList ();

7 LinkedList Functionality add(aObject) – add aObject to end of list add(new BigInteger(234512909345689131321122110863523)) 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

8 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() + "\"");

9 Maps

10 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 231-45-3278Jones, William 229-03-1259Keller, Mary 256-41-0920Madden, Matt 209-60-8877Jones, William

11 HashMap Table consisting of two columns Column 1 contains the keys Column 2 contains the values 231-45-3278Jones, William 229-03-1259Keller, Mary 256-41-0920Madden, Matt 200-60-8877Jones, William KeysValues Like a one-dimensional array, where a key acts like an index to access the corresponding value

12 Instantiating a HashMap HashMap Examples: HashMap hmap = new HashMap (); HashMap > amap = new HashMap >();

13 HashMap Functionality get(aKey) – returns corresponding value get(229-03-1259) = Keller, Mary put(aKey, aValue) – inserts a pair put(229-03-1259, Keller, Mary) containsKey(aKey) – does key belong containsValue(aValue) – does value belong remove(aKey) – remove a pair whose key is aKey

14 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

15 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()); }

16 TreeMap Sorted table with respect to keys Column 1 contains the keys Column 2 contains the values 200-60-8877Jones, William 229-03-1259Keller, Mary 231-45-3278Jones, William 256-41-0920Madden, Matt KeysValues sorted

17 Instantiating a TreeMap TreeMap Examples: TreeMap hmap = new TreeMap (); TreeMap amap = new TreeMap ();

18 TreeMap Functionality get(aKey) – returns corresponding value get(229-03-1259) = Keller, Mary put(aKey, aValue) – inserts a pair put(229-03-1259, Keller, Mary) containsKey(aKey) – does key belong containsValue(aValue) – does value belong remove(aKey) – remove a pair whose key is aKey

19 TreeMap Class values() – returns the set of all values keySet() - returns the set of all keys

20 TreeMap as a Tree A TreeMap is an implementation of a data structure called a binary search tree 231-45-3278Jones, William 229-03-1259Keller, Mary256-41-0920Madden, Matt 200-60-8877Jones, William


Download ppt "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,"

Similar presentations


Ads by Google