JAVA实现链表双向链表.docx
《JAVA实现链表双向链表.docx》由会员分享,可在线阅读,更多相关《JAVA实现链表双向链表.docx(14页珍藏版)》请在冰豆网上搜索。
JAVA实现链表双向链表
JAVA实现链表,双向链表.txt你无法改变别人,但你可以改变自己;你无法改变天气,但你可以改变心情;你无法改变生命长度,但你可以拓展它的宽度。
classListNode{
//friendlydatasoclasslistcanaccessitdirectly
Objectdata;
ListNodenext;
ListNode(Objecto)
{
data=o;
next=null;
}
//Constructor:
CreateaListNodethatreferstoObjectoandtothe
//nextListNodeintheList.
ListNode(Objecto,ListNodenextNode)
{
data=o;
next=nextNode;
}
//ReturntheObjectinthisnode
ObjectgetObject()
{
returndata;
}
ListNodegetnext()
{
returnnext;
}
}
//ClassListdefinition
classList{
privateListNodefirstNode;
privateListNodelastNode;
privateStringname;//Stringlike"list"usedinprinting
publicList(Strings)
{
name=s;
firstNode=lastNode=null;
}
//Constructor:
ConstructoranemptyListwith"List"asthename
publicList(){
this("list");
}
//InsertanObjectatthefrontoftheListIfListisempty,firstNode
//andlastNoderefertosameObject.Otherwise,firstNodereferstonewnode.
publicvoidinsertAtFront(ObjectinsertItem)
{
if(isEmpty())//如果链表为空,返回true,否则返回false.
firstNode=lastNode=newListNode(insertItem);
else
firstNode=newListNode(insertItem,firstNode);
}
//InsertanObjectattheendoftheListIfListisempty,firstNodeand
//lastNoderefertosameObject.Otherwise,lastNode'snextinstancevariablereferstonewnode.
publicvoidinsertAtBack(ObjectinsertItem)
{
if(isEmpty())
firstNode=lastNode=newListNode(insertItem);
else
lastNode=lastNode.next=newListNode(insertItem);
}
//RemovethefirstnodefromtheList.
publicObjectremoveFromFront()throwsEmptyListException
{
ObjectremoveItem=null;
if(isEmpty())
thrownewEmptyListException(name);
removeItem=firstNode.data;//retrievethedataresetthefirstNodeandlastNodereferences
if(firstNode.equals(lastNode))
firstNode=lastNode=null;
else
firstNode=firstNode.next;
returnremoveItem;
}
//RemovethelastnodefromtheList
publicObjectremoveFromBack()throwsEmptyListException
{
ObjectremoveItem=null;
if(isEmpty())
thrownewEmptyListException(name);
removeItem=lastNode.data;//retrievethedataresetthefirstNodeandlastNodereferences
if(firstNode.equals(lastNode))
firstNode=lastNode=null;
else
{
ListNodecurrent=firstNode;
while(current.next!
=lastNode){
current=current.next;
}
lastNode=current;
current.next=null;
}
returnremoveItem;
}
//ReturntrueiftheListisempty.
publicbooleanisEmpty(){
returnfirstNode==null;
}
//OutputtheListcontents
publicvoidprint()
{
if(isEmpty()){
System.out.println("Empty"+name);
return;
}
System.out.print("The"+name+"is:
");
ListNodecurrent=firstNode;
while(current!
=null){
System.out.print(current.data.toString()+"");
current=current.next;
}
System.out.println();
System.out.println();
}
}
//ClassEmptyListExceptiondefinition
classEmptyListExceptionextendsRuntimeException{
publicEmptyListException(Stringname)
{
super("The"+name+"isempty");
}
}
//ClassListTest
publicclassListTest{
publicstaticvoidmain(String[]args){
ListobjList=newList();
//CreateobjecttostoreintheList
Booleanb=newBoolean(true);
Characterc=newCharacter('$');
Integeri=newInteger(34567);
Strings=newString("hello");
//UsetheListinsertmethods
objList.insertAtFront(b);
objList.print();
objList.insertAtFront(c);
objList.print();
objList.insertAtBack(i);
objList.print();
objList.insertAtBack(s);
objList.print();
//UsetheListremovemethods
ObjectremovedObj;
try{
removedObj=objList.removeFromFront();
System.out.println(removedObj.toString()+"removed");
objList.print();
removedObj=objList.removeFromFront();
System.out.println(removedObj.toString()+"removed");
objList.print();
removedObj=objList.removeFromBack();
System.out.println(removedObj.toString()+"removed");
objList.print();
removedObj=objList.removeFromBack();
System.out.println(removedObj.toString()+"removed");
objList.print();
}
catch(EmptyListExceptione){
System.err.println("\n"+e.toString());
}
}
}
packageLinkedList;
importjava.util.Iterator;
importjava.util.ListIterator;
importjava.util.NoSuchElementException;
publicclassMyLinkedList{
//*************************************************************
privateDNodeheader;
privateintlistSize;
//*************************************************************
publicMyLinkedList(){
header=newDNode();
listSize=0;
}
//*************************************************************
privatestaticclassDNode{
TnodeValue;
DNodeprev;
DNodenext;
publicDNode(){//forheader
nodeValue=null;
prev=this;//left
next=this;//right
}
publicDNode(Titem){
nodeValue=item;
prev=this;
next=this;
}
}
//**************************************************************
publicbooleanisEmpty(){
return(header.prev==header||header.next==header);
}
publicintsize(){
returnlistSize;
}
//**************************************************************
privateDNodeaddBefore(DNodecurr,Titem){
DNodenewNode,prevNode;
newNode=newDNode(item);
prevNode=curr.prev;
newNode.prev=prevNode;
newNode.next=curr;
prevNode.next=newNode;
curr.prev=newNode;
returnnewNode;
}
publicbooleanadd(Titem){
addBefore(header,item);
listSize++;
returntrue;
}
publicvoidaddFirst(Titem){
addBefore(header.next,item);
listSize++;
}
publicvoidaddLast(Titem){
addBefore(header,item);
listSize++;
}
//**************************************************************
privatevoidremove(DNodecurr){
if(curr.next==curr)return;
DNodeprevNode=curr.prev,nextNode=curr.next;
prevNode.next=nextNode;
nextNode.prev=prevNode;
}
publicbooleanremove(Objecto){
for(DNodep=header.next;p!
=header;p=p.next){
if(o.equals(p.nodeValue)){
remove(p);
listSize--;
returntrue;
}
}
returnfalse;
}
//**************************************************************
publicvoidprintList(){
for(DNodep=header.next;p!
=header;p=p.next)
System.out.println(p.nodeValue);
}
//**************************************************************
privateclassMyIteratorimplementsIterator{
publicDNodenextNode=header.next;
publicDNodelastReturned=header;
publicbooleanhasNext(){
returnnextNode!
=header;
}
publicTnext(){
if(nextNode==header)
thrownewNoSuchElementException("");
lastReturned=nextNode;
nextNode=nextNode.next;
returnlastReturned.nodeValue;
}
publicvoidremove(){
if(lastReturned==header)
thrownewIllegalStateException("");
MyLinkedList.this.remove(lastReturned);
lastReturned=header;
listSize--;
}
}
//**************************************************************
privateclassMyListIteratorextendsMyIteratorimplementsListIterator{
privateintnextIndex;
MyListIterator(intindex){
if(index<0||index>listSize)
thrownewIndexOutOfBoundsException("");
//如果index小于listSize/2,就从表头开始查找定位,否则就从表尾开始查找定位
if(index<(listSize>>1)){
nextNode=header.next;
for(nextIndex=0;nextIndexnextNode=nextNode.next;
}else{
nextNode=header;
for(nextIndex=listSize;nextIndex>index;nextIndex--)
nextNode=nextNode.prev;
}
}
publicbooleanhasPrevious(){
returnnextIndex!
=0;
//returnnextNode.prev!
=header;
}
publicTprevious(){
if(nextIndex==0)
thrownewNoSuchElementException("no");
lastReturned=nextNode=nextNode.prev;
nextIndex--;
returnlastReturned.nodeValue;
}
publicvoidremove(){
if(lastReturned==header)
thrownewIllegalStateException("");
MyLinkedList.this.remove(lastReturned);
nextIndex--;
listSize--;
if(lastReturned==nextNode)
nextNode=nextNode.next;
lastReturned=header;
}
publicvoidadd(Titem){
MyLinkedList.this.addBefore(nextNode,item);
nextIndex++;
listSize++;
lastReturned=header;
}
publicvoidset(Titem){
if(lastReturned==header)
thrownewIllegalStateException();
lastReturned.nodeValue=item;
}
publicintnextIndex(){
returnnextIndex;
}
publicintpreviousIndex(){
returnnextIndex-1;
}
}
//**************************************************************
publicIteratoriterator(){
returnnewMyIterator();
}
//**************************************************************
publicListIteratorlistIterator(intindex){
returnnewMyListIterator(index);
}
//**************************************************************
publicstaticvoidmain(String[]args){
MyLinkedListt=newMyLinkedList();
t.add("A");
t.add("B");
t.add("C");
t.add("D");
//t.remove("B");
//t.addFirst("AA");
//t.addLast("BB");
//t.printList();
/*
Iteratorit=t.iterator();
while(it.hasNext())System.out.println(it.next());//ABCD
*/
ListIteratorit=t.listIterator(t.size());
while(it.hasPrevious()){
System.out.println(it.previous());//DCBA
}
}
}//MyLinkedListend~