java 操作双向链表

本文介绍了一个使用Java实现的双向链表数据结构。包括链表的初始化、节点插入(头部、尾部、指定位置)、删除(头部、尾部、指定节点)等基本操作,并提供了完整的代码示例。

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();
 }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值