Download ppt

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
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:
0
45, 75, 31, 52, 27
Bob, Nick, Anne, Mary, Larry, Celia
1
2
3
4
5
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
next node
object
Instantiating a LinkedList
• LinkedList<E>
• Examples:
LinkedList<BigInteger> bigLst = new LinkedList<BigInteger>();
LinkedList<String> stringLst = new LinkedList<String>();
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
LinkedList<String> list = new LinkedList<String>();
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 " + "\"" +
System.out.println("last item in list is " + "\"" +
list.getFirst() + "\"");
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.
231-45-3278
229-03-1259
256-41-0920
209-60-8877
keys
Jones, William
Keller, Mary
Madden, Matt
Jones, William
values
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
Instantiating a HashMap
• HashMap<K,E>
• Examples:
HashMap<String, String> hmap = new HashMap<String, String>();
HashMap<String, LinkedList<String>> amap =
new HashMap<String, LinkedList<String>>();
HashMap Functionality
• get(aKey) –
returns corresponding value
get(“229-03-1259”) = “Keller, Mary”
• put(aKey, aValue) –
inserts a <key,value> pair
put(“229-03-1259”, “Keller, Mary”)
• containsKey(aKey) –
does key belong
• containsValue(aValue) –
• remove(aKey) –
does value belong
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<String,Double> table = new HashMap<String,Double>();
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"))
if(!table.containsKey("Donna"))
table.put("Al",28.0);
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
sorted
Keys
Values
200-60-8877
Jones, William
229-03-1259
Keller, Mary
231-45-3278
Jones, William
256-41-0920
Madden, Matt
Instantiating a TreeMap
• TreeMap<K,E>
• Examples:
TreeMap<String, String> hmap = new TreeMap<String, String>();
TreeMap<String, Double> amap = new TreeMap<String, Double>();
TreeMap Functionality
• get(aKey) –
returns corresponding value
get(“229-03-1259”) = “Keller, Mary”
• put(aKey, aValue) –
inserts a <key,value> pair
put(“229-03-1259”, “Keller, Mary”)
• containsKey(aKey) –
does key belong
• containsValue(aValue) –
• remove(aKey) –
does value belong
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
231-45-3278
229-03-1259
200-60-8877
Keller, Mary
Jones, William
Jones, William
256-41-0920
Madden, Matt
Related documents