Danh sách liên kết dùng cấp phát động, co giãn tương ứng. Là tập hợp nút. Nút có hai phần: dữ liệu và liên kết. Biểu diễn:

Các thao tác trên danh sách liên kết
Ba loại thao tác:
- Chèn
- Xóa
- Duyệt
Xóa
Ví dụ xóa nút:
Xóa nút 2

Xóa nút 1

Xóa nút 3

Chương trình
Chương trình C xóa phần tử danh sách liên kết:
#include <stdio.h>
#include <stdlib.h>
struct Node{
int data;
struct Node *next;
};
void push(struct Node** head_ref, int new_data){
struct Node* new_node = (struct Node*) malloc(sizeof(struct Node));
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
void deleteNode(struct Node **head_ref, int position){
// Neu danh sach rong
if(*head_ref == NULL)
return;
struct Node* temp = *head_ref;
if(position == 0){
*head_ref = temp->next;
free(temp);
return;
}
for(int i = 0; temp != NULL && i < position - 1; i++)
temp = temp->next;
if(temp == NULL || temp->next == NULL)
return;
struct Node *next = temp->next->next;
free(temp->next); // Giai phong bo nho
temp->next = next;
}
void printList(struct Node *node){
while(node != NULL){
printf(" %d ", node->data);
node = node->next;
}
}
int main(){
struct Node* head = NULL;
push(&head, 7);
push(&head, 1);
push(&head, 3);
push(&head, 2);
push(&head, 8);
puts("Danh sach tao: ");
printList(head);
deleteNode(&head, 3);
puts("\n Danh sach sau xoa vi tri 3: ");
printList(head);
return 0;
}
Đầu ra
Kết quả chạy chương trình:
Danh sach tao: 8 2 3 1 7 Danh sach sau xoa vi tri 3: 8 2 3 7