Java Reference
In-Depth Information
/
**
Singlylinkedlistnodewithfreelistsupport
*
/
classLink<E>{
privateEelement; //Valueforthisnode
privateLink<E>next;//Pointtonextnodeinlist
/
**
Constructors
*
/
Link(Eit,Link<E>nextval)
{element=it;next=nextval;}
Link(Link<E>nextval){next=nextval;}
/
**
Getandsetmethods
*
/
Link<E>next(){returnnext;}
Link<E>setNext(Link<E>nxtval){returnnext=nxtval;}
Eelement(){returnelement;}
EsetElement(Eit){returnelement=it;}
/
**
Extensionstosupportfreelists
*
/
staticLinkfreelist=null; //Freelistfortheclass
/
**
@returnAnewlink
*
/
static<E>Link<E>get(Eit,Link<E>nextval){
if(freelist==null)
returnnewLink<E>(it,nextval);//Getfrom"new"
Link<E>temp=freelist; //Getfromfreelist
freelist=freelist.next();
temp.setElement(it);
temp.setNext(nextval);
returntemp;
}
/
**
Returnalinktothefreelist
*
/
voidrelease(){
element=null; //Dropreferencetotheelement
next=freelist;
freelist=this;
}
}
Figure4.11
Implementation for the
Link
class with a freelist. The
static
declaration for member
freelist
means that all
Link
class objects share the
same freelist pointer variable instead of each object storing its own copy.