#defineTRUE 1#defineFALSE 0#defineOK 1#defineERROR 0#defineINFEASIBLE-1#defineMYOVERFLOW-2#defineMAXSIZE 1000//maximum length of a linked listtypedefintStatus;typedefintElemtype;//represents the int type with the specified identifier Elemtype, as the name implies that the element type is inttypedefstruct{elemtype data; intcur;} component, Slinklist[maxsize];voidINITSPACE_SL (Slinklist &space);//chain a one-dimensional array of space components into an alternate list, space[0].cur as the head pointer//"0" means null pointerintMALLOC_SL (Slinklist &space);//If the spare space list is not empty, the assigned node subscript is returned, otherwise 0 is returned .voidFREE_SL (Slinklist &space,intk);//Recycle the Idle node labeled K to the alternate listvoidCREAT_SL (int&,slinklist &);//Create a static linked list that contains a header node with a data field of 0 for the head nodevoidTRAVERSE_SL (intSl,slinklist);//traversing a static linked list SLintLocateelem_sl (slinklist, elemtype e);//find the first element with the value E in the static unrequited love linear table L//if found, returns the ordinal of its position in L, otherwise returns 0voidDifference (slinklist &space,int&S);//enter the elements of collections A and B, and set up a representation set in a one-dimensional array space (-a) U (b-a)//the static linked list, S is its head pointer, assuming that the device space is large enough, space[0].cur for its head pointervoidINITSPACE_SL (Slinklist &space)//chain a one-dimensional array of space components into an alternate list, space[0].cur as the head pointer//"0" means null pointer{ for(inti =0; I < MAXSIZE-1; i++) Space[i].cur = i +1; Space[maxsize-1].cur =NULL;}intMALLOC_SL (Slinklist &space)//returns the assigned node subscript if the spare space list is not empty, otherwise returns 0{ inti = space[0].cur; if(space[0].cur) space[0].cur =space[i].cur; returni;}voidFREE_SL (Slinklist &space,intK//Recycle the Idle node labeled K to the alternate list{space[k].cur= space[0].cur; space[0].cur =K;}voidCREAT_SL (int&sl,slinklist &space)//Create a static linked list that contains a header node with a data field of 0 for the head node{ intN; cout<<"Please input the length of the list:"; CIN>>N; SL= MALLOC_SL (space);//Create a header node with a null data field for the head node intPT =SL; Space[sl].data=NULL; cout<<"Please input the data of the node:"; for(inti =1; I <= N; i++) {//Create a node with data at the back of N inttemp =MALLOC_SL (space); CIN>>Space[temp].data; Space[sl].cur= temp;//move the node backwardsSL =temp; } space[sl].cur=null;//The last node has a pointer field of 0.SL =pt; cout<<"The creation of list is completed!"<<Endl;}voidTRAVERSE_SL (intSl,slinklist space)//traversing a static linked list SL{cout<<"The contain of the list is:"<<Endl; SL=space[sl].cur; for(; SL;) {cout<< Space[sl].data <<" "; SL=space[sl].cur; } cout<<Endl;}intLOCATEELEM_SL (slinklist space, elemtype e)//find the first element with the value E in the static unrequited love linear table L//if found, returns the ordinal of its position in L, otherwise returns 0{ inti =2; for(; Space[i-1].cur; i =space[i].cur)if(Space[i].data = = e &&space[i-1].cur) {returnI1; } return 0;}voidDifference (slinklist &space,int&S)//enter the elements of collections A and B, and set up a representation set in a one-dimensional array space (-a) U (b-a)//the static linked list, S is its head pointer, assuming that the device space is large enough, space[0].cur for its head pointer{S=MALLOC_SL (space); Space[s].data= NULL;//establish the head node. intR =S; intLA, LB; cout<<"Please input the length of A and B:"; CIN>> La >>lb; cout<<"Please input the data of A:"<<Endl; for(inti =1; I <= la; i++) {//Enter the value of a inttemp =MALLOC_SL (space); CIN>>Space[temp].data; Space[r].cur=temp; R=temp; } space[r].cur= NULL;//R is the last node at the moment.cout <<"Please input the data of B:"<<Endl; for(intj =1; J <= LB; J + +) {//Enter the value of B inttemp =MALLOC_SL (space); CIN>>Space[temp].data; intp; P=S; intK =space[p].cur; while(K&&space[k].data! = space[temp].data) {//determines whether the value of B entered is an element in ap = k;//P is the pioneer node of K, which requires the use of a precursor node for INSERT and delete operations .K =space[k].cur; } if(!k) {//The element that is not a is inserted directly at the end of a, at which point P is the last elementSpace[temp].cur =space[p].cur; Space[p].cur=temp; } Else{//is the element in a, releasing the temporary established node first .FREE_SL (space, temp); if(k! = r) {//Delete the point of the element in A and B in the objectSpace[p].cur =space[k].cur; FREE_SL (space, k); } Else{space[p].cur=space[k].cur; FREE_SL (space, k); R= P;//if the last element is deleted, the value of R is changed } } }}
Static linked list