Implement an algorithm to delete a node in a single-chain table, and only give the pointer to that node (keep it up), single-chain

Source: Internet
Author: User

Implement an algorithm to delete a node in a single-chain table, and only give the pointer to that node (keep it up), single-chain
Hehe, this question cannot directly Delete known nodes, because it is a single-chain table, do not know the precursor, only know
Directly deleting the successor node will disconnect the linked list. However, we can delete the successor nodes of known nodes,

Assign the value of the successor node to a 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-> nex T = vRoot-> next; vRoot-> next = Temp;} int main () {Node * Root = new Node; Root-> next = NULL; initList (Root ); while (Root-> next) // The Last node is not deleted {if (! RemoveNode (Root) break;} std: cout <Root-> data <std: endl; delete Root; Root = NULL; system ("pause "); return 0 ;}



A linked list does not know the header node. A pointer points to one of the nodes and asks how to delete the node pointed to by the pointer.

Copy the node to the value of the next node, and then delete the next node.
Node * p; // current node
Node * q;
Q = p-> next;
P. data = q. data; // copy the q node to p

P-> next = q-> next; // Delete q
Free (q );

Data Structure: in a single-chain table, if p points to a node's successor node, the following operations are performed :()

In the specific implementation code, p is just a pointer to point to the current node, but p itself is not a node in the linked list, therefore, the question means that the current node is known (pointer p points to this node, p is not the precursor node of this node), and the successor node of the current node needs to be deleted

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.