#defineTRUE 1#defineFALSE 0#defineOK 1#defineERROR 0#defineINFEASIBLE-1#defineMYOVERFLOW-2typedefintStatus;typedefintElemtype;//represents the int type with the specified identifier Elemtype, as the name implies that the element type is inttypedefstructlnode{elemtype data; Lnode*next;}*linklist; Status visit (linklist L);//A function that iterates over a linked listvoidAssignment (linklist &l);//attach the initial value to the linked listStatus getelem_l (linklist L,intI, Elemtype &e);//l head pointer for single-linked list with lead node//when the first element is present, its value is assigned to E and returns OK, otherwise the error is returnedvoidListtraverse (linklist L, Status (*PF) (linklist l));//The function is called for each element of L, and the operation fails once the function failsStatus listinsert_l (linklist &l,intI, Elemtype e);//inserting element e before the first position in the single-chain linear table L of the lead nodeStatus listdelete_l (linklist &l,intI, Elemtype &e);//in the single-chain linear table L of the lead node, delete the element I, and return its value by evoidmergelist_l (linklist &la, linklist &lb, Linklist &Lc);//elements of the single-chain linear table, LA and LB, are known to be non-descending by value//merging LA and lb to get new single-stranded linear table LC,LC elements are also non-descending by valueStatus visit (linklist L)//A function that iterates over a linked list{ if(L = =NULL) {cout<<"It's an empty list!"<< Endl;//l is empty linked list, traversal failed returnERROR; } linklist p; P=L; P= p->Next; for(; p! = NULL; p=p->next)//The output p->data knows that p is null, at which point the linked list has been traversed completelycout << P->data <<" "; cout<<Endl; returnOK;}voidAssignment (linklist &l)//attach the initial value to the linked list{cout<<"Please input the length of the linklist:"; intN; CIN>> N;//Enter the number of nodes in the list, not including an empty head node.cout <<Endl; cout<<"Please input the data of the Lnode:"; Lnode*p; P=NewLnode; P->data = NULL;//the data element of the head node is nullL =p; for(inti =1; I <= N; i++) {p->next =NewLnode;//Create a new nodep = p->next;//pointer moves backCIN >> p->data;//data elements for input nodes} P->next = NULL;//The last node pointer field is a NULL pointercout <<"The linklist ' s assignment is completed!"<<Endl;}voidListtraverse (linklist L, Status (*PF) (linklist L))//The function is called for each element of L, and the operation fails once the function fails{visit (L);} Status getelem_l (linklist L,intI, Elemtype &e)//l head pointer for single-linked list with lead node//when the first element is present, its value is assigned to E and returns OK, otherwise the error is returned{ if(I <1) {cout<<" the number of"<< I <<"You have the input is wrong!"<< Endl;//Location <1 indicates an error in the input data returnERROR; } linklist p; P=L; for(intj =1; J <= i&&p!=null; J + +) {//move p to the I node (excluding the head node) .p = p->Next; } if(p = = NULL) {//if moving to the last node has not reached the I node, then the input data error, more than the linked list lengthcout <<"can ' t find the position of I in the linklist!"<<Endl; returnERROR; } e= p->data;//assigns the data element of the I bit to the E returnOK;} Status listinsert_l (linklist&l,intI, Elemtype e)//inserting element e before the first position in the single-chain linear table L of the lead node{linklist p; P=L; intj =1; if(I >=1){ for(; J < i&&p; J + +, p = p->next) {}//find the previous node of I bit if(p) {linklist temp=NewLnode; Temp->data =e; Temp->next = p->next;//Modify the pointer so that the new node is inserted into the listP->next =temp; returnOK; } Else{//if the last node of I has not reached the previous node of I, then the input data is incorrect .cout <<"The position"<< I <<"You have the input is wrong!"<<Endl; returnERROR; } } Else{cout<<"The position"<< I <<"You have the input is wrong!"<<Endl; returnERROR; }}status listdelete_l (linklist&l,intI, Elemtype &e)//in the single-chain linear table L of the lead node, delete the element I, and return its value by e{linklist p; P=L; intj =1; if(I >=1){ for(k <i&&p->next; J + +, p = p->next) {}//find the previous node of I bit if(p->next) {Linklist temp; Temp= p->Next; E= temp->data; P->next=p->next->next;//Modify the pointer so that the new node is inserted into the list Deletetemp; returnOK; } Else{//if the last node of I has not reached the previous node of I, then the input data is incorrect .cout <<"The position"<< I <<"You have the input is wrong!"<<Endl; returnERROR; } } Else{cout<<"The position"<< I <<"You have the input is wrong!"<<Endl; returnERROR; }}voidmergelist_l (linklist &la, linklist &lb, Linklist &Lc)//elements of the single-chain linear table, LA and LB, are known to be non-descending by value//merging LA and lb to get new single-stranded linear table LC,LC elements are also non-descending by value{linklist pa, Pb, PC; PA= la->Next; PB= lb->Next; PC=Lc; for(; pa&&PB;) {if(Pa->data < pb->data) {PC->next =PA; PA= pa->Next; PC= pc->Next; } Else{PC->next =PB; PB= pb->Next; PC= pc->Next; } } if(PA) PCNext =PA; ElsePc->next =PB;}
Dynamic chain representation and implementation