LinkedLists - PowerPoint PPT Presentation

1 / 20
About This Presentation
Title:

LinkedLists

Description:

Jones, William. HashMap. Table consisting of two columns. Column 1 ... Jones, ... Jones, William. 256-41-0920. Madden, Matt. Keys. Values. sorted ... – PowerPoint PPT presentation

Number of Views:52
Avg rating:3.0/5.0
Slides: 21
Provided by: georgen1
Category:

less

Transcript and Presenter's Notes

Title: LinkedLists


1
LinkedLists
  • CS445Data 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, Examples45, 75,
    31, 52, 27Bob, Nick, Anne, Mary, Larry, Celia

0
1
2
3
4
5
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

next node
object
6
Instantiating a LinkedList
  • LinkedListltEgt
  • Examples
  • LinkedListltBigIntegergt bigLst new
    LinkedListltBigIntegergt()
  • LinkedListltStringgt stringLst new
    LinkedListltStringgt()

7
LinkedList Functionality
  • add(aObject) add aObject to end of list
    add(new BigInteger(234512909345689131321122110863
    523))
  • 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
LinkedListltStringgt list new LinkedListltStringgt()
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.pri
ntln("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.

231-45-3278
Jones, William
229-03-1259
Keller, Mary
256-41-0920
Madden, Matt
209-60-8877
Jones, William
values
keys
11
HashMap
  • Table consisting of two columns
  • Column 1 contains the keys
  • Column 2 contains the values

Keys
Values
231-45-3278
Jones, William
229-03-1259
Keller, Mary
256-41-0920
Madden, Matt
200-60-8877
Jones, William
Like a one-dimensional array, where a key acts
like an index to access the corresponding value
12
Instantiating a HashMap
  • HashMapltK,Egt
  • Examples
  • HashMapltString, Stringgt hmap new
    HashMapltString, Stringgt()
  • HashMapltString, LinkedListltStringgtgt amap
    new HashMapltString,
    LinkedListltStringgtgt()

13
HashMap Functionality
  • get(aKey) returns corresponding value
    get(229-03-1259) Keller, Mary
  • put(aKey, aValue) inserts a ltkey,valuegt 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)
HashMapltString,Doublegt table new
HashMapltString,Doublegt()
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

Keys
Values
200-60-8877
Jones, William
229-03-1259
Keller, Mary
sorted
231-45-3278
Jones, William
256-41-0920
Madden, Matt
17
Instantiating a TreeMap
  • TreeMapltK,Egt
  • Examples
  • TreeMapltString, Stringgt hmap new
    TreeMapltString, Stringgt()
  • TreeMapltString, Doublegt amap new
    TreeMapltString, Doublegt()

18
TreeMap Functionality
  • get(aKey) returns corresponding value
    get(229-03-1259) Keller, Mary
  • put(aKey, aValue) inserts a ltkey,valuegt 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-3278
Jones, William
229-03-1259
Keller, Mary
256-41-0920
Madden, Matt
200-60-8877
Jones, William
Write a Comment
User Comments (0)
About PowerShow.com