Question: Given the head pointer and a node pointer of the linked list, delete the node at O (1) time. The linked list node is defined as follows:
Struct listnode
{
Int m_nkey;
Listnode * m_pnext;
};
The function declaration is as follows:
Void deletenode (listnode * plisthead, listnode * ptobedeleted );
The method comes from the network and the complete code is self-written.
When you delete a node in a linked list, you must first find the node.
The common method is to use two pointers: Q and pre. Q searches for nodes in the previous traversal table, And pre points to the previous node that Q just traversed. When q = node, it indicates that the node to be deleted has been found.
You only need to: Pre-> next = Q-> next; Delete Q.
However, the complexity of the above method is obviously O (n), which does not meet the requirements of the question.
Since the question has given us a pointer to the node to be deleted, we do not need to query this node in a traversal table. The method we selected is to assign the value of P to the next node of the node to the data field of the node, and delete the P node. In this way, the effect is equivalent to deleting the node.
However, in this case, the last node is to be deleted. In this case, the next node P of node is null. At this time, we still need to follow the conventional approach to traverse the chain table and find a node before the node.
The following is the complete and runable code.
// Helloworld. cpp: defines the entry point of the console application. // # Include "stdafx. H "# include <iostream> # include <algorithm> # include <string> # include <sstream> using namespace STD; typedef struct node {int data; struct node * Next ;} node, * pnode; pnode createlist (int n) {pnode head = (pnode) malloc (sizeof (node); head-> DATA = 0; // The data field of the head header pointer stores the length of the linked list head-> next = NULL; pnode P = head; int num; while (n --) {cout <"input" <n <"node:"; CIN> num; pnode q = (pnode) malloc (sizeof (node )); Q-> DATA = num; q-> next = NULL; P-> next = Q; P = Q; (Head-> data) ++;} return head ;} void display (pnode head) {If (Head = NULL) {cout <"empty list" <Endl; return;} pnode P = head-> next; cout <p-> data; P = p-> next; while (p) {cout <"->" <p-> data; P = p-> next;} cout <Endl;} pnode find (pnode head, int index) // locate the index node in the linked list, returns the pointer to the node {If (Head = NULL) return NULL; If (index> head-> data) {cout <"index exceed the boundary! "<Endl; return NULL;} pnode P = head; while (P & Index --) {P = p-> next;} return P ;} void deletenode (pnode head, pnode p) {If (Head = NULL | P = NULL) return; If (p-> next! = NULL) // if the last node is not deleted {pnode q = p-> next; P-> DATA = Q-> data; p-> next = Q-> next;} else // if the last node is deleted, {pnode r = head-> next; while (R-> next! = NULL & R-> next! = P) {r = r-> next;} r-> next = p-> next;} int main () {int n = 5; pnode head = createlist (n); // create a chain table display (head) with a length of N; // output chain table pnode P = find (Head, 1 ); // find the nth node deletenode (Head, P); // Delete the nth node display (head); // output the effect of getchar () after deleting the node (); getchar (); Return 0 ;}