[CTDL] Danh Sách Liên Kết đơn - Linked List - Phần 3. Xóa Node
🔥CHỌN LỌC TOP NHỮNG KHOÁ HỌC LẬP TRÌNH ONLINE NHIỀU NGƯỜI THEO HOC TẠI ĐÂY🔥
Chào ace, bài này chúng ta sẽ tìm hiểu về Xoá node trong Linked List(danh sách liên kết đơn) trong series tự học về cấu trúc dữ liệu và giải thuật, sau đây cafedev sẽ giới thiệu và chia sẻ chi tiết(khái niệm, độ phức tạp, ứng dụng của nó, code ví dụ…) về nó thông qua các phần sau.
Trong các bài học trước, chúng ta đã tìm hiểu cơ bản về linked list và cách để chèn thêm một node mới vào linked list. Ở bài này chúng ta sẽ tìm hiểu về thao tác xóa một node khỏi linked list. Cho trước một ‘key’, hãy xóa đi sự hiện diện đầu tiên của key này trong linked list.
Để xóa một node khỏi linked list, chúng ta cần làm các bước sau:
1. Tìm ra node nằm trước node cần xóa
2. Thay đổi con trỏ next của node nằm trước node cần xóa đã tìm được ở bước 1
3. Giải phóng bộ nhớ của node cần xóa

