Professional Documents
Culture Documents
2
Cấu trúc dữ liệu
3
Linked List
4
Linked List
15 10
5
Linked List
firstNode lastNode
H D ... Q
6
Cài đặt Linked List
public LinkedList()
{
firstNode = lastNode = null;
}
11
Mô tả insertAtFront
(a ) firstNode
7 11
new ListNode
12
(b ) firstNode
7 11
new ListNode
12
12
Mô tả insertAtBack
12 7 11 5
12 7 11 5
13
Mô tả removeFromFront
(a ) firstNode lastNode
12 7 11 5
(b ) firstNode lastNode
12 7 11 5
removeItem
14
Mô tả removeFromBack
(a ) firstNode lastNode
12 7 11 5
12 7 11 5
removeItem
15
Sử dụng Linked List
list.removeFromFront();
list.removeFromBack();
list.print();
}
}
16
Stack
17
Cài đặt Stack
18
Sử dụng Stack
19
Queue
20
Cài đặt Queue
21
Sử dụng Queue
22
Tree
Nút trong
Nút lá
23
Binary Search Tree
24
Binary Search Tree
47
11 43 65 93
7 17 31 44 68
25
Cài đặt Binary Search Tree
28
Cài đặt Binary Search Tree
29
Sử dụng Binary Search Tree
31
Collection
32
Collections Framework
33
Collections Framework
34
Collections Framework
35
Interfaces
<<interface>> <<interface>>
Collection Map
<<interface>>
SortedSet
36
Giao tiếp Collection
38
Giao tiếp Set
39
Giao tiếp SortedSet
40
Duyệt collection
Collection c;
41
Duyệt collection
42
Giao tiếp Map
43
Giao tiếp Map
44
Giao tiếp SortedMap
45
Implementations
46
Implementations
LinkedList
List
ArrayList
HashSet
Set LinkedHashSet
SortedSet TreeSet
HashMap
Map LinkedHashMap
SortedMap TreeMap
47
Mô tả các cài đặt
48
Mô tả các cài đặt
49
Ví dụ 1: TreeSet
50
Ví dụ 2: Student Set
52
Ví dụ 2: Student Set
53
Ví dụ 2: Student Set
{
public int compare(Object a, Object b)
{
Student itemA = (Student) a;
Student itemB = (Student) b;
double scoreA = itemA.getScore();
double scoreB = itemB.getScore();
if ( scoreA < scoreB )
return -1;
else
return 1;
}
}); // end of inner class
sortByScore.addAll(stu);
System.out.println(sortByScore);
}
}
54
Ví dụ 3: HashMap
// remove an entry
phoneDir.remove("8584490");
55
Ví dụ 3: HashMap
// replace an entry
phoneDir.put("7290028", new String("International Relation"));
// look up a value
System.out.println(phoneDir.get("5581814"));
// iterate through all entries
Set entries = phoneDir.entrySet();
Iterator iter = entries.iterator();
while (iter.hasNext())
{
Map.Entry entry = (Map.Entry) iter.next();
String key = (String) entry.getKey();
String value = (String) entry.getValue();
System.out.println("key=" + key + ", value=" + value);
}
}
}
56
Các lớp bao
57
Algorithms
58
Ví dụ: Trộn bài
import java.util.*;
public class MyShuffleTest
{
public static void main(String[] args)
{
List numbers = new ArrayList(52);
for (int i = 1; i <= 52; i++)
numbers.add(new Integer(i));
System.out.println("Before shuffling:" + numbers + "\n");
Collections.shuffle(numbers);
System.out.println("After shuffling:" + numbers + "\n");
}
}
59
Collections Framework
Legacy Implementations
Là các lớp cũ được cài đặt bổ sung thêm các
collection interface.
Vector: Có thể thay bằng ArrayList
Hastable: Có thể thay bằng HashMap
Abstract Implementations
Là các lớp trừu tượng đã cài đặt các collection
interface mà ta có thể kế thừa để tạo ra các
collection mới.
AbstractCollection, AbstractSet, AbstractList...
60
Bài tập
61
Bài tập
62