Académique Documents
Professionnel Documents
Culture Documents
//linkList.java
//demonstrateslinkedlist
//torunthisprogram:C>javaLinkListApp
///////////////////////////////////////////////
classLink
{
publicintiData;//dataitem(key)
publicdoubledData;//dataitem
publicLinknext;//nextlinkinlist
//
publicLink(intid,doubledd)//constructor
iData=id;//initializedata
dData=dd;//(nextisautomatically
}//settonull)
//
publicvoiddisplayLink()//displayourself
System.out.print("{"+iData+","+dData+"}");
}
}//endclassLink
////////////////////////////////////////////////////////////////
classLinkList
{
privateLinkfirst;//reftofirstlinkonlist
//
publicLinkList()//constructor
first=null;//noitemsonlistyet
//
publicbooleanisEmpty()//trueiflistisempty
return(first==null);
//
//insertatstartoflist
publicvoidinsertFirst(intid,doubledd)
{//makenewlink
LinknewLink=newLink(id,dd);
newLink.next=first;//newLink>oldfirst
first=newLink;//first>newLink
//
publicLinkdeleteFirst()//deletefirstitem
{//(assumeslistnotempty)
Linktemp=first;//savereferencetolink
first=first.next;//deleteit:first>oldnext
returntemp;//returndeletedlink
//
publicvoiddisplayList()
System.out.print("List(first>last):");
Linkcurrent=first;//startatbeginningoflist
while(current!=null)//untilendoflist,
current.displayLink();//printdata
current=current.next;//movetonextlink
System.out.println("");
//
}//endclassLinkList
////////////////////////////////////////////////////////////////
classLinkListApp
{
publicstaticvoidmain(String[]args)
LinkListtheList=newLinkList();//makenewlist
theList.insertFirst(22,2.99);//insertfouritems
theList.insertFirst(44,4.99);
theList.insertFirst(66,6.99);
theList.insertFirst(88,8.99);
theList.displayList();//displaylist
while(!theList.isEmpty())//untilitsempty,
LinkaLink=theList.deleteFirst();//deletelink
System.out.print("Deleted");//displayit
aLink.displayLink();
System.out.println("");
theList.displayList();//displaylist
}//endmain()
}//endclassLinkListApp
DoubleLinkedList
//doublyLinked.java
//demonstratesdoublylinkedlist
//torunthisprogram:C>javaDoublyLinkedApp
////////////////////////////////////////////////////////////////
classLink
{
publiclongdData;//dataitem
publicLinknext;//nextlinkinlist
publicLinkprevious;//previouslinkinlist
//
publicLink(longd)//constructor
{dData=d;}
//
publicvoiddisplayLink()//displaythislink
{System.out.print(dData+"");}
//
}//endclassLink
////////////////////////////////////////////////////////////////
classDoublyLinkedList
{
privateLinkfirst;//reftofirstitem
privateLinklast;//reftolastitem
//
publicDoublyLinkedList()//constructor
{
first=null;//noitemsonlistyet
last=null;
}
//
publicbooleanisEmpty()//trueifnolinks
{returnfirst==null;}
//
publicvoidinsertFirst(longdd)//insertatfrontoflist
{
LinknewLink=newLink(dd);//makenewlink
if(isEmpty())//ifemptylist,
last=newLink;//newLink<last
else
first.previous=newLink;//newLink<oldfirst
newLink.next=first;//newLink>oldfirst
first=newLink;//first>newLink
}
//
publicvoidinsertLast(longdd)//insertatendoflist
{
LinknewLink=newLink(dd);//makenewlink
if(isEmpty())//ifemptylist,
first=newLink;//first>newLink
else
{
last.next=newLink;//oldlast>newLink
newLink.previous=last;//oldlast<newLink
}
last=newLink;//newLink<last
}
//
publicLinkdeleteFirst()//deletefirstlink
{//(assumesnonemptylist)
Linktemp=first;
if(first.next==null)//ifonlyoneitem
last=null;//null<last
else
first.next.previous=null;//null<oldnext
first=first.next;//first>oldnext
returntemp;
}
//
publicLinkdeleteLast()//deletelastlink
{//(assumesnonemptylist)
Linktemp=last;
if(first.next==null)//ifonlyoneitem
first=null;//first>null
else
last.previous.next=null;//oldprevious>null
last=last.previous;//oldprevious<last
returntemp;
}
//
//insertddjustafterkey
publicbooleaninsertAfter(longkey,longdd)
{//(assumesnonemptylist)
Linkcurrent=first;//startatbeginning
while(current.dData!=key)//untilmatchisfound,
{
current=current.next;//movetonextlink
if(current==null)
returnfalse;//didntfindit
}
LinknewLink=newLink(dd);//makenewlink
if(current==last)//iflastlink,
{
newLink.next=null;//newLink>null
last=newLink;//newLink<last
}
else//notlastlink,
{
newLink.next=current.next;//newLink>oldnext
//newLink<oldnext
current.next.previous=newLink;
}
newLink.previous=current;//oldcurrent<newLink
current.next=newLink;//oldcurrent>newLink
returntrue;//foundit,didinsertion
}
//
publicLinkdeleteKey(longkey)//deleteitemw/givenkey
{//(assumesnonemptylist)
Linkcurrent=first;//startatbeginning
//CHAPTER5LinkedLists228
//LISTING5.8Continued
while(current.dData!=key)//untilmatchisfound,
{
current=current.next;//movetonextlink
if(current==null)
returnnull;//didntfindit
}
if(current==first)//foundit;firstitem?
first=current.next;//first>oldnext
else//notfirst
//oldprevious>oldnext
current.previous.next=current.next;
if(current==last)//lastitem?
last=current.previous;//oldprevious<last
else//notlast
//oldprevious<oldnext
current.next.previous=current.previous;
returncurrent;//returnvalue
}
//
publicvoiddisplayForward()
{
System.out.print("List(first>last):");
Linkcurrent=first;//startatbeginning
while(current!=null)//untilendoflist,
{
current.displayLink();//displaydata
current=current.next;//movetonextlink
}
System.out.println("");
}
//
publicvoiddisplayBackward()
{
System.out.print("List(last>first):");
Linkcurrent=last;//startatend
while(current!=null)//untilstartoflist,
{
current.displayLink();//displaydata
current=current.previous;//movetopreviouslink
}
System.out.println("");
}
//
}//endclassDoublyLinkedList
////////////////////////////////////////////////////////////////
classDoublyLinkedApp
{
publicstaticvoidmain(String[]args)
{//makeanewlist
DoublyLinkedListtheList=newDoublyLinkedList();
theList.insertFirst(22);//insertatfront
theList.insertFirst(44);
theList.insertFirst(66);
theList.insertLast(11);//insertatrear
theList.insertLast(33);
theList.insertLast(55);
theList.displayForward();//displaylistforward
theList.displayBackward();//displaylistbackward
theList.deleteFirst();//deletefirstitem
theList.deleteLast();//deletelastitem
theList.deleteKey(11);//deleteitemwithkey11
theList.displayForward();//displaylistforward
theList.insertAfter(22,77);//insert77after22
theList.insertAfter(33,88);//insert88after33
theList.displayForward();//displaylistforward
}//endmain()
}//endclassDoublyLinkedApp
////////////////////////////////////////////////////////////////
TugasLaporan:
BuatprogramLinkedListberbasisStack
BuatprogramLinkedListberbasisQueue
Angkaangkayangdimasukkan/dijadikandataadalahdarinomornimmasingmasing