📄 dbllist.java
字号:
package ai.base;
import java.util.Collection;
import java.util.Iterator;
/**
* @author 赵秀成
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class DblList{
/*
* 自定义双向链表类,
* 可以用一个继承了Collection借口的容器类实例化(例如:Vector,Stack等)
* 支持前插、后插、搜索、获取子表并生成新的DblList实例
*
*/
Node head;
Node tail;
public DblList(){
head=new Node();
tail=new Node();
head.setNext(tail);
tail.setPrev(head);
}
public DblList(Collection alls){
this();
Iterator it=alls.iterator();
Node pos=head;
while(it.hasNext()){
Object cur=it.next();
pos=insAfter(pos,cur);
}
}
public Node Head(){
return head;
}
public Node Tail(){
return tail;
}
public Node insAfter(Node pos,Object _data){
if(pos==tail) return null;
Node posNext=pos.Next();
Node temp=new Node(pos,_data,posNext);
pos.setNext(temp);
posNext.setPrev(temp);
return temp;
}
public Node insBefore(Node pos,Object _data){
if(pos==head) return null;
Node posPrev=pos.Prev();
Node temp=new Node(posPrev,_data,pos);
pos.setPrev(temp);
posPrev.setNext(temp);
return temp;
}
public boolean isEmpty(){
if(head.Next()==tail) return true;
return false;
}
//to纪录遍历结尾的下一个元素
public DblList getRange(Node from,Node to,boolean forward){
if(isEmpty()) return null;
if(from==tail && forward) return null;
if(from==head && !forward) return null;
Node pos=from;
DblList lst=new DblList();
Node newFor=lst.Head();
if(forward){
while(pos!=to){
newFor=lst.insAfter(newFor,pos.Data());
pos=pos.Next();
}
}
else{
while(pos!=to){
newFor=lst.insAfter(newFor,pos.Data());
pos=pos.Prev();
}
}
return lst;
}
public DblList getRange(Node from,boolean forward){
Node to;
if(forward){
to=tail;
}
else
to=head;
return getRange(from,to,forward);
}
public Node find(Object _data){
if(isEmpty()) return null;
Node pos=head;
while(pos!=tail){
pos=pos.Next();
if(pos.Data()==_data)
return pos;
}
return null;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -