Professional Documents
Culture Documents
B06 DanhSachLienKet
B06 DanhSachLienKet
4.1 Giới
thiệu danh
sách liên
kết
4.2 Danh
sách liên kết
đơn 4.3 Danh
sách liên kết
mở rộng
chèn
0 1 2 3 4 n-2 n-1
Insert,
Delete
Định
nghĩa
Ứng Khai
dụng SLL báo
Thao
tác
info next
Node
i 1FF0 1FF4
pi = &i; *pi ? pi 1FF0
i 1FF0 1FF4
i = 10 or *pi = 10 *pi 10 pi 1FF0
A1 A2 A3 An
null
Node Tail Node
pHead
pHead
Danh sách rỗng
null
Cấu trúc dữ liệu - Khoa CNTT 10
SLL – Minh họa
VD:
Địa chỉ
600 300
‘L’ 300 ‘D’ null
typedef struct
{
char Ten[30];
char MaSv[10];
int Gioitinh;
float diem;
} SinhVien;
2. {
3. *pHead = NULL;
4. }
node node
pHead pHead
node node
p p
node node
p p
pHead pHead
p p
pHead pHead
p
Cấu trúc dữ liệu - Khoa CNTT 31
4.2.3 SLL – Các thao tác
DeleteFirst: loại bỏ node đầu tiên của danh sách
1. void DeleteFirst(NodePtr &pHead)
2. { NodePtr p;
3. if (IsEmpty(pHead))
4. cout<<“List is empty!”;
5. else
6. { p = pHead;
7. pHead = pHead->next;
8. delete p;
9. }
10.}
pHead
q p p
pHead q
pHead q
p
q pHead
p
Cấu trúc dữ liệu - Khoa CNTT 35
4.2.3 SLL – Các thao tác
DeleteAfter: xoá node sau node p trong danh sách
1. void DeleteAfter(NodePtr &pHead, NodePtr &p)
2. { NodePtr q;
3. if (p->next == NULL)
4. cout<<“Cannot delete node!”;
5. else
6. {
7. q = p->next; //q tro nut sau p
8. p->next = q->next;
9. delete q; //giai phong nut sau p
10. }
11.}
Cấu trúc dữ liệu - Khoa CNTT 36
4.2.3 SLL – Các thao tác
Loại bỏ phần tử trước nút p - DeleteBefore
pHead r q
p
pHead r q
p
Cấu trúc dữ liệu - Khoa CNTT 37
4.2.3 SLL – Các thao tác
Loại bỏ phần tử được trỏ bởi p - DeleteNode
pHead
q
pHead p
q
p
Cấu trúc dữ liệu - Khoa CNTT 38
4.2.3 SLL – Các thao tác
DeleteNode: xoá node p trong danh sách
1. void DeleteNode(NodePtr &pHead, NodePtr &p)
2. { if (p == NULL)
3. cout<<“Cannot delete node!”;
4. else
5. { NodePtr q = pHead;
6. while (q->next !=p)
7. q = q->next;
8. q->next = p->next;
9. delete p;
10. }
11.}
Cấu trúc dữ liệu - Khoa CNTT 39
4.2.3 SLL – Các thao tác
DeleteAll: xoá toàn bộ danh sách
1. void DeleteAll(NodePtr &pHead)
2. {
3. NodePtr p;
4. while (pHead!=NULL)
5. {
6. p = pHead;
7. pHead = pHead -> next;
8. delete p;
9. }
10.}
4 10 -2 3 6 0
4 10 -2 3 6 0
-6 10 8 6 2 3 5 1
C
C(x) = A(x) + B(x) = -2x10 + 8x6 +5x +6
-2 10 8 6 5 1 6 0
A. q = pHead; B. q = pHead;
while (q -> next != p) while (q != p)
q = q -> next; q = q -> next;
delete tmp;
A. p++;
B. p = p->next;
C. p->next = p->next->next
A. r->next=p; C. p = r;
r->next = NULL;
B. r -> next = p; D. p->next=r;
p = r;
60
Câu hỏi củng cố bài
Câu hỏi trắc nghiệm
2. Làm thế nào để xóa nút ở đầu danh sách nối đơn có nút đầu
được trỏ bởi p?
A. p = p->next; C. NodePtr r;
r = p;
p = p->next;
delete p;
B. NodePtr r; D. NodePtr r;
r = p; r = p;
p=p->next; r = r->next; 61
Câu hỏi củng cố bài
Câu hỏi trắc nghiệm
3. Làm thế nào để thêm nút r vào cuối danh sách nối đơn có nút
đầu trỏ bởi p?
temp = p; temp = p;
temp->next = r; temp->next=r;
r->next=NULL; }
Cơ bản
Các thao Init, IsEmpty, ShowList
tác Bổ sung
InsertFirst, InsertLast, InsertAfter, InsertBefore
Loại bỏ
DeleteFirst, DeleteLast, DeleteAfter,
DeleteBefore, DeleteNode, DeleteAll
Khác
Search, Sort
pHead d=0
int d = 0;
p NodePtr p = pHead;
while ( p != NULL)
{ d++;
p = p -> next;
}
return d;
q node
p
pHead
d=k
q p
Cấu trúc dữ liệu - Khoa CNTT 68
SLL – Bài tập
Insert_k: thêm node có nội dung x vào cuối ds
void Insert_k(NodePtr &pHead, int k, int x)
{ //tao nut moi
NodePtr node;
node = new NODE;
node->info = x;
//noi vao danh sach
int d = 0; //bien d de dem so nut
NodePtr p, q; //con tro duyet ds
p = pHead -> next; q = pHead;
pHead q p
else
Cấu trúc dữ liệu - Khoa CNTT 72
SLL– Bài tập
Loại bỏ nút có giá trị y trong danh sách
{ NodePtr q = pHead;
while (q -> next != p)
q = q -> next;
q -> next = p -> next;
delete p;
}
}
p
r q
pHead
p
Cấu trúc dữ liệu - Khoa CNTT 78
SLL – Bài tập
void DeleteBefore(NodePtr &pHead, NodePtr &p)
{ if (p == NULL)
cout<<“Cannot delete node!”;
else
{ NodePtr q, r; //con tro de duyet ds
r = pHead; q = pHead -> next;
while ( q -> next != p)
{ q = q -> next; r = r -> next; }
r -> next = p;
delete q;
}
}
Cấu trúc dữ liệu - Khoa CNTT 79
SLL – Bài tập
Tìm in ra các phần tử chia hết cho 7
void ShowDivide(NodePtr pHead)
{
NodePtr p; //p là con trỏ để duyệt
p = pHead; //duyệt từ đầu danh sách
while (p != NULL && p -> info % 7 == 0)
{
ShowNode(p); //tác động lên nút
p = p->next; //chuyển nút sau
}
}