Sword Point of Offer question 13th: Delete linked list nodes in O (1) time
1 //============================================================================2 //Name:jz-c-13.cpp3 //Author:laughing_lz4 //Version:5 //Copyright:all Right Reserved6 //Description: Delete a linked list node in O (1) Time7 //============================================================================8 9#include <iostream>Ten#include"List.h" One using namespacestd; A - /*typedef struct LISTNODE { - int value; the listnode* Next; - } NODE;*/ - - voidDeletenode (listnode** Headnode, listnode*tobedeleted) { + if(! Headnode | | !tobedeleted) { - return; + } A if(Tobedeleted->m_pnext! = NULL) {//the node you want to delete is not a tail node . atlistnode* p = tobedeleted->M_pnext; -Tobedeleted->m_nvalue = p->M_nvalue; -Tobedeleted->m_pnext = p->M_pnext; - Deletep; -p =NULL; -}Else if(*headnode = = tobedeleted) {//The linked list has only one node, deleting the head node (also the tail node) . in Deletetobedeleted; -tobedeleted =NULL; to*headnode =NULL; +}Else{//There are multiple nodes in the linked list, delete the tail nodes . -listnode* p = *Headnode; the while(P->m_pnext! =tobedeleted) { *p = p->M_pnext; $ }Panax NotoginsengP->m_pnext =NULL; - Deletetobedeleted; thetobedeleted =NULL; + } A the } + //==================== test Code ==================== - voidTest (listnode* plisthead, listnode*Pnode) { $ //printf ("The original list is: \ n"); $cout <<"The original list is:"<<Endl; - printlist (plisthead); - the //printf ("The node to being deleted is: \ n"); -cout <<"The node to being deleted is:"<<Endl;Wuyi Printlistnode (pnode); the -Deletenode (&Plisthead, pnode); Wu //printf ("The result list is: \ n"); -cout <<"The result list is:"<<Endl; About printlist (plisthead); $ } - - //There are multiple nodes in the list, delete the middle nodes . - voidTest1 () { Alistnode* pNode1 = Createlistnode (1); +listnode* PNode2 = Createlistnode (2); thelistnode* pNode3 = Createlistnode (3); -listnode* pNode4 = Createlistnode (4); $listnode* PNODE5 = Createlistnode (5); the the connectlistnodes (PNode1, pNode2); the connectlistnodes (PNode2, pNode3); the connectlistnodes (PNode3, pNode4); - connectlistnodes (PNode4, PNODE5); in the Test (PNode1, pNode3); the About destroylist (pNode1); the } the the //There are multiple nodes in the linked list, delete the tail nodes . + voidTest2 () { -listnode* pNode1 = Createlistnode (1); thelistnode* PNode2 = Createlistnode (2);Bayilistnode* pNode3 = Createlistnode (3); thelistnode* pNode4 = Createlistnode (4); thelistnode* PNODE5 = Createlistnode (5); - - connectlistnodes (PNode1, pNode2); the connectlistnodes (PNode2, pNode3); the connectlistnodes (PNode3, pNode4); the connectlistnodes (PNode4, PNODE5); the - Test (PNode1, PNODE5); the the destroylist (pNode1); the }94 the //There are multiple nodes in the list, deleting the head node . the voidTest3 () { thelistnode* pNode1 = Createlistnode (1);98listnode* PNode2 = Createlistnode (2); Aboutlistnode* pNode3 = Createlistnode (3); -listnode* pNode4 = Createlistnode (4);101listnode* PNODE5 = Createlistnode (5);102 103 connectlistnodes (PNode1, pNode2);104 connectlistnodes (PNode2, pNode3); the connectlistnodes (PNode3, pNode4);106 connectlistnodes (PNode4, PNODE5);107 108 Test (PNode1, pNode1);109 the destroylist (pNode1);111 } the 113 //There is only one node in the list, delete the head node . the voidTest4 () { thelistnode* pNode1 = Createlistnode (1); the 117 Test (PNode1, pNode1);118 }119 - //The linked list is empty121 voidTest5 () {122 Test (null, NULL);123 }124 the intMainintargcChar**argv) {126 Test1 ();127 Test2 (); - Test3 ();129 Test4 (); the Test5 ();131 the return 0;133}
Jz-c-13