数据结构和算法

3.双向链表

package link;

public class DoubleLink {

 public int iData;
 public DoubleLink next;
 public DoubleLink previous;
 public DoubleLink(int data) {
  super();
  iData = data;
 }
 
 public void displayDoubleLink(){
  System.out.println("{"+iData+"}");
 }
 
 public String toString(){
  return "{"+iData+"}";
 }
}

双向双端链表

package link;

public class DoubleLinkList {

 private DoubleLink first = null;
 private DoubleLink last = null;
 public DoubleLinkList(DoubleLink first, DoubleLink last) {
  super();
  this.first = first;
  this.last = last;
 }
 
 public DoubleLinkList() {
  super();
  this.first = null;
  this.last = null;
 }
 
 public boolean isEmpty(){
  return first == null;
 }
 
 public void insertFirst(int data){
  DoubleLink dl = new DoubleLink(data);
  
  if(isEmpty()){
   first = dl;
   last = dl;
  }else{
   dl.next = first;
//   dl = first.previous;
   first.previous  = dl;
   dl.previous = null;
   first = dl;
   
  }
  
 }
 
 public void insertLast(int data){
  DoubleLink dl = new DoubleLink(data);
  if(isEmpty()){
   first = dl;
   last = dl;
  }else{
   dl.next = null;
   last.next = dl;
   dl.previous = last;
   last = dl;
  }
 }
 public void displayForward(){
  System.out.println("List (first ---> last):");
  DoubleLink d = first;
  while(d != null){
   d.displayDoubleLink();
   d = d.next;
  }
  
 }
 

public void displayBackward(){
  System.out.println("List (last ---> first):");
  DoubleLink d = last;
  while(d != null){
   d.displayDoubleLink();
   d = d.previous;
  }
  
 }


 public DoubleLink delteFirst(){
  if(isEmpty())
   return null;
  DoubleLink d = first;
  first = d.next;
  if(first == null){
   first = null;
   last = null;
  }else{
   first.previous = null;
   
  }
  return d;
 }
 
 public DoubleLink deleteLast(){
  if(isEmpty())
   return null;
  DoubleLink d = last;
  last = d.previous;
  if(last == null){
   first = null;
   last = null;
  }else{
   last.next = null;
   
  }
  return d;
 }
 
 public boolean find(int data){
  boolean r = false;
  if(isEmpty())
   return r;
  DoubleLink c = first;
  while(c != null){
   if(c.iData == data){
    r = true;
    return r;
   }
   c = c.next;
  }
  
  return r;
 }
 
 public boolean insertAfter(int key,int data){
  boolean r = false;
  if(isEmpty()){
   return r;
  }
  DoubleLink c = first;
  while(c != null){
   if(c.iData != key){
    c = c.next;
   }else{
    break;
   }
  }
  
  if(c == null){
   return r;
   
  }else{
   DoubleLink d = new DoubleLink(data);
   if( c.next == null){//如果是插在链表的末端
    last.next = d;
    d.previous = last;
    d = last;
    
    
   }else{
    d.next = c.next;
    c.next.previous = d;
    d.previous = c;
    c.next = d;
    
   }
   
   r = true;
   
  }
  return r;
 }
 
 public DoubleLink delete(int key){
  if(isEmpty())
   return null;
  
  DoubleLink c = first;
  while(c != null){
   if(c.iData != key){
    c = c.next;
   }else{
    break;
   }
  }
  if(c == first){//如果是第一个节点
   first = first.next;
   if(first == null){//如果只有一个节点
    first = null;
    last = null;
    
   }else{
    first.previous = null;
    
   }
  }else if(c == last){//如果是最后一个节点
   last = last.previous;
   if(last == null){//如果只有一个节点
    first = null;
    last = null;
   }else{
    last.next = null;
    
   }
   
   
  }else{
   c.previous.next = c.next;
   c.next.previous = c.previous;
   
  }
  return c;
 }
 
 public boolean deleteKey(int key){
  boolean r = false;
  if(isEmpty())
   return r;
  
  DoubleLink c = first;
  while(c != null){
   if(c.iData != key){
    c = c.next;
   }else{
    break;
   }
  }
  if (c == null)
   return r;
  else if(c == first){//如果是第一个节点
   first = first.next;
   if(first == null){//如果只有一个节点
    first = null;
    last = null;
    
   }else{
    first.previous = null;
    
   }
  }else if(c == last){//如果是最后一个节点
   last = last.previous;
   if(last == null){//如果只有一个节点
    first = null;
    last = null;
   }else{
    last.next = null;
    
   }
   
   
  }else{
   c.previous.next = c.next;
   c.next.previous = c.previous;
   
  }
  r = true;
  return r;
 }
}

package link;

public class DoubleLinkApp {

 public static void main(String[] args){
  DoubleLinkList dl = new DoubleLinkList();
//  dl.insertFirst(2);
//  dl.insertFirst(3);
//  dl.insertFirst(31);
//  dl.insertFirst(1);
//  dl.insertFirst(11);
//  dl.displayForward();
  
  dl.insertLast(4);
//  dl.insertLast(41);
//  dl.insertLast(45);
//  dl.displayForward();
//  dl.delteFirst();
//  dl.displayForward();
//  dl.insertLast(41);
  dl.insertLast(411);
//  dl.insertLast(1);
//  dl.deleteLast();
  dl.displayForward();
  
//  boolean b1 = dl.find(41);
//  if(b1)
//   System.out.println("find :"+b1);
//  else
//   System.out.println("not find :"+b1);
//  
  boolean b1 = dl.insertAfter(4, 444);
//  if(b1)
//   System.out.println("find :"+b1);
//  else
//   System.out.println("not find :"+b1);
//   
  dl.displayForward();
  
  boolean b = dl.deleteKey(444);
  dl.displayForward();
  if(b)
   System.out.println("delete :"+b);
  else
   System.out.println("not delete :"+b);
  dl.displayForward();
 }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值