java 实现双向链表实例详解
java实现双向链表实例详解
双向链表是一个基本的数据结构,在Java中LinkedList已经实现了这种结构,不过作为开发者,也要拥有自己显示这种结构的能力。话不多说,上代码:
首先是链表的节点类:
/** *链表节点 *@authorAdministrator * *@param*/ publicclassChainNode { privateTdata; //对象编号 privateintdataNo; publicChainNode nextChainNode; publicChainNode preChainNode; publicChainNode(Tdata,ChainNode nextChainNode, ChainNode preChainNode){ this.data=data; this.nextChainNode=nextChainNode; this.preChainNode=preChainNode; } publicChainNode(Tdata){ this.data=data; } publicintgetDataNo(){ returndataNo; } publicvoidsetDataNo(intdataNo){ this.dataNo=dataNo; } publicvoidsetData(Tdata){ this.data=data; } publicTgetData(){ returndata; } }
然后是链表:
/** *链表实现类 *@authorAdministrator * *@param */ publicclassChain { //头节点 privateChainNode headNode; //末尾节点指针 privateChainNode lastNode; privateintsize; //创建链表就创建头节点 publicChain(){ this.headNode=newChainNode (null); this.lastNode=headNode; } //增加一个节点 publicvoidaddNode(Tdata){ ChainNode node=newChainNode (data); if(lastNode!=null){ lastNode.nextChainNode=node; node.preChainNode=node; node.setDataNo(size); lastNode=node; size++; } } //删除指定索引的节点 publicvoiddeleteNode(intdataNo)throwsException{ if(getSize()==0){ thrownewException("chainisempty"); } for(ChainNode node=headNode.nextChainNode;node!=null;node=node.nextChainNode){ if(node.getDataNo()==dataNo){ node.preChainNode.nextChainNode=node.nextChainNode; if(node.nextChainNode!=null){ node.nextChainNode.preChainNode=node.preChainNode; } node.nextChainNode=null; node.preChainNode=null; size--; //重新设置节点的编号 for(ChainNode chainNode=node.nextChainNode;chainNode!=null;chainNode=chainNode.nextChainNode){ chainNode.setDataNo(chainNode.getDataNo()-1); } return; } } thrownewException("thecorrespondingdatathatcannotbefound"); } //获取指定索引的节点 publicTget(intdataNo)throwsException{ if(getSize()==0){ thrownewException("chainisempty"); } for(ChainNode node=headNode.nextChainNode;node!=null;node=node.nextChainNode){ if(node.getDataNo()==dataNo){ returnnode.getData(); } } thrownewException("thecorrespondingdatathatcannotbefound"); } //修改对应索引的节点 publicvoidset(intdataNo,Tdata)throwsException{ if(getSize()==0){ thrownewException("chainisempty"); } for(ChainNode node=headNode.nextChainNode;node!=null;node=node.nextChainNode){ if(node.getDataNo()==dataNo){ node.setData(data); return; } } thrownewException("thedatathatistobemodifiedcannotbefound"); } //是否包含对应数据的节点 publicbooleanisContains(Tdata)throwsException{ if(getSize()==0){ thrownewException("chainisempty"); } for(ChainNode chainNode=headNode.nextChainNode;chainNode!=null;chainNode=chainNode.nextChainNode){ if(chainNode.getData()==data){ returntrue; } } returnfalse; } //获取节点数量(不含头节点) publicintgetSize(){ returnsize; } }
测试一下:
publicclassChainTest{
publicstaticvoidmain(String[]args)throwsException{
StringoneString="one";
StringtwoString="two";
StringthreeString="three";
StringfourString="four";
Chainchain=newChain();
chain.addNode(oneString);
chain.addNode(twoString);
chain.addNode(threeString);
chain.addNode(fourString);
for(inti=0;i
结果:
one
two
three
four
thedataofthesecondnodeisthree
true
modifychain
one
haha
three
four
deleteonenode
one
haha
three
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
热门推荐
10 祝女儿简短祝福语大全
11 大学新年祝福语简短创意
12 元旦适合的祝福语简短
13 朋友出远门祝福语简短
14 初六简短的祝福语
15 祝男孩生日祝福语简短
16 同事调离的祝福语简短
17 拜年红包的祝福语简短
18 妈妈生日祝福语简短励志