Java数据结构之链表(动力节点之Java学院整理)

 更新时间:2017年04月14日 10:01:29   投稿:mrr  
这篇文章主要介绍了Java数据结构之链表(动力节点之Java学院整理)的相关资料,需要的朋友可以参考下

单链表:

insertFirst:在表头插入一个新的链接点,时间复杂度为O(1)

deleteFirst:删除表头的链接点,时间复杂度为O(1)

find:查找包含指定关键字的链接点,由于需要遍历查找,平均需要查找N/2次,即O(N)

remove:删除包含指定关键字的链接点,由于需要遍历查找,平均需要查找N/2次,即O(N) 

public class LinkedList { 
   private class Data{ 
     private Object obj; 
     private Data next = null;       
     Data(Object obj){ 
       this.obj = obj; 
     } 
   } 
   private Data first = null; 
    
   public void insertFirst(Object obj){ 
     Data data = new Data(obj); 
     data.next = first; 
     first = data; 
   } 
   public Object deleteFirst() throws Exception{ 
     if(first == null) 
       throw new Exception("empty!"); 
     Data temp = first; 
     first = first.next; 
     return temp.obj; 
   }     
   public Object find(Object obj) throws Exception{ 
     if(first == null) 
       throw new Exception("LinkedList is empty!"); 
     Data cur = first; 
     while(cur != null){ 
       if(cur.obj.equals(obj)){ 
         return cur.obj; 
       } 
       cur = cur.next; 
     } 
     return null; 
   } 
   public void remove(Object obj) throws Exception{ 
     if(first == null) 
       throw new Exception("LinkedList is empty!"); 
     if(first.obj.equals(obj)){ 
       first = first.next; 
     }else{ 
       Data pre = first; 
       Data cur = first.next; 
       while(cur != null){ 
         if(cur.obj.equals(obj)){ 
           pre.next = cur.next; 
         } 
        pre = cur; 
         cur = cur.next; 
       } 
     } 
   } 
   public boolean isEmpty(){ 
     return (first == null); 
   } 
   public void display(){ 
     if(first == null) 
       System.out.println("empty"); 
     Data cur = first; 
     while(cur != null){ 
       System.out.print(cur.obj.toString() + " -> "); 
       cur = cur.next; 
     } 
     System.out.print("\n"); 
   }     
   public static void main(String[] args) throws Exception { 
     LinkedList ll = new LinkedList(); 
     ll.insertFirst(4); 
     ll.insertFirst(3); 
     ll.insertFirst(2); 
     ll.insertFirst(1); 
     ll.display(); 
     ll.deleteFirst(); 
     ll.display(); 
     ll.remove(3); 
     ll.display(); 
     System.out.println(ll.find(1)); 
     System.out.println(ll.find(4)); 
   } 
 } 
 1 -> 2 -> 3 -> 4 ->  
 2 -> 3 -> 4 ->  
 2 -> 4 ->  
 null 
 4 

双端链表(不是双向链表):

与单向链表的不同之处在保存有对最后一个链接点的引用(last)

insertFirst:在表头插入一个新的链接点,时间复杂度O(1)

insertLast:在表尾插入一个新的链接点,时间复杂度O(1)

deleteFirst:删除表头的链接点,时间复杂度O(1)

deleteLast::删除表尾的链接点,由于只保存了表尾的链接点,而没有保存表尾的前一个链接点(这里就体现出双向链表的优势了),所以在删除表尾链接点时需要遍历以找到表尾链接点的前一个链接点,需查找N-1次,也就是O(N)
有了这几个方法就可以用双端链表来实现一个队列了

 public class FirstLastList { 
   private class Data{ 
     private Object obj; 
     private Data next = null;       
     Data(Object obj){ 
       this.obj = obj; 
     } 
   }     
   private Data first = null; 
   private Data last = null;     
   public void insertFirst(Object obj){ 
     Data data = new Data(obj); 
     if(first == null) 
       last = data; 
     data.next = first; 
     first = data; 
   }     
   public void insertLast(Object obj){ 
     Data data = new Data(obj); 
     if(first == null){ 
       first = data; 
     }else{ 
       last.next = data;   
     } 
     last = data; 
   }     
   public Object deleteFirst() throws Exception{ 
      if(first == null) 
       throw new Exception("empty"); 
      Data temp = first; 
      if(first.next == null) 
       last = null; 
      first = first.next; 
      return temp.obj; 
  }   
   public void deleteLast() throws Exception{ 
     if(first == null) 
       throw new Exception("empty"); 
     if(first.next == null){ 
       first = null; 
       last = null; 
     }else{ 
       Data temp = first; 
       while(temp.next != null){ 
         if(temp.next == last){ 
           last = temp; 
           last.next = null; 
           break; 
        } 
        temp = temp.next; 
      } 
     } 
   } 
   public void display(){ 
     if(first == null) 
       System.out.println("empty"); 
     Data cur = first; 
     while(cur != null){ 
       System.out.print(cur.obj.toString() + " -> "); 
       cur = cur.next; 
     } 
     System.out.print("\n"); 
   } 
   public static void main(String[] args) throws Exception { 
     FirstLastList fll = new FirstLastList(); 
     fll.insertFirst(2); 
     fll.insertFirst(1); 
     fll.display(); 
     fll.insertLast(3); 
     fll.display(); 
     fll.deleteFirst(); 
     fll.display(); 
     fll.deleteLast(); 
     fll.display(); 
   } 
 } 
 1 -> 2 ->  
 1 -> 2 -> 3 ->  
 2 -> 3 ->  
 2 -> 

