欢迎来到天天文库
浏览记录
ID:17957500
大小:51.00 KB
页数:16页
时间:2018-09-11
《java实现链表,双向链表》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、JAVA实现链表,双向链表JAVA实现链表,双向链表.txt你无法改变别人,但你可以改变自己;你无法改变天气,但你可以改变心情;你无法改变生命长度,但你可以拓展它的宽度。classListNode{//friendlydatasoclasslistcanaccessitdirectlyObjectdata;ListNodenext;ListNode(Objecto){data=o;next=null;}//Constructor:CreateaListNodethatreferstoObjectoandtothe//nextListNodei
2、ntheList.ListNode(Objecto,ListNodenextNode){data=o;next=nextNode;}//ReturntheObjectinthisnodeObjectgetObject(){returndata;}ListNodegetnext(){returnnext;}}//ClassListdefinitionclassList{privateListNodefirstNode;privateListNodelastNode;privateStringname;//Stringlike"list"used
3、inprintingpublicList(Strings){name=s;firstNode=lastNode=null;}//Constructor:ConstructoranemptyListwith"List"asthenamepublicList(){this("list");}//InsertanObjectatthefrontoftheListIfListisempty,firstNode//andlastNoderefertosameObject.Otherwise,firstNodereferstonewnode.public
4、voidinsertAtFront(ObjectinsertItem){if(isEmpty())//如果链表为空,返回true,否则返回false.firstNode=lastNode=newListNode(insertItem);elsefirstNode=newListNode(insertItem,firstNode);}//InsertanObjectattheendoftheListIfListisempty,firstNodeand//lastNoderefertosameObject.Otherwise,lastNode's
5、nextinstancevariablereferstonewnode.publicvoidinsertAtBack(ObjectinsertItem){if(isEmpty())firstNode=lastNode=newListNode(insertItem);elselastNode=lastNode.next=newListNode(insertItem);}//RemovethefirstnodefromtheList.publicObjectremoveFromFront()throwsEmptyListException{Obj
6、ectremoveItem=null;if(isEmpty())thrownewEmptyListException(name);removeItem=firstNode.data;//retrievethedataresetthefirstNodeandlastNodereferencesif(firstNode.equals(lastNode))firstNode=lastNode=null;elsefirstNode=firstNode.next;returnremoveItem;}//RemovethelastnodefromtheL
7、istpublicObjectremoveFromBack()throwsEmptyListException{ObjectremoveItem=null;if(isEmpty())thrownewEmptyListException(name);removeItem=lastNode.data;//retrievethedataresetthefirstNodeandlastNodereferencesif(firstNode.equals(lastNode))firstNode=lastNode=null;else{ListNodecur
8、rent=firstNode;while(current.next!=lastNode){current=current.next;}lastNode=curren
此文档下载收益归作者所有