栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java数据结构之实现双向链表的示例

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java数据结构之实现双向链表的示例

复制代码 代码如下:

public class DoubleNodeList {
 //节点类
 private static class Node{
  Node perv;  //前节点
  Node next;  //后节点
  T data;    //数据

  public Node(T t){
   this.data = t;
  }
 }
 private Node head;  //头节点
 private Node last;  //尾节点
 private Node other;  //备用节点存放临时操作
 private int length;  //链表长度

 
 public DoubleNodeList(){
  head = new Node(null);
  last = head;
  length = 0;
 }

 
 public DoubleNodeList(T data){
  head = new Node(data);
  last = head;
  length = 1;
 }

 
 public void add(T data){
  if(isEmpty()){
   head = new Node(data);
   last = head;
   length++;
  }else{
   //尾插法
   other = new Node(data);
   other.perv = last;
   last.next = other;
   last = other;
   length++;
  }
 }

 
 public boolean addAfert(T data , T insertData){
  other = head;
  while(other != null){
   if(other.data.equals(data)){
    Node t = new Node(insertData);
    t.perv = other;
    t.next = other.next;
    other.next = t;
    //判断是否在最后一个节点后添加节点
    if(t.next==null){
     last = t;
    }
    length++;
    return true;
   }
   other = other.next;
  }
  return false;
 }

 
 public boolean addBefore(T data, T insertData){
  other = head;
  while(other != null){
   if(other.data.equals(data)){
    Node t = new Node(insertData);
    t.perv = other.perv;
    t.next = other;
    other.perv.next = t;
    length++;
    return true;
   }
   other = other.next;
  }
  return false;
 }

 
 public T get(int index){
  if(index>length || index<0){
   throw new IndexOutOfBoundsException("索引越界:"+index);
  }
  other = head;
  for(int i=0;i   other = other.next;
  }
  return other.data;
 }

 
 public boolean set(T oldValue,T newValue){
  other = head;
  while(other!=null){
   if(other.data.equals(oldValue)){
    other.data = newValue;
    return true;
   }
   other = other.next;
  }
  return false;
 }

 
 public boolean remove(T data){
  other = head;
  while(other != null){
   if(other.data.equals(data)){
    other.perv.next = other.next;
    length--;
    return true;
   }
   other = other.next;
  }
  return false;
 }

 
 public boolean contains(T data){
  other = head;
  while(other != null){
   if(other.data.equals(data)){
    return true;
   }
   other = other.next;
  }
  return false;
 }

 
 public T getLast(){
  return last.data;
 }

 
 public T getFirst(){
  return head.data;
 }

 
 public int getSize(){
  return length;
 }

 
 public boolean isEmpty(){
  return length==0;
 }

 
 public void clear(){
  head = null;
  length = 0;
 }

 
 public void printList(){
  if(isEmpty()){
   System.out.println("空链表");
  }else{
   other = head;
   for(int i=0;i    System.out.print(other.data+" ");
    other = other.next;
   }
   System.out.println();
  }
 }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/152331.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号