用java实现的一个链表

随手写的一个用java实现的链表。
package JavaHash;
/**
* 这个类定义了链表中应有的属性和获取方法
* @author zyn
*
*/
public class Node {
    private Node left;
    private Node right;
    private Object obj;
    private int flag;
    public int getFlag() {
return flag;
}
public void setFlag(int flag) {
this.flag = flag;
}
public Node(Object obj){
    this.obj=obj;
    }
public Node getLeft() {
return left;
}
public void setLeft(Node left) {
this.left = left;
}
public Node getRight() {
return right;
}
public void setRight(Node right) {
this.right = right;
}
public Object getObj() {
return obj;
}
public void setObj(Object obj) {
this.obj = obj;
}
   
   
}
package JavaHash;
/**
* 建一个双向链表
* @author zyn
* 这个链表有一个致命性漏洞,当存入的数据是整形是,你搜索和删除只能用确定链表中位置的方式
* 因此此链表十分适合向其中加入不是整型的数据。
*/
public class JavaLinkList {
private Node first;//建立头结点
private Node last;
private Node move=null;
/**
* 初始化链表
*/
   public void creatLinkList(){
   first=new Node("head");
   last=new Node("last");
   first.setRight(last);
   first.setLeft(null);
   last.setRight(null);
   last.setLeft(first);
   last.setFlag(1);
   first.setFlag(0);
   move=first;
 
   }
   /**
    * 当建一个 JavaLinkList的对象时自动调用初始化方法
    */
   public JavaLinkList(){
   creatLinkList();
   }
   /**
    * 检查链表中是否有相同的数据
    * @param obj 数据
    * @return
    */
   public boolean check(Object obj){
   boolean b=true;
   int i=last.getFlag();
   for(int t=0;t<=i;t++){
   Node no=getLink(i);
   if(no.getObj().equals(obj)){
   b=false;
   }
   no=null;
   }
   return b;
   }
   /**
    * 向链表中加入元素
    * @param obj 要加入的元素
    * @return 加入元素在链表中的位置
    */
   public int add(Object obj){
   if(check(obj)){
   Node no=new Node(obj);
   move.setRight(no);
   no.setLeft(move);
   no.setRight(last);
   last.setLeft(no);
   no.setFlag(move.getFlag()+1);
   last.setFlag(move.getFlag()+2);
   move=no;
           no=null;
   }
   return move.getFlag();
 
   }
   /**
    * 删除链表中对应标记的元素
    * @param i
    */
   public boolean delete(int i){
   Node no=getLink(i);
   Node aim=no;
   boolean b=true;
   for(int j=no.getFlag()+1;j<=last.getFlag();j++){
   Node no2=getLink(j);
   no2.setFlag(no2.getFlag()-1);
   no2=null;
   }
   if(no.getLeft()!=null&&no.getRight()!=null){
   no.getLeft().setRight(no.getRight());
   no.getRight().setLeft(no.getLeft());
   no.setLeft(null);
   no.setRight(null);
   no=null;
   }else{
   b=false;
   no=null;
   }
  return b;
   }
   /**
    * 根据所存元素删除链表节点
    * @param obj 存的元素
    *
    */
   public boolean delete(Object obj){
   Node no=getLink(obj);
   Node aim=no;
   boolean b=true;
   int t=no.getFlag();
   for(int i=t+1;i<=last.getFlag();i++){
   Node no2=getLink(i);
   no2.setFlag(no.getFlag()-1);
   no2=null;
   }
   if(no.getLeft()!=null&&no.getRight()!=null){
   no.getLeft().setRight(no.getRight());
   no.getRight().setLeft(no.getLeft());
   no.setLeft(null);
   no.setRight(null);
   no=null;
   }else{
   b=false;
   }
  
   return b;
   }
  
   /**
    * 寻找链表中的元素
    * @param i 链表位置标记
    * @return 返回要寻找的链表对象
    */
   public Node getLink(int i){
   Node aim=new Node("mubiao");
   if(i>0){
   Node find=first.getRight();
   if(find.getFlag()==i){
   aim=find;
}else{
for(int j=2;j<=last.getFlag();j++){
Node no=find.getRight();
if(no.getFlag()==i){
   aim=no;
   break;
}else{
find=no;
}
no=null;
}

}
   }else{
  aim=first;
   }
   return aim;
   }
   /**
    * 根据所村内容找
    * @param obj 存的内容
    * @return 返回节点对象
    */
   public Node getLink(Object obj){
   Node aim=null;
   for(int i=0;i<=last.getFlag();i++){
   Node no=getLink(i);
   if(no.getObj()==obj){
   aim=getLink(i);
   }
   no=null;
   }
   return aim;
   }
   /**
    * 向链表中的指定位置加入新的节点。
    * @param i节点位置
    * @param obj 要加入的数据
    */
   public boolean insert(int i,Object obj){
  
   boolean b=true;
   Node no=getLink(i);
       if(no.getLeft()!=null){
       for(int j=i+1;j<=last.getFlag();j++){
       Node no2=getLink(j);
       no2.setFlag(no2.getFlag()+1);
       no2=null;
       }
       no.setFlag(no.getFlag()+1);
       Node fresh=new Node(obj);
       fresh.setLeft(no.getLeft());
       fresh.setRight(no);
       no.getLeft().setRight(fresh);
       no.setLeft(fresh);
       fresh.setFlag(i);
       no=null;
       fresh=null;

   }else{
   b=false;
   no=null;
   }
   return b;
   }
   /**
    *
    * @return 得到链表中有多少元素
    */
   public int length(){
   return last.getFlag()+1;
   }
  
  
}

你可能感兴趣的:(链表)