Bởi vì mọi node của linked list đều được cấp phát động bằng hàm malloc() trong C, chúng ta sẽ cần phải gọi hàm free() để giải phóng bộ nhớ đã được cấp phát cho node cần xóa bỏ.
CODE VÍ DỤ
C/C++
// A complete working C program to demonstrate deletion in singly // linked list #include <stdio.h> #include <stdlib.h> // A linked list node struct Node { int data; struct Node *next; }; /* Given a reference (pointer to pointer) to the head of a list and an int, inserts a new node on the front of the list. */ 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; } /* Given a reference (pointer to pointer) to the head of a list and a key, deletes the first occurrence of key in linked list */ void deleteNode(struct Node **head_ref, int key) { // Store head node struct Node* temp = *head_ref, *prev; // If head node itself holds the key to be deleted if (temp != NULL && temp->data == key) { *head_ref = temp->next; // Changed head free(temp); // free old head return; } // Search for the key to be deleted, keep track of the // previous node as we need to change 'prev->next' while (temp != NULL && temp->data != key) { prev = temp; temp = temp->next; } // If key was not present in linked list if (temp == NULL) return; // Unlink the node from linked list prev->next = temp->next; free(temp); // Free memory } // This function prints contents of linked list starting from // the given node void printList(struct Node *node) { while (node != NULL) { printf(" %d ", node->data); node = node->next; } } /* Drier program to test above functions*/ int main() { /* Start with the empty list */ struct Node* head = NULL; push(&head, 7); push(&head, 1); push(&head, 3); push(&head, 2); puts("Created Linked List: "); printList(head); deleteNode(&head, 1); puts("\nLinked List after Deletion of 1: "); printList(head); return 0; }Java
// A complete working Java program to demonstrate deletion in singly // linked list class LinkedList { Node head; // head of list /* Linked list Node*/ class Node { int data; Node next; Node(int d) { data = d; next = null; } } /* Given a key, deletes the first occurrence of key in linked list */ void deleteNode(int key) { // Store head node Node temp = head, prev = null; // If head node itself holds the key to be deleted if (temp != null && temp.data == key) { head = temp.next; // Changed head return; } // Search for the key to be deleted, keep track of the // previous node as we need to change temp.next while (temp != null && temp.data != key) { prev = temp; temp = temp.next; } // If key was not present in linked list if (temp == null) return; // Unlink the node from linked list prev.next = temp.next; } /* Inserts a new Node at front of the list. */ public void push(int new_data) { Node new_node = new Node(new_data); new_node.next = head; head = new_node; } /* This function prints contents of linked list starting from the given node */ public void printList() { Node tnode = head; while (tnode != null) { System.out.print(tnode.data+" "); tnode = tnode.next; } } /* Drier program to test above functions. Ideally this function should be in a separate user class. It is kept here to keep code compact */ public static void main(String[] args) { LinkedList llist = new LinkedList(); llist.push(7); llist.push(1); llist.push(3); llist.push(2); System.out.println("\nCreated Linked list is:"); llist.printList(); llist.deleteNode(1); // Delete node with data 1 System.out.println("\nLinked List after Deletion of 1:"); llist.printList(); } }Python 3
# Python program to delete a node from linked list # Node class class Node: # Constructor to initialize the node object def __init__(self, data): self.data = data self.next = None class LinkedList: # Function to initialize head def __init__(self): self.head = None # Function to insert a new node at the beginning def push(self, new_data): new_node = Node(new_data) new_node.next = self.head self.head = new_node # Given a reference to the head of a list and a key, # delete the first occurence of key in linked list def deleteNode(self, key): # Store head node temp = self.head # If head node itself holds the key to be deleted if (temp is not None): if (temp.data == key): self.head = temp.next temp = None return # Search for the key to be deleted, keep track of the # previous node as we need to change 'prev.next' while(temp is not None): if temp.data == key: break prev = temp temp = temp.next # if key was not present in linked list if(temp == None): return # Unlink the node from linked list prev.next = temp.next temp = None # Utility function to print the linked LinkedList def printList(self): temp = self.head while(temp): print (" %d" %(temp.data)), temp = temp.next # Driver program llist = LinkedList() llist.push(7) llist.push(1) llist.push(3) llist.push(2) print ("Created Linked List: ") llist.printList() llist.deleteNode(1) print ("\nLinked List after Deletion of 1:") llist.printList()Kết quả in ra là:
Created Linked List: 2 3 1 7 Linked List after Deletion of 1: 2 3 7Nguồn và Tài liệu tiếng anh tham khảo:
- w3schools
- Geeksforgeeks
- codechef
- dev.to
Tài liệu từ cafedev:
- Full series tự học Cấu trúc dữ liệu và giải thuật từ cơ bản tới nâng cao tại đây nha.
- Ebook về Cấu trúc dữ liệu và giải thuật tại đây.
- Các series tự học lập trình khác
Nếu bạn thấy hay và hữu ích, bạn có thể tham gia các kênh sau của cafedev để nhận được nhiều hơn nữa:
- Group Facebook
- Fanpage
- Youtube
- Trang chủ
Chào thân ái và quyết thắng!
Đăng ký kênh youtube để ủng hộ Cafedev nha các bạn, Thanks you!
Từ khóa » Xoa Trong Dslk
-
Xóa Node Trong Danh Sách Liên Kết đơn - Freetuts
-
Xóa Phần Tử Trong Danh Sách Liên Kết đơn C/C++ | Lập Trình Từ Đầu
-
[Basic-DSAA] Danh Sách Liên Kết đơn - Xóa Phần Tử. - CodeLearn
-
Xóa Phần Tử Trong Danh Sách Liên Kết đơn - Hỏi Đáp
-
Cách Xóa 1 Node Trong Danh Sách Liên Kết đơn - Dạy Nhau Học
-
Xóa Phàn Tử Cuối Trong Danh Sách Liên Kết đơn - Dạy Nhau Học
-
Xóa Một Phần Tử Từ Danh Sách Liên Kết đơn Trong C
-
Xoá Bỏ Tất Cả Các Node Có Giá Trị Bằng X Khỏi Danh Sách Liên Kết đơn.
-
Xoá Phần Tử Trong Danh Sách Liên Kết - Deft Blog
-
Toàn Tập Về Danh Sách Liên Kết | Thư Viện Hướng Dẫn
-
10 Xoá Các Số âm Trong DSLK: - Tài Liệu Text - 123doc
-
Danh Sách Liên Kết đơn – Single Linked List - Lập Trình Không Khó
-
Danh Sách Liên Kết - Xóa Phần Tử Cuối Cùng Của Danh Sách
-
Xóa Node Trong Danh Sách Liên Kết đôi