DANH SÁCH LIÊN KẾT Linked List Nguyễn Xuân Vinh nguyenxuanvinh@hcmuaf.edu.vn CẤU TRÚC DỮ LIỆU DATA STRUCTURES [214331]... • Danh sách liên kết đơn Singly Linked List • Danh sách liên kết
Trang 1DANH SÁCH LIÊN KẾT
(Linked List)
Nguyễn Xuân Vinh nguyenxuanvinh@hcmuaf.edu.vn
CẤU TRÚC DỮ LIỆU DATA STRUCTURES
[214331]
Trang 2 Pros
Access quickly via array index
Easier to use
Cons
Fixed size: the size of the array is static
One block allocation
Complex position-based insertion/removal
Trang 3• A data structure consisting of a group of nodes which together represent a sequence a linear structure.
• Each node is composed of a data and a reference(*).
• Allows more efficient insertion or removal of elements from any position in the sequence.
• Reference of the last node point to null.
• Only need to handle the first (head) element.
Trang 4• Pros
– Flexibility: insert/delete from any position in constant time – No single allocation of memory needed
– Dynamic allocation: the size is not required to be known in advance
Cons
– There is no index to query element directly not allow random access to element
– Complex to use and access
– No constant time access to the elements
Question: How to get the last element in the list?
Trang 5• Normally, Linked List is a linear data structure
• However, the complex reference also be a non-linear structure such as Tree, Graph
Trang 6• Danh sách liên kết đơn (Singly Linked List)
• Danh sách liên kết kép (Doubly Linked List)
• Danh sách liên kết vòng (Circular Linked List)
Trang 7public class Node<T> {
private T data ;
private Node<T> next ;
public Node(T data, Node<T> next) {
this data = data;
this next = next;
}
public T getData() {
return data ;
}
public void setData(T data) {
this data = data;
}
public Node<T> getNext() {
return next ;
}
public void setNext(Node<T> next) {
this next = next;
}
public Node(T data) {
this data = data;
}
}
1) Duyệt các phần tử 2) Chèn them phần tử
Chèn vào đầu
Chèn vào giữa 3) Xóa phần tử
Xóa phần tử đầu
Xóa phần tử giữa
Trang 8Node head = ;
Node current = head;
while ((current = current.getNext()) !=
null ) {
System.out.println(current);
}
Trang 9Chèn vào đầu danh sách liên kết
Chèn vào giữa danh sách liên kết
Trang 10Xóa phần tử ở đầu danh sách
Xóa phần tử ở giữa danh sách
Trang 11• Trong danh sách liên kết mà mỗi nút có 2 liên kết trỏ, 1 tới nút liền trước, 1 tới nút liền sau
• Ưu điểm:
– Có thể duyệt theo cả hai chiều
Trang 12• Trong danh sách liên kết đơn, nút cuối cùng của danh sách trỏ tới nút đầu tiên
• Ưu điểm:
– Bất kỳ nút nào cũng có thể coi là đầu danh sách
• Nhược điểm:
– Không biết lúc nào là kết thúc của danh sách
Trang 131 LinkedList<T>
2 Node<T>
a) Node<T> in different class
b) Static inner class Node<T>
c) Non-static inner class Node<T>
Trang 14public class LinkedList<T> {
private Node<T> head ;
public LinkedList(Node<T> head) { this head = head;
}
public Node<T> getHead() {
return head ;
}
public void setHead(Node<T> head) { this head = head;
}
}
Trang 15public class Node<T> {
private T data ;
private Node<T> next ;
public Node(T data, Node<T> next) { this.data = data;
this.next = next;
}
public T getData() {
return data ;
}
public void setData(T data) {
this.data = data;
}
public Node<T> getNext() {
return next ;
}
public void setNext(Node<T> next) { this.next = next;
}
public Node(T data) {
this.data = data;
}
Trang 16public class LinkedList<T> {
private Node<T> head ;
public LinkedList(Node<T> head) {
this head = head;
}
public Node<T> getHead() {
return head ;
}
public void setHead(Node<T> head) {
this head = head;
}
private static class Node<T> {
private T data ;
private Node<T> next ;
public Node(T data, Node<T> next) {
this data = data;
this next = next;
}
}
}
Trang 17public class LinkedList<T> {
private Node<T> head ;
private String name;
public LinkedList(Node<T> head) {
this head = head;
}
public Node<T> getHead() {
return head ;
}
public void setHead(Node<T> head) {
this head = head;
}
private class Node<T> {
private T data ;
private Node<T> next ;
private String listName;
public Node(T data, Node<T> next) {
this data = data;
this next = next;
this listName = name;
}
}
}
Trang 18Operation Array Singly Linked List
Insert/Delete at beginning O(n) O(1) Insert/Delete at end O(1) O(n) Insert/Delete in middle O(1) search time + O(1)
Trang 19 Review Arrays
Introduce LinkedList
Pros and cons
Non-linear Linked List
Classification of Linked List
Các phép toán trên Linked List
Danh sách liên kết kép
Danh sách liên kết vòng
Cài đặt LinkedList
Trang 20HỎI ĐÁP