Hướng Dẫn Delete smaller nodes in linked list C++ 2022

Thủ Thuật Hướng dẫn Delete smaller nodes in linked list C++ Chi Tiết

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.

Deleting a node in linked list

Delete a given node from the linked list.

Nội dung chính

    Deleting a node in linked list1. Head node has the given key2. For other nodes (Non-Head)Implementation of deleting a node in linked listTopics You Might LikeVideo liên quan

Example

Linked List : 10 20 30 NULL

Input

20

Output

10 30 NULL

Input

30

Output

10 20 NULL

Algorithm

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.

1. Head node has the given key

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.

2. For other nodes (Non-Head)

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.

Implementation of deleting a node in 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 ++

Topics You Might Like

Video Delete smaller nodes in linked list C++ ?

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

Chia Sẻ Link Down Delete smaller nodes in linked list C++ miễn phí

Hero đang tìm một số trong những ShareLink Tải Delete smaller nodes in linked list C++ Free.

Thảo Luận vướng mắc về Delete smaller nodes in linked list C++

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

Phone Number

Recent Posts

Tra Cứu MST KHƯƠNG VĂN THUẤN Mã Số Thuế của Công TY DN

Tra Cứu Mã Số Thuế MST KHƯƠNG VĂN THUẤN Của Ai, Công Ty Doanh Nghiệp…

2 years ago

[Hỏi – Đáp] Cuộc gọi từ Số điện thoại 0983996665 hoặc 098 3996665 là của ai là của ai ?

Các bạn cho mình hỏi với tự nhiên trong ĐT mình gần đây có Sim…

2 years ago

Nhận định về cái đẹp trong cuộc sống Chi tiết Chi tiết

Thủ Thuật về Nhận định về nét trẻ trung trong môi trường tự nhiên vạn…

2 years ago

Hướng Dẫn dooshku là gì – Nghĩa của từ dooshku -Thủ Thuật Mới 2022

Thủ Thuật về dooshku là gì - Nghĩa của từ dooshku -Thủ Thuật Mới 2022…

2 years ago

Tìm 4 số hạng liên tiếp của một cấp số cộng có tổng bằng 20 và tích bằng 384 2022 Mới nhất

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…

2 years ago

Mẹo Em hãy cho biết nếu đèn huỳnh quang không có lớp bột huỳnh quang thì đèn có sáng không vì sao Mới nhất

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…

2 years ago