您的位置:首页 > 编程语言 > Java开发

java 操作双向链表

2014-10-16 21:44 344 查看
java 操作双向链表

最近在复习数据结构,看了链表的操作,但是书上是用C实现的,我本人用java实现了双向

链表的操作,现在贴出代码与大家分享一下:

package com.qgmobile.list;

/**

 * 双向链表节点

 * @author yangchuxi

 *

 */

public class TwoWayNode<Object> {

  public Object t;

  //后继

  public TwoWayNode<Object> next;

  //前驱

  public TwoWayNode<Object> pre;

  public TwoWayNode(Object t){

   this.t=t;

  }

  public String toString(){

   return t.toString();

  }

  

}

 

 

package com.qgmobile.list;

/**

 * 对双向链表的操作

 * @author yangchuxi

 *

 */

public class TwoWayList {

  private TwoWayNode<Object> head;

  private int size;

  /**

   * 链表的初始化

   */

  public TwoWayList() {

   size=0;

   //表头不放数据

   head=new TwoWayNode<Object>(null);

   head.next=null;

   head.pre=null;

  }

  //插入到链表前段(表头之后)

  public boolean insertfirst(TwoWayNode<Object> tn){

   if(tn!=null){

    //首先要先把插入节点的前驱和后继搞定先,注意第二句和第三局的顺序

    if(head.next!=null){

     tn.pre=head;

     tn.next=head.next;

        head.next.pre=tn;

     head.next=tn; 

    }else{

     head.next=tn;

     tn.pre=head;

    }

    size++;

    return true;

   }

   return false;

  }

 //插入到链表的末尾

  public boolean insertlast(TwoWayNode<Object> tn){

   if(tn!=null){

    TwoWayNode<Object> current=head;

    while(current.next!=null){

     current=current.next;

    }

    current.next=tn;

    tn.pre=current;

    size++;

    return true;

   }

   return false;

  }

  // 在指定节点后添加节点

  public boolean insertinto(TwoWayNode<Object> tn1,TwoWayNode<Object> tn2){

   if(tn1!=null && tn2!=null){

    //首先遍历这个链表,看看有没有等于tn1

    TwoWayNode<Object> current=head;

    while(current!=tn1){

     current=current.next;

     if(current==null){

      return false;

     }

    }

    if(tn1==head){

     insertfirst(tn2);

     size++;

     return true;

    }

    if(current.next==null){

     insertlast(tn2);

     size++;

     return true;

    }

    tn2.pre=current;

    tn2.next=current.next;

    current.next.pre=tn2;

    current.next=tn2;

    size++;

    return true;

   }

   return false;

  }

    //删除链表前端节点  

  public boolean  deletehead(){

   if(head.next!=null){

    TwoWayNode<Object> current=head.next;

    head.next=current.next;

    current.next.pre=head;

    current=null;

    size--;

    return true;

   }

   return false;

  }

  // 删除尾节点  

  public void deltetail(){

   TwoWayNode<Object> current=head;

   while(current.next!=null){

    current=current.next;

   }

   TwoWayNode<Object> pre=current.pre;

   pre.next=null;

   current=null;

   size--;

  }

  //删除指定节点

  public boolean deletenode(TwoWayNode<Object> tn){

   if(tn!=null){

    TwoWayNode<Object> current=head;

    while(current!=tn){

     current=current.next;

     if(current==null){

      return false;

     }

    }

    if(tn==head.next){

    return  deletehead();

    }

    //保存与tn相等的值

    TwoWayNode<Object> acurrent=current;

    while(current.next!=null){

     current=current.next;

    }

    if(current==tn){

    deltetail();

    return true;

    }

    TwoWayNode<Object> pre=acurrent.pre;

    TwoWayNode<Object> next=acurrent.next;

    pre.next=next;

    next.pre=pre;

    acurrent=null;

    size--;

    return true;

   }

   return false;

  }

  // 链表长度   

  public int getSize() {   

      return size;   

  }   

  // 正序和反序遍历遍历链表并打印   

  public void diplay() {   

      TwoWayNode<Object> current = head.next;   

      TwoWayNode<Object> tail=null;

      System.out.println("正序输出:");

      while (current != null) {   

          System.out.println(current.toString());   

          tail=current;

          current = current.next;   

      }   

     // System.out.println("tail"+tail.toString());

      System.out.println("反序输出:");

      while(tail!=head){

       if(tail!=null){

        

        System.out.println(tail.toString());  

       }

      tail=tail.pre;

      }

  }   

    public static void main(String[] args) {

  TwoWayList twoWayList=new TwoWayList();

  TwoWayNode<Object> node1=new TwoWayNode<Object>("test1");

  TwoWayNode<Object> node2=new TwoWayNode<Object>("test2");

  TwoWayNode<Object> node3=new TwoWayNode<Object>("test3");

  TwoWayNode<Object> node4=new TwoWayNode<Object>("test4");

  //twoWayList.insertlast(node4);

  twoWayList.insertfirst(node1);

  twoWayList.insertlast(node2);

  twoWayList.insertfirst(node3);

  twoWayList.insertinto(node2, node4);

  twoWayList.diplay();

  //twoWayList.deletehead();

  //twoWayList.deltetail();

  twoWayList.deletenode(node2);

  twoWayList.diplay();

 }

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: