Today, a lot of information about the list of linked lists probably understand the list, for the record only with a pen here.
Linked list Overview: Dynamic Data storage units that can be more flexible than arrays.
The composition of the linked list: the stored data, the next node.
First let's complete a node with code.
classNode//Node Class{ Public: Node () {}//three different ways to create nodesNode (intn) {num = n; next =NULL;} Node (intN, Node *p) {num = n; next =p;} voidSetnum (intn) {num =N;} voidSetnext (Node *p) {next =p;} intGetnum () {returnNum }//Use the Set&&get function to operateNode *getnext () {returnnext;}Private: intNum//only one integer is stored here, in fact the data can be in any form. Node *next;//the next pointer is stored here};
In fact, you can also use a simple struct. Examples are as follows:
struct Node { int num; *next;};
Too simple, no longer detailed.
And for the node operation method, as follows:
classLinklist//Linked List class{ Public: Linklist () {head=NULL;} Linklist (intN) {head =NewNode (n);} voidAddatend (intN); voidAddatend (Node *p); voidVisitallnode (); voidAddbeforenum (Node *p,intNUM);//add a new node before the node value is num p voidDeletenum (intNUM);//Delete a node with a value of NumPrivate: Node*head;};
The above is the main body, then each function is described separately.
To add a node:
voidLinklist::addatend (intN//Add a node to the end of a list{Node*p=Head; if(Head==null) {head =NewNode (n);} Else { while(P->getnext ()! =NULL) {P= P->getnext (); }//use p to point to the last nodeP->setnext (NewNode (n));//inserting Nodes } } voidLinklist::addatend (Node *p)//Add a node to the end of a list{Node*pn=Head; if(Head==null) {head=p;} Else { while(Pn->getnext ()! =NULL) {PN=pn->getnext (); }//use PN to point to the last nodePn->setnext (P);//inserting Nodes } }
Traversing a linked list:
voidLinklist::visitallnode ()//traversing a linked list{Node*p; P=head;if(P==null) {cout<<"empty linked list!"<<Endl;} Else{cout<<"table Header:"; while(P->getnext ()! =NULL) {cout<<p->getnum () <<" -"; P=p->GetNext (); } cout<<p->getnum () <<"End of Table"<<Endl; } }
Insert node before storing data for NUM (insert here and delete below need to create two pointers to operate)
voidLinklist::addbeforenum (Node *p,intNum//add a new node before the node value is num p{Node*pn=head,*follow=PN; if(pn==NULL) {cout<<"Empty linked list! Inserts a node. "; Head=p; return; } while(Pn->getnext ()! = NULL && pn->getnum ()! =num)//find a node that points to a node with a value of Num{Follow= PN;//follow always points to a node before PNPN = pn->GetNext (); } if(Pn==head && pn->getnum () = = num)//node is found, and the head pointer{p-Setnext (PN); Head=p; } Else if(Pn->getnum () = = num)//Locate this node, note: Follow points to its previous node{p-Setnext (PN); Follow-Setnext (P); } Else //node is not found, PN points to the last node, inserts new node into the end of the list{cout<<"not found, inserted into the end of the list. "<<Endl; PN-Setnext (P); } }
Delete a node with a value of num
voidlinklist::d Eletenum (intNum//Delete a node{Node*p=head,*follow=p; if(P->getnum () = = num)//If the deleted node is a 1th node{Head= p->GetNext (); Deletep; } Else { while(P->getnext ()!=null && p->getnum ()! =num) {Follow= P;//follow always points to the previous node of Pp = p->GetNext (); } if(P->getnum () = =num) {Follow->setnext (p->GetNext ()); Deletep; } } }
Main function (test program)
intMain () {linklist*pl=NULL; intnum, n; Node*p; Charoption; while(1) {cout<<"\ n List operation exercise:"<<Endl; cout<<"1: Create a linked list"<<Endl; cout<<"2: Insert a new node at the end of the list"<<Endl; cout<<"3: Insert a new node before the node with a node value of Num"<<Endl; cout<<"4: Delete node"<<Endl; cout<<"5: Traversing nodes"<<Endl; cout<<"0: Exit"<<Endl; cout<<"\ n Please enter your choice:"; CIN>>option; Switch(option) { Case '0': Break; Case '1': cout<<"Please enter the value of the first node"<<Endl; CIN>>num; PL=Newlinklist (num); PL-Visitallnode (); Break; Case '2': cout<<"Please enter a node value:"; CIN>>num; if(Pl==null) {PL =Newlinklist (num);} Else{PL->addatend (num);//Pl->addatend (new Node (num));} PL-Visitallnode (); Break; Case '3': cout<<"Please enter the value of the inserted node:"; CIN>>num; cout<<"Enter the node to which you want to insert it, as indicated by the value of the node:"; CIN>>N; PL->addbeforenum (NewNode (num), n); PL-Visitallnode (); Break; Case '4': cout<<"Please enter the value of the node to be deleted:"; CIN>>num; PL-deletenum (num); PL-Visitallnode (); Break; Case '5': Pl-Visitallnode (); Break; default: cout<<"you entered the error, please re-enter! "<<Endl; } if(option = ='0' ) Break; } return 0;}
Learn notes C + + linked list