Contents
Bạn đang tìm kiếm từ khóa Delete smaller nodes in linked list C++ được Cập Nhật vào lúc : 2022-01-18 19:12:18 . Với phương châm chia sẻ Bí quyết về trong nội dung bài viết một cách Chi Tiết 2022. Nếu sau khi Read tài liệu vẫn ko hiểu thì hoàn toàn có thể lại Comment ở cuối bài để Mình lý giải và hướng dẫn lại nha.
Delete a given node from the linked list.
Nội dung chính
Linked List : 10 20 30 NULL
Input
20
Output
10 30 NULL
Input
30
Output
10 20 NULL
1. If the head node has the given key,
make the head node points to the second node and không lấy phí its memory.
2. Otherwise,
From the current node, check whether the next node has the given key
if yes, make the current->next = current->next->next and không lấy phí the memory.
else, update the current node to the next and do the above process (from step 2) till the last node.
void deleteNode(struct node **head, int key)
//temp is used to freeing the memory
struct node *temp;
//key found on the head node.
//move to head node to the next and không lấy phí the head.
if(*head->data == key)
temp = *head; //backup the head to không lấy phí its memory
*head = (*head)->next;
không lấy phí(temp);
Visual Representation
Let’s delete data 10 (head node).
1. Make the head points to the next node.
2. Free the head node’s memory.
3. Finally, the new linked list.
void deleteNode(struct node **head, int key)
//temp is used to freeing the memory
struct node *temp;
//key found on the head node.
//move to head node to the next and không lấy phí the head.
if((*head)->data == key)
temp = *head; //backup to không lấy phí the memory
*head = (*head)->next;
không lấy phí(temp);
else
struct node *current = *head;
while(current->next != NULL)
//if yes, we need to delete the current->next node
if(current->next->data == key)
temp = current->next;
//node will be disconnected from the linked list.
current->next = current->next->next;
không lấy phí(temp);
break;
//Otherwise, move the current node and proceed
else
current = current->next;
Visual Representation
Let’s delete data 20.
1. Make the current node points to the head node. (current => data = 10).
2. current => next. (current=>next=>data = 20).
3. current => next => next. (current=>next=>next=>data = 30).
4. We have to remove the node 20. Since current => next = 20 we can remove the node by setting current => next = current =>next => next. And then không lấy phí the node.
5. Finally, the new linked list.
Example
/*
* Program: Deleting a node in the linked list
* Language: C
*/
#include
#include
struct node
int data;
struct node *next;
;
void addLast(struct node **head, int val)
//create a new node
struct node *newNode = malloc(sizeof(struct node));
newNode->data = val;
newNode->next = NULL;
//if head is NULL, it is an empty list
if(*head == NULL)
*head = newNode;
//Otherwise, find the last node and add the newNode
else
struct node *lastNode = *head;
//last node’s next address will be NULL.
while(lastNode->next != NULL)
lastNode = lastNode->next;
//add the newNode the end of the linked list
lastNode->next = newNode;
void deleteNode(struct node **head, int key)
//temp is used to freeing the memory
struct node *temp;
//key found on the head node.
//move to head node to the next and không lấy phí the head.
if((*head)->data == key)
temp = *head; //backup to không lấy phí the memory
*head = (*head)->next;
không lấy phí(temp);
else
struct node *current = *head;
while(current->next != NULL)
//if yes, we need to delete the current->next node
if(current->next->data == key)
temp = current->next;
//node will be disconnected from the linked list.
current->next = current->next->next;
không lấy phí(temp);
break;
//Otherwise, move the current node and proceed
else
current = current->next;
void printList(struct node *head)
struct node *temp = head;
//iterate the entire linked list and print the data
while(temp != NULL)
printf(“%d ->”, temp->data);
temp = temp->next;
printf(“NULLn”);
int main()
struct node *head = NULL;
addLast(&head,10);
addLast(&head,20);
addLast(&head,30);
printf(“Linked List Elements:n”);
printList(head);
//delete first node
deleteNode(&head,10);
printf(“Deleted 10. The New Linked List:n”);
printList(head);
//delete last node
deleteNode(&head,30);
printf(“Deleted 30. The New Linked List:n”);
printList(head);
//delete 20
deleteNode(&head,20);
printf(“Deleted 20. The New Linked List:n”);
printList(head);
return 0;
Run it
Page –Page ++
Bạn vừa Read Post Với Một số hướng dẫn một cách rõ ràng hơn về Review Delete smaller nodes in linked list C++ tiên tiến và phát triển nhất
Hero đang tìm một số trong những ShareLink Tải Delete smaller nodes in linked list C++ Free.
Nếu sau khi đọc nội dung bài viết Delete smaller nodes in linked list C++ vẫn chưa hiểu thì hoàn toàn có thể lại Comment ở cuối bài để Mình lý giải và hướng dẫn lại nha
#Delete #smaller #nodes #linked #list
Tra Cứu Mã Số Thuế MST KHƯƠNG VĂN THUẤN Của Ai, Công Ty Doanh Nghiệp…
Các bạn cho mình hỏi với tự nhiên trong ĐT mình gần đây có Sim…
Thủ Thuật về Nhận định về nét trẻ trung trong môi trường tự nhiên vạn…
Thủ Thuật về dooshku là gì - Nghĩa của từ dooshku -Thủ Thuật Mới 2022…
Kinh Nghiệm Hướng dẫn Tìm 4 số hạng liên tục của một cấp số cộng…
Mẹo Hướng dẫn Em hãy cho biết thêm thêm nếu đèn huỳnh quang không còn…