现在对Java原生数据结构特感兴趣,于是决定自己动手实现一些类,比如这个List类,在熟习了单链表和双链表的数据结构之后,终于实现了,代码如下:
源码SingleLinkedList.java
packagehk.inso.service;
/**
*CreatedbyIntelliJIDEA.
*Date:8/10/156:09PM
*Author:Richard
*/
publicclassSingleLinkedList< E>{
privatestaticclassNode< E>{
private E element;
privateNode next;
publicNode( Eelement){
//constructorwithargs
this. element=element;
}
publicNode(){
//constructorwithoutargs
}
@Override
publicStringtoString(){
return element.toString();
}
}
privateNode first;
privateNode last;
/**
*添加元素到链表头部
* @param element
*/
publicvoidadd( Eelement){
Node< E>node= newNode< E>(element);
linkFirst(node);
}
/**
*添加元素到链表尾部
* @param element
*/
publicvoidaddLast( Eelement){
Node< E>node= newNode< E>(element);
linkLast(node);
}
/**
*根据索引移除元素
* @param index
*/
publicvoidremove( intindex){
if(index== 0){
unlinkFirst();
}
else{
Nodenode=getNode(index);
unlink(node);
}
}
/**
*移除链表中最后一个元素
*/
publicvoidremoveLast(){
unlinkLast();
}
/**
*将元素插入指定链表索引为止
* @param index
* @param element
*/
publicvoidinsert( intindex,Eelement){
Node< E>node1= newNode< E>(element);
if(index== 0){
linkFirst(node1);
} else{
Nodenode2=getNode(index);
link(node1,node2);
}
}
/**
*获取某个元素
* @param index
* @return
*/
public Eget( intindex){
Node< E>node=getNode(index);
Eelement=node. element;
returnelement;
}
/**
*统计链表里面所有元素的个数
* @return 总的元素的个数
*/
publicintsize(){
intcount= 0;
Nodenode= first;
while(node!= null){
count++;
node=node. next;
}
returncount;
}
/**
*toString方法实现
* @return
*/
@Override
publicStringtoString(){
Nodenode= first;
StringBuilderstringBuilder= newStringBuilder();
if(node== null){
return "{}";
}
while(node!= null){
stringBuilder.append( "["+node. element.toString()+ "],");
node=node. next;
}
Stringresult=stringBuilder.toString();
intindex=result.lastIndexOf( ",");
returnresult.substring( 0,index);
}
/**
*插入结点到链表中间
*插入前
*node2->node3->node4
*插入后
*node2->node1->node3->node4
* @param node1 要插入的结点
* @param node2 插入结点位置结点
*/
privatevoidlink(Nodenode1,Nodenode2){
node1. next=node2;
NodeprevNode=getPrevNode(node2);
prevNode. next=node1;
}
/**
*插入结点到链表首部
* @param node
*/
privatevoidlinkFirst(Nodenode){
if( first== null|| last== null){
first= last=node;
} else{
node. next= first;
first=node;
}
}
/**
*插入结点到链表尾部
* @param node
*/
privatevoidlinkLast(Nodenode){
last. next=node;
last=node;
}
/**
*从链表中间移除当前结点
* @param node
*/
privatevoidunlink(Nodenode){
NodeprevNode=getPrevNode(node);
prevNode. next=node. next;
}
/**
*移除头部的结点
*/
privatevoidunlinkFirst(){
first= first. next;
}
/**
*移除尾部的结点
*/
privatevoidunlinkLast(){
NodeprevNode=getPrevNode( last);
prevNode. next= null;
last=prevNode;
}
/**
*寻找当前结点的前一个结点
* @param node 当前结点
* @return
*/
privateNodegetPrevNode(Nodenode){
//这里多走了一步
//NodenodeItr=first.next;
NodenodeItr= first;
NodeprevNode= newNode();
while(!nodeItr.equals(node)){
prevNode=nodeItr;
nodeItr=nodeItr. next;
}
returnprevNode;
}
/**
*根据索引查找链表中得数据
* @param index
* @return 链表结点
*/
privateNodegetNode( intindex){
Nodenode= first;
NodecurrentNode= newNode();
if(index== 0){
//continue
}
if(index>=size()){
thrownewIndexOutOfBoundsException(errMessage(index));
}
else{
inti= 0;
while(i<=index){
if(node!= null){
currentNode=node;
//注意这里有递增
node=node. next;
}
i++;
}
}
returncurrentNode;
}
/**
*错误消息显示
* @param index
* @return
*/
privateStringerrMessage( intindex){
return "Size:"+size()+ ",index:"+index;
}
}
测试方法TestSingleLinkedList.java
packagehk.inso.test;
importhk.inso.service.SingleLinkedList;
/**
*CreatedbyIntelliJIDEA.
*Date:8/11/154:58PM
*Author:Richard
*/
publicclassTestSingleLinkedList{
publicstaticvoidmain(String[]args){
SingleLinkedList<User>list= newSingleLinkedList<User>();
list.add( newUser( 5,"Jimmy","Girl"));
list.add( newUser( 7,"Tom","Boy"));
list.addLast( newUser( 6,"Amy","Girl"));
System. out.println(list.toString());
}
}
classUser{
privateint age;
privateString name;
privateString gender;
publicUser( intage,Stringname,Stringgender){
this. age=age;
this. name=name;
this. gender=gender;
}
publicintgetAge(){
return age;
}
publicvoidsetAge( intage){
this. age=age;
}
publicStringgetName(){
return name;
}
publicvoidsetName(Stringname){
this. name=name;
}
publicStringgetGender(){
return gender;
}
publicvoidsetGender(Stringgender){
this. gender=gender;
}
@Override
publicStringtoString(){
return "User{"+
"age="+ age+
",name='"+ name+ ' \' '+
",gender='"+ gender+ ' \' '+
'}';
}
importhk.inso.service.SingleLinkedList;
/**
*CreatedbyIntelliJIDEA.
*Date:8/11/154:58PM
*Author:Richard
*/
publicclassTestSingleLinkedList{
publicstaticvoidmain(String[]args){
SingleLinkedList<User>list= newSingleLinkedList<User>();
list.add( newUser( 5,"Jimmy","Girl"));
list.add( newUser( 7,"Tom","Boy"));
list.addLast( newUser( 6,"Amy","Girl"));
System. out.println(list.toString());
}
}
classUser{
privateint age;
privateString name;
privateString gender;
publicUser( intage,Stringname,Stringgender){
this. age=age;
this. name=name;
this. gender=gender;
}
publicintgetAge(){
return age;
}
publicvoidsetAge( intage){
this. age=age;
}
publicStringgetName(){
return name;
}
publicvoidsetName(Stringname){
this. name=name;
}
publicStringgetGender(){
return gender;
}
publicvoidsetGender(Stringgender){
this. gender=gender;
}
@Override
publicStringtoString(){
return "User{"+
"age="+ age+
",name='"+ name+ ' \' '+
",gender='"+ gender+ ' \' '+
'}';
}
}
原文链接:https://www.f2er.com/javaschema/284546.html