• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号
您的位置:首页 > 程序设计 >Java > Java数据结构之链表(动力节点之Java学院整理)

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

作者: 字体:[增加 减小] 来源:互联网 时间:2017-05-28

通过本文主要向大家介绍了数据结构java单链表,java数据结构链表,java中的链表,java实现单链表,java实现双向链表等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

单链表:

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)); 
   } 
 } 
</div>
 1 -> 2 -> 3 -> 4 ->  
 2 -> 3 -> 4 ->  
 2 -> 4 ->  
 null 
 4 
</div>

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

与单向链表的不同之处在保存有对最后一个链接点的引用(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(); 
   } 
 } 
</div>
 1 -> 2 ->  
 1 -> 2 -> 3 ->  
 2 -> 3 ->  
 2 -> 
</div>

有序链表:

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

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(); 
   } 
 } 
</div>
 first -> last : 2 -> 80 -> 100 ->  
 2 
 first -> last : 33 -> 80 -> 100 ->  
 first -> last : 33 -> 80 -> 99 -> 100 -> 
</div>

表的插入和删除平均需要比较N/2次,即O(N),但是获取最小数据项只需O(1),因为其始终处于表头,对频繁操作最小数据项的应用,可以考虑使用有序链表实现,如:优先级队列和数组相比,链表

分享到:QQ空间新浪微博腾讯微博微信百度贴吧QQ好友复制网址打印

您可能想查找下面的文章:

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

相关文章

  • 2017-05-28详解Java从后台重定向(redirect)到另一个项目的方法
  • 2017-05-28SWT(JFace)体验之Sash(活动控件)
  • 2017-05-28java中 String和StringBuffer的区别实例详解
  • 2017-05-28SpringBoot+MyBatis简单数据访问应用的实例代码
  • 2017-05-28Spring boot学习教程之快速入门篇
  • 2017-05-28java中常见的中文乱码总结
  • 2017-05-28详谈java中boolean和Boolean的区别
  • 2017-05-28Spring Data Jpa实现分页和排序代码实例
  • 2017-05-28java后台利用Apache poi 生成excel文档提供前台下载示例
  • 2017-05-28java 制作验证码并进行验证实例详解

文章分类

  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号

最近更新的内容

    • SpringBoot下的值注入(推荐)
    • 数据库基本操作语法归纳总结
    • Spring-data-redis操作redis知识总结
    • 浅析java中next与nextLine用法对比
    • java实现简单的webservice方式
    • 数据库基本操作语法归纳总结
    • java实现把对象数组通过excel方式导出的功能
    • Java中HashSet和HashMap的区别_动力节点Java学院整理
    • 详解Spring整合Ehcache管理缓存
    • spring boot 的常用注解使用小结

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有