JAVA基础——链表结构之双端链表
双端链表:双端链表与传统链表非常相似.只是新增了一个属性-即对最后一个链结点的引用
如上图所示:由于有着对最后一个链结点的直接引用.所以双端链表比传统链表在某些方面要方便.比如在尾部插入一个链结点.双端链表可以进行直接操作 但传统链表只能通过next节点循环找到最后链结点操作.所以双端链表适合制造队列. 下面的双端链表类.有几个重要方法. insertFirst(插入首链结点) 这个方法与上篇博文的单链表是基本一样的.唯一区别就是,多了个last引用的操作.正常由于last是指向尾链结点的引用,所以插入首链结点是与他无关的. 但当链结点为空(isEmpty())的时候,这会追加的链结点既是首链结点又是尾链结.所以需要将last指向它.
public void insertFirst(double dd) { Link newLink = new Link(dd); if(isEmpty()){ last = newLink; } newLink.next = first; first = newLink; }
insertLast(插入尾链结点) 插入尾部链结点也是与普通的理解基本一致,所以不多赘述.唯一也要注意的是链结点为空(isEmpty())的时候.需要将first指向该链结点.
public void insertLast(double dd) { Link newLink = new Link(dd); if(isEmpty()) { first = newLink; }else { last.next = newLink; } last = newLink; }
看下插入尾部链结点的引用指向:
deleteFirst(删除首部链结点) 这个需要注意的就是,如果仅剩下一个链结点.那么删除后last就应该指向null了.
public void deleteFirst() { first = first.next; if(first.next == null) { last = null; } }
最后这个代码如下:
FirstLastLink package com.dbstructor.oop3;
// 链结点类 class Link { public double dData; public Link next; public Link(double dd) { dData = dd; } public void displayLink() { System.out.print(dData + " "); } } // 双端链表类 class FirstLastList { public Link first; public Link last; public FirstLastList() { first = null; last = null; } public boolean isEmpty(){ return (first == null); } // 表头插入 public void insertFirst(double dd) { Link newLink = new Link(dd); if(isEmpty()){ last = newLink; } newLink.next = first; first = newLink; } // 表尾插入 public void insertLast(double dd) { Link newLink = new Link(dd); if(isEmpty()) { first = newLink; }else { last.next = newLink; } last = newLink; } // 删除表头 public void deleteFirst() { first = first.next; if(first.next == null) { last = null; } } public void displayList() { System.out.print("List (first--->last)"); Link current = first; while(current != null){ current.displayLink(); current = current.next; } System.out.println(); } } public class FirstLastApp { public static void main(String[] args) { FirstLastList theList = new FirstLastList(); // 插入链表头数据 theList.insertFirst(22); theList.insertFirst(44); theList.insertFirst(66); // 插入链表尾数据 theList.insertLast(11); theList.insertLast(33); theList.insertLast(55); theList.displayList(); // 删除表头数据 theList.deleteFirst(); theList.deleteFirst(); theList.displayList(); }
}
代码运行结果为:
List (first--->last)66.0 44.0 22.0 11.0 33.0 55.0 List (first--->last)22.0 11.0 33.0 55.0
链表的效率 这里顺便谈下链表和数组相比效率的优越性.在表头插入和删除的速度都很快,因为只需要改变一下引用所以花费O(1)的时间. 平均起来查找,删除和在指定节点后插入数据都需要搜索一半的链结点.需要O(N)次比较和数组一样.然由于链表删除插入的时候 不需要像数组那种元素的移动.所以效率还是要优于数组. 还有一点就是链表的内存可以随时的扩展内存.而数组的内存是一开始就固定好的.这样就会导致数组的效率和可用性大大下降.
JAVA基础——链表结构之双端链表的更多相关文章
- 队列(存储结构双端链表)--Java实现
/*用链表实现的队列--使用的是双端链表 *注意:空指针错误肯定是引用没有指向对象 * */ public class MyLinkedQueue { private MyFirstAndLastLi ...
- Java数据结构——用双端链表实现队列
//================================================= // File Name : LinkQueue_demo //---------------- ...
- Java单链表、双端链表、有序链表实现
单链表: insertFirst:在表头插入一个新的链接点,时间复杂度为O(1) deleteFirst:删除表头的链接点,时间复杂度为O(1) 有了这两个方法,就可以用单链表来实现一个栈了,见htt ...
- 《Java数据结构与算法》笔记-CH5-链表-3双端链表
/** * 双端链表的实现 */ class LinkA { public long dData; public LinkA next; public LinkA(long d) { dData = ...
- java实现双端链表
PS:双端链表(持有对最后一个节点的引用,允许表尾操作与表头操作等效的功能) public class DoubleLinkedList { //节点类 static class Node { pub ...
- 双端链表--Java实现
/*双端链表--比普通链表多了一个指向最后一个节点的引用 * 特点: 链表可以进行尾巴插入--输出顺序和输入顺序一致 * 但是不可以进行尾巴删除因为没有倒数第二节点的引用 * */ public cl ...
- java数据结构——单链表、双端链表、双向链表(Linked List)
1.继续学习单链表,终于摆脱数组的魔爪了,单链表分为数据域(前突)和引用域(指针域)(后继),还有一个头结点(就好比一辆火车,我们只关心火车头,不关心其它车厢,只需知晓车头顺藤摸瓜即可),头结点没有前 ...
- 循环双端链表(python)
# -*- coding: utf-8 -*- class Node(object): __slots__ = ('value', 'prev', 'next') # save memory def ...
- 自己动手实现java数据结构(四)双端队列
1.双端队列介绍 在介绍双端队列之前,我们需要先介绍队列的概念.和栈相对应,在许多算法设计中,需要一种"先进先出(First Input First Output)"的数据结构,因 ...
随机推荐
- 基于百度AI人脸识别技术的Demo
编写demo之前首先浏览官方API:http://ai.baidu.com/docs#/Face-API/top 下面是源码: package com.examsafety.test; import ...
- 修改ip 在linux上永久修改IP地址 子网掩码
小结: 1. 子网掩码.子网IP计算 2. linux centos 修改ip地址细节介绍_LINUX_操作系统_脚本之家 http://www.jb51.net/LINUXjishu/66509.h ...
- 修改版Putty,可保持密码
修改版Putty,可保持密码: http://files.cnblogs.com/findumars/putty_v6.0.rar 转自: http://unmi.cc/putty-auto-logi ...
- POJ 2080:Calendar
Calendar Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 12546 Accepted: 4547 Descrip ...
- 【HDU4706】Children's Day
http://acm.hdu.edu.cn/showproblem.php?pid=4706 水题,也不知道有没有spj // <4706.cpp> - 11/03/16 14:11:21 ...
- ViewPager嵌套ViewPager后子ViewPager滑动不正常问题
ViewPager嵌套ViewPager后,滑动事件没法在子ViewPager里面响应. 解决办法是自定义子ViewPager. 以下代码是转载的,经本人测试,可以用!!! 转载地址:http://b ...
- 4.7.4 Constructing LALR Parsing Tables
4.7.4 Constructing LALR Parsing Tables We now introduce our last parser construction method, the LAL ...
- Eclipse 使用Anaconda python 解释器
问题: ubuntu16.04 Anaconda 安装成功 Eclispe 写Python代码 无法使用 (pandas库等) 原因: Eclispe 此时的python解释器==>用的并不是A ...
- Spark 多项式逻辑回归__多分类
package Spark_MLlib import org.apache.spark.ml.Pipeline import org.apache.spark.ml.classification.{B ...
- Wannafly挑战赛29A御坂美琴
传送门 这套题很有意思2333 蠢了--首先先判总共加起来等不等于\(n\),不是的话就不行 然后dfs记录\(n\)不断分下去能分成哪些数,用map记录一下,判断是否所有数都能被分出来就是了 //m ...