009 implement an algorithm to delete a node in a single-linked list, giving only pointers to that node (keep it up)

Source: Internet
Author: User

Oh, this problem can not directly delete the known nodes. Because it is a single linked list, do not know the precursor, just know
Subsequent nodes, direct deletion causes the list to be disconnected. It's just that we can delete the subsequent nodes of the known nodes,

Assigns the value of the successor node to the known node.

#include <iostream>struct node{int   data; Node* next;}; BOOL RemoveNode (node* vnode) {if (Vnode = = NULL | | vnode->next = NULL) return false; node* Pnext = Vnode->next;vnode->data = Pnext->data;vnode->next = Pnext->next;delete PNext;pNext = NULL;} void Initlist (node* vRoot) {if (VRoot = = NULL) {std::cout << "wrong node\n"; return;} for (int i=0; i<20; ++i) {node* Temp = new Node; Temp->data = i + 1; Temp->next = Vroot->next;vroot->next = Temp;}} int main () {node* Root = new Node; Root->next = null;initlist (Root), while (Root->next)//Last node did not delete {if (root) break;} Std::cout << root->data << std::endl;delete Root; Root = Null;system ("pause"); return 0;}


009 implement an algorithm to delete a node in a single-linked list, giving only pointers to that node (keep it up)

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.