Doubly linked list (2)-delete node

Source: Internet
Author: User

Deleting a specified node in a doubly linked list can be divided into the following 3 scenarios:
(a) The original doubly linked list

(b) After removing the head node

(c) After removing the intermediate node

(d) After deleting the tail node


Algorithm
Suppose that the node that needs to be deleted is called Delnode.
1) If Delnode is the head node, point the head pointer to the subsequent node.
2) If the forward node of the Delnode is present, the forward pointer of the front node is set to the Delnode back pointer .
3) If the delnode node is present, the forward pointer of the back node is set to the forward pointer of Del.

#include <iostream>struct node{int data; Node *next; Point to the next node *prev; Point to the previous node};/* implements a doubly linked list to remove a node head-to-list header pointer. Delnode the node that needs to be deleted */void deletenode (node **head, node *delnode) {//Yes No null-linked list if (*head = = NULL | | delnode = null) return;//Delete head node if (*head = = delnode) *head = delnode->next;//truncated point not tail node if (delno De->next = NULL) Delnode->next->prev = delnode->prev;//The truncated point is not the first node if (Delnode->prev! = null) delnode-> Prev->next = Delnode->next;delete Delnode;return;} Given a list of head pointers (head) and an integer, insert a new node to the head of the linked list//The reason for passing in a double pointer, because the function needs to modify the list void push (node** head, int newdata) {//1. Allocating new node memory node* N Ewnode = new NODE;//2. Assignment Newnode->data = Newdata;//3. The original head node is made as a back pointer to the new node, while the forward pointer is set to NULL Newnode->next = (*head); Newnode->prev = Null;//4. Set the forward pointer of the original head node to the new node if ((*head)! = NULL) (*head)->prev = NEWNODE;//5. Position the head pointer to a new node (*head) = NewNode;} void Printlist (Node *head) {while (head! = NULL) {std::cout<< "" <Output:
Original DLL is:10 8 6 4 2 0
New DLL Is:8 6 2

Time complexity: O (N)

Doubly linked list (2)-delete node

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.