Tải bản đầy đủ (.pptx) (20 trang)

Bài giảng cấu trúc dữ liệu chương 5 nguyễn xuân vinh

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (325.86 KB, 20 trang )

GV: NGUYỄN XUÂN VINH

CẤU TRÚC DỮ LIỆU
DATA STRUCTURES
[214331]

MÔN: CẤU TRÚC DỮ LIỆU

DANH SÁCH LIÊN KẾT
(Linked List)

Nguyễn Xuân Vinh

1

/20

12/3/15




GV: NGUYỄN XUÂN VINH
MÔN: CẤU TRÚC DỮ LIỆU
12/3/15
/20
2

Review Arrays

 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


GV: NGUYỄN XUÂN VINH

 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.

3

/20

12/3/15

MÔN: CẤU TRÚC DỮ LIỆU

Linked List (Singly Linked List)

(*) There might be two references, references can link to previous or next element.



GV: NGUYỄN XUÂN VINH
MÔN: CẤU TRÚC DỮ LIỆU

Pros and cons

 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.

4

/20

12/3/15

Question: How to get the last element in the list?


GV: NGUYỄN XUÂN VINH
MÔN: CẤU TRÚC DỮ LIỆU
12/3/15
/20
5


Non-linear Linked List (Cấu trúc phi tuyến tính)

 Normally, Linked List is a linear data structure.
 However, the complex reference also be a non-linear structure such as Tree, Graph.


GV: NGUYỄN XUÂN VINH

Classification of Linked List

 Danh sách liên kết đơn (Singly Linked List)

6

/20

12/3/15

MÔN: CẤU TRÚC DỮ LIỆU

 Danh sách liên kết kép (Doubly Linked List)

 Danh sách liên kết vòng (Circular Linked List)


GV: NGUYỄN XUÂN VINH
MÔN: CẤU TRÚC DỮ LIỆU
12/3/15
/20
7


Các phép toán trên Linked List
public 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)
2)



3)



Duyệt các phần tử
Chèn them phần tử
Chèn vào đầu
Chèn vào giữa
Xóa phần tử
Xóa phần tử đầu
Xóa phần tử giữa


GV: NGUYỄN XUÂN VINH

1) Duyệt
Node head = ...;
Node current = head;
while ((current = current.getNext()) != null) {
System.out.println(current);

8

/20

12/3/15

MÔN: CẤU TRÚC DỮ LIỆU

}



GV: NGUYỄN XUÂN VINH

2) Chèn phần tử

MÔN: CẤU TRÚC DỮ LIỆU

Chèn vào đầu danh sách liên kết

9

/20

12/3/15

Chèn vào giữa danh sách liên kết


Xóa phần tử ở đầu danh sách

MÔN: CẤU TRÚC DỮ LIỆU

GV: NGUYỄN XUÂN VINH

3) Xóa phần tử

10

/20


12/3/15

Xóa phần tử ở giữa danh sách


GV: NGUYỄN XUÂN VINH
MÔN: CẤU TRÚC DỮ LIỆU
12/3/15
/20
11

Danh sách liên kết kép (Doubly Linked List)

 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.


GV: NGUYỄN XUÂN VINH

Danh sách liên kết vòng (Circular Linked List)

 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

12

/20


12/3/15

MÔN: CẤU TRÚC DỮ LIỆU

A

B

 Ư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

C

D

E


GV: NGUYỄN XUÂN VINH
MÔN: CẤU TRÚC DỮ LIỆU
12/3/15
/20
13

LinkedList Example

1. LinkedList<T>
2. Node<T>

a) Node<T> in different class
b) Static inner class Node<T>
c) Non-static inner class Node<T>


GV: NGUYỄN XUÂN VINH
MÔN: CẤU TRÚC DỮ LIỆU
12/3/15
/20
14

1. LinkedList<T>
public 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;
}
}


GV: NGUYỄN XUÂN VINH
MÔN: CẤU TRÚC DỮ LIỆU
12/3/15
/20

15

2. a) Node<T>
public 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;
}
}


GV: NGUYỄN XUÂN VINH
MÔN: CẤU TRÚC DỮ LIỆU

12/3/15
/20
16

2. b) Static inner class Node<T>
public 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;
}
}
}


GV: NGUYỄN XUÂN VINH
MÔN: CẤU TRÚC DỮ LIỆU


2. c) Non-static inner class Node<T>
public 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;

12/3/15

this.listName = name;
}

/20

}


17

}


GV: NGUYỄN XUÂN VINH

Complexity: Array vs Linked List

18

/20

12/3/15

MÔN: CẤU TRÚC DỮ LIỆU

Operation

Array

Singly Linked List

Indexing

O(1)

O(n)

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)


GV: NGUYỄN XUÂN VINH
MÔN: CẤU TRÚC DỮ LIỆU
12/3/15
/20
19

Tóm tắt

 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


20

/20

12/3/15
MÔN: CẤU TRÚC DỮ LIỆU

GV: NGUYỄN XUÂN VINH

HỎI ĐÁP



×