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