Академический Документы
Профессиональный Документы
Культура Документы
AbstractionandDesignUsingJava
http://www2.hawaii.edu/~esb/2011fall.ics211/LinkedListRec.java.html
http://cs.boisestate.edu/~mvail/teaching/225/source/KW50_Student/CH07/LinkedListRec.java
________________________________
/**Arecursivelinkedlistclasswithrecursivemethods.
*@authorKoffmanandWolfgang
**/
publicclassLinkedListRec<E>{
/**Thelisthead*/
privateNode<E>head
/**ANodeisthebuildingblockforasinglelinkedlist.*/
privatestaticclassNode<E>{
//DataFields
/**Thereferencetothedata.*/
privateEdata
/**Thereferencetothenextnode.*/
privateNodenext
//Constructors
/**Createsanewnodewithanullnextfield.
@paramdataItemThedatastored
*/
privateNode(EdataItem){
data=dataItem
next=null
}
/**Createsanewnodethatreferencesanothernode.
@paramdataItemThedatastored
@paramnodeRefThenodereferencedbynewnode
*/
privateNode(EdataItem,Node<E>nodeRef){
data=dataItem
next=nodeRef
}
}//endclassNode
/**Findsthesizeofalist.
@paramheadTheheadofthecurrentlist
@RETURNTHESIZEOFTHECURRENTLIST
*/
privateintsize(Node<E>head){
if(head==null)
return0
else
return1+size(head.next)
}
/**Wrappermethodforfindingthesizeofalist.
@returnThesizeofthelist
*/
publicintsize(){
returnsize(head)
}
/**Returnsthestringrepresentationofalist.
@paramheadTheheadofthecurrentlist
@returnThestateofthecurrentlist
*/
privateStringtoString(Node<E>head){
if(head==null)
return""
else
returnhead.data+"\n"+toString(head.next)
}
/**Wrappermethodforreturningthestringrepresentationofalist.
@returnThestringrepresentationofthelist
*/
publicStringtoString(){
returntoString(head)
}
/**ReplacesalloccurrencesofoldObjwithnewObj.
post:EachoccurrenceofoldObjhasbeenreplacedbynewObj.
@paramheadTheheadofthecurrentlist
@paramoldObjTheobjectbeingremoved
@paramnewObjTheobjectbeinginserted
*/
privatevoidreplace(Node<E>head,EoldObj,EnewObj){
if(head!=null){
if(oldObj.equals(head.data))
head.data=newObj
replace(head.next,oldObj,newObj)
}
}
/*WrappermethodforreplacingoldObjwithnewObj.
post:EachoccurrenceofoldObjhasbeenreplacedbynewObj.
@paramoldObjTheobjectbeingremoved
@paramnewObjTheobjectbeinginserted
*/
publicvoidreplace(EoldObj,EnewObj){
replace(head,oldObj,newObj)
}
/**Addsanewnodetotheendofalist.
@paramheadTheheadofthecurrentlist
@paramdataThedataforthenewnode
*/
privatevoidadd(Node<E>head,Edata){
//Ifthelisthasjustoneelement,addtoit.
if(head.next==null)
head.next=newNode<E>(data)
else
add(head.next,data)//Addtorestoflist.
}
/**Wrappermethodforaddinganewnodetotheendofalist.
@paramdataThedataforthenewnode
*/
publicvoidadd(Edata){
if(head==null)
head=newNode<E>(data)//Listhas1node.
else
add(head,data)
}
/**Removesanodefromalist.
post:ThefirstoccurrenceofoutDataisremoved.
@paramheadTheheadofthecurrentlist
@parampredThepredecessorofthelisthead
@paramoutDataThedatatoberemoved
@returntrueiftheitemisremoved
andfalseotherwise
*/
privatebooleanremove(Node<E>head,Node<E>pred,EoutData){
if(head==null)//Basecaseemptylist.
returnfalse
elseif(head.data.equals(outData)){//2ndbasecase.
pred.next=head.next//Removehead.
returntrue
}
else
returnremove(head.next,head,outData)
}
/**Wrappermethodforremovinganode(inLinkedListRec).
post:ThefirstoccurrenceofoutDataisremoved.
@paramoutDataThedatatoberemoved
@returntrueiftheitemisremoved,
andfalseotherwise
*/
publicbooleanremove(EoutData){
if(head==null)
returnfalse
elseif(head.data.equals(outData)){
head=head.next
returntrue
}
else
returnremove(head.next,head,outData)
}
______________________________
/**
*Alistimplementedusingasinglylinkedlistandusingrecursivemethods
*@authorEdoBiagioni
*@lectureICS211Jan27(orlater)
*@dateJanuary26,2011
*/
publicclassLinkedListRec<E>{
//here,includetheLinkedNodedefinition
/**
*Anodeinasinglylinkedlist
*@authorEdoBiagioni
*@lectureICS211Jan27orlater
*@dateJanuary26,2010
*/
privatestaticclassLinkedNode<T>{
privateTitem
privateLinkedNode<T>next
/**
*constructortobuildanodewithnosuccessor
*@paramthevaluetobestoredbythisnode
*/
privateLinkedNode(Tvalue){
item=value
next=null
}
/**
*constructortobuildanodewithspecified(maybenull)successor
*@paramthevaluetobestoredbythisnode
*@paramthenextfieldforthisnode
*/
privateLinkedNode(Tvalue,LinkedNode<T>reference){
item=value
next=reference
}
}
//endoftheLinkedNodedefinition
//thisisthestartofthelinkedlist.Ifthelistisempty,itisnull
protectedLinkedNode<E>head
/**
*initializesanemptylinkedlist
*/
publicLinkedListRec(){
head=null
}
/**recursiveprivatemethod,calledbythepublicwrappermethod
*@paramtheheadofthelist(maybenullifweareattheend)
*@returnthesizeofthelist
*/
privateintsize(LinkedNode<E>current){
if(current==null){
return0//anemptylisthassize0
}//anonemptylisthassize1morethantherestofthelist:
return1+size(current.next)
}
/**publicwrappermethod,callstheprivaterecursivemethod
*@paramnone
*@returnthesizeofthelist
*/
publicintsize(){
returnsize(head)
}
/**recursiveprivatemethod,calledbythepublicwrappermethod
*@paramtheheadofthelist(maybenullifweareattheend)
*@paramthevaluetobeadded
*@returnthelist,withthevalueadded
*/
privateLinkedNode<E>addAtEnd(LinkedNode<E>node,Evalue){
if(node==null){
returnnewLinkedNode<E>(value)
}
node.next=addAtEnd(node.next,value)
returnnode
}
/**publicwrappermethod,callstheprivaterecursivemethod
*@paramthevaluetobeaddedattheendofthelinkedlist
*/
publicvoidadd(Evalue){
head=addAtEnd(head,value)
}
/**recursiveprivatemethod,calledbythepublicwrappermethod
*@paramtheheadofthelist(maybenullifweareattheend)
*@paramthenumberofnodestoskipbeforeinserting
*@paramthevaluetobeadded
*@returnthelist,withthevalueadded
*/
privateLinkedNode<E>addAtPosition(LinkedNode<E>node,intskip,Evalue){
if(skip==0){
returnnewLinkedNode<E>(value,node)
}
if(node==null){//nodeisnullbutskip>0badindex
thrownewIndexOutOfBoundsException("badindexforadd")
}
node.next=addAtPosition(node.next,skip1,value)
returnnode
}
/**publicwrappermethod,callstheprivaterecursivemethod
*@paramthepositionatwhichtoadd:0toaddatthestart
*@paramthevaluetobeadded
*@throwsIndexOutOfBoundsExceptioniftheindexislessthan0
*orgreaterthanthenumberofelementsinthelinkedlist
*/
publicvoidadd(intindex,Evalue){
head=addAtPosition(head,index,value)
}
/**recursiveprivatemethod,calledbythepublicwrappermethod
*@paramtheheadofthelist(maybenullifweareattheend)
*@paramthevaluetoberemoved
*@returnthelist,withthevalueremoved
*/
privateLinkedNode<E>remove(LinkedNode<E>node,Evalue){
if(node==null){//nodeisnullbutskip>0badindex
returnnode
}
if(node.item.equals(value)){
returnnode.next//match,soremovethisnode
}
node.next=remove(node.next,value)
returnnode
}
/**publicwrappermethod,callstheprivaterecursivemethod
*@paramtheobjecttoremove
*/
publicvoidremove(Evalue){
head=remove(head,value)
}
/**recursiveprivatemethod,calledbythepublicwrappermethod
*@paramtheheadofthelist(maybenullifweareattheend)
*@returnthestringrepresentingthelist
*/
privateStringtoString(LinkedNode<E>node){
if(node==null){
return""
}
if(node.next==null){
returnnode.item.toString()
}
returnnode.item.toString()+"==>"+toString(node.next)
}
/**
*concatenatestheelementsofthelinkedlist,separatedby"==>"
*@returnthestringrepresentationofthelist
*/
publicStringtoString(){
returntoString(head)
}
/**
*unittestmethodbasictestingofthefunctionality
*@paramrequired,ignored
*/
publicstaticvoidmain(String[]arguments){
LinkedListRec<String>ll=newLinkedListRec<String>()
System.out.println(ll)
ll.add("foo")
System.out.println(ll)
ll.add(1,"bar")
System.out.println(ll)
ll.add("baz")
System.out.println(ll)
ll.add(0,"hello")
System.out.println(ll)
ll.add(1,"world")
System.out.println(ll)
ll.remove("foo")//removeanintermediateelement
System.out.println(ll)
ll.remove("hello")//removethefirstelement
System.out.println(ll)
ll.remove("baz")//removethelastelement
System.out.println(ll)
}