Using an array representation for each property, most groups can represent a set of isomorphic objects. The pointer is placed in an extra array, denoted by a subscript.
This implementation code is based on 10.3-5, mainly in order to implement the compacitify operation, that is, all the elements of the list are kept compact in memory, the subject is placed in the first n positions (n elements).
Thought is from the web, moving elements in the process of keeping the elements of the list in the direction of each other , and the free table is not required, because it has nothing to store.
This implements a List_freelist class that supports operations such as Delete,insert,compacitify,print,get_next.
#include <iostream>#include<vector>using namespacestd;Const intM = -;classlist_freelist{ Public: List_freelist (inttotalsize); voidDelete (intx); voidInsert (intkey); voidcompactify (); intGet_next (intx); voidPrint (); ~list_freelist () {}Private: Vector<int>Next; Vector<int>key; Vector<int>prev; intFree_list;//Head of Freelist intList//Head of List intAllocate_object (); voidFree_object (intx);}; List_freelist::list_freelist (inttotalsize) { if(TotalSize <=0) {cout<<"size too small"; Throw; } next.resize (TotalSize); Key.resize (totalsize); Prev.resize (totalsize); List= -1; Free_list=0; for(inti =0; I < totalsize-1; ++i) next[i]= i +1; Next[totalsize-1] = -1;}voidList_freelist::D elete (intx) {if(Prev[x] = =-1&& next[x] = =-1) List= -1; if(prev[x]! =-1) Next[prev[x]]=Next[x]; if(next[x]! =-1) Prev[next[x]]=Prev[x]; Free_object (x);}intList_freelist::allocate_object () {if(Free_list = =-1) {cout<<"Overflow"; Throw; } inttemp =free_list; Free_list=Next[free_list]; returntemp;}voidList_freelist::free_object (intx) {Next[x]=free_list; Free_list=x;}voidList_freelist::insert (intvalue) { inttemp =Allocate_object (); Key[temp]=value; Prev[temp]= -1; Next[temp]=list; if(List! =-1) Prev[list]=temp; List=temp;}intList_freelist::get_next (intx) { if(Next[x] = =-1) {cout<<"End of List"; Throw; } Else returnkey[next[x]];}voidlist_freelist::P rint () {if(List = =-1) {cout<<"Empty list"; return; } inttemp =list; while(Next[temp]! =-1) {cout<< key[temp]<<" "; Temp=Next[temp]; }}voidlist_freelist::compactify () {if(Free_list = =-1) return; if(List = =-1) return; inti =free_list; while(I! =-1) {Prev[i]=int_min; I=Next[i]; } intleft =0, right = Next.size ()-1; while(true){ while(Prev[left]! = int_min&&left<right)//Prevent overflow++Left ; while(Prev[right] = =int_min)--Right ; if(Left >=Right ) Break; Prev[left]=Prev[right]; Key[left]=Key[right]; Next[left]=Next[right]; Next[right]= left;//address of the linked list element after record Exchange++Left ; --Right ; } ++Right ; //organize the list to ensure its correctness for(inti =0; I < right; ++i) { if(Prev[i] >=Right ) Prev[i]=Next[prev[i]]; if(Next[i] >=Right ) Next[i]=Next[next[i]]; } //update linked list head if(List >=Right ) List=Next[list]; //Organize your free form for(inti = right; I < next.size ()-1; i++) Next[i]= i +1; Next[next.size ()-1] = -1; //Update free Table headFree_list =Right ;}intMain () {list_freelist lf ( the); for(inti =0; I < the; ++i) LF. Insert (i); Lf. Print (); cout<<Endl; Lf. Delete (2); Lf. Delete (4); Lf. Delete (6); Lf. Print (); cout<<Endl; Lf.compactify (); Lf. Print (); cout<<Endl;}
Most groups implement the linked list structure C + + implementation code