有序链表:

链表中的数据按从小到大排列

public class SortedList { 
   private class Data{ 
     private Object obj; 
     private Data next = null;       
     Data(Object obj){ 
       this.obj = obj; 
     } 
   }   
   private Data first = null;     
   public void insert(Object obj){ 
     Data data = new Data(obj); 
     Data pre = null; 
     Data cur = first; 
     while(cur != null && (Integer.valueOf(data.obj.toString()) 
        .intValue() > Integer.valueOf(cur.obj.toString()) 
         .intValue())){ 
       pre = cur; 
      cur = cur.next; 
     } 
     if(pre == null) 
       first = data; 
     else 
       pre.next = data; 
     data.next = cur; 
   }     
   public Object deleteFirst() throws Exception{ 
     if(first == null) 
       throw new Exception("empty!"); 
     Data temp = first; 
     first = first.next; 
     return temp.obj; 
   }     
   public void display(){ 
     if(first == null) 
       System.out.println("empty"); 
     System.out.print("first -> last : "); 
     Data cur = first; 
     while(cur != null){ 
       System.out.print(cur.obj.toString() + " -> "); 
       cur = cur.next; 
     } 
     System.out.print("\n"); 
   }     
   public static void main(String[] args) throws Exception{ 
     SortedList sl = new SortedList(); 
     sl.insert(80); 
     sl.insert(2); 
     sl.insert(100); 
     sl.display(); 
     System.out.println(sl.deleteFirst()); 
     sl.insert(33); 
     sl.display(); 
     sl.insert(99); 
     sl.display(); 
   } 
 } 
 first -> last : 2 -> 80 -> 100 ->  
 2 
 first -> last : 33 -> 80 -> 100 ->  
 first -> last : 33 -> 80 -> 99 -> 100 -> 

表的插入和删除平均需要比较N/2次,即O(N),但是获取最小数据项只需O(1),因为其始终处于表头,对频繁操作最小数据项的应用,可以考虑使用有序链表实现,如:优先级队列和数组相比,链表的优势在于长度不受限制,并且在进行插入和删除操作时,不需要移动数据项,故尽管某些操作的时间复杂度与数组想同,实际效率上还是比数组要高很多劣势在于随机访问,无法像数组那样直接通过下标找到特定的数据项 。

以上所述是小编给大家介绍的Java数据结构之链表(动力节点之Java学院整理),希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

相关文章

  • mybatis 一对多嵌套查询的实现

    mybatis 一对多嵌套查询的实现

    本文主要介绍了mybatis 一对多嵌套查询的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2024-07-07
  • 浅谈springboot项目中定时任务如何优雅退出

    浅谈springboot项目中定时任务如何优雅退出

    这篇文章主要介绍了浅谈springboot项目中定时任务如何优雅退出?具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-09-09
  • java实现按层遍历二叉树

    java实现按层遍历二叉树

    这篇文章主要为大家详细介绍了java实现按层遍历二叉树,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-01-01
  • JAVA算法起步之快速排序实例

    JAVA算法起步之快速排序实例

    这篇文章主要介绍了JAVA算法起步之快速排序实例,需要的朋友可以参考下
    2014-02-02
  • 通过Java计算文件的MD5值实现方式

    通过Java计算文件的MD5值实现方式

    本文将详细介绍如何使用Java语言来计算文件的MD5值,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2025-04-04
  • Spring Boot CLI安装教程

    Spring Boot CLI安装教程

    Spring Boot是一个命令行工具,用于使用Spring进行快速原型搭建。本文重点给大家介绍Spring Boot CLI安装教程,感兴趣的朋友参考下吧
    2017-08-08
  • Java的Collection集合的常用方法详解

    Java的Collection集合的常用方法详解

    这篇文章主要为大家详细介绍了Java的Collection集合的常用方法,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能够给你带来帮助
    2022-02-02
  • SpringSecurity多认证器配置多模式登录自定义认证器方式

    SpringSecurity多认证器配置多模式登录自定义认证器方式

    这篇文章主要介绍了SpringSecurity多认证器配置多模式登录自定义认证器方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2025-04-04
  • 后端如何接收格式为x-www-form-urlencoded的数据

    后端如何接收格式为x-www-form-urlencoded的数据

    x-www-form-urlencoded格式是一种常见的HTTP请求数据格式,它将请求参数编码为键值对的形式,以便于传输和解析,下面这篇文章主要给大家介绍了关于后端如何接收格式为x-www-form-urlencoded的数据,需要的朋友可以参考下
    2023-05-05
  • Spring boot基于ScheduledFuture实现定时任务

    Spring boot基于ScheduledFuture实现定时任务

    这篇文章主要介绍了Spring boot基于ScheduledFuture实现定时任务,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-06-06

最新评论