双向链表--Java实现
/*双向链表特点:
*1.每个节点含有两个引用,previos和next,支持向前或向后的遍历(除头节点)
*2.缺点插入或删除的时候涉及到引用修改的比较多
*注意:下面的双向链表其实也实现了双端链表
*注意:在Java中多个引用可以指向同一个对象,也可以随时改变引用的指向
* 关于修改引用细心一点就可以 引用A = 引用B 表示A引用指向B引用指向的对象
*应用:利用双向链表可以实现双端队列
* */ public class MyDoubleLink {
private Link first;
private Link last; public boolean isEmpty(){
return first == null;
} public void insertFirst(int key){
Link newLink = new Link(key);
if(first == null){
last = newLink;
}
else{
first.previous = newLink;
}
newLink.next = first;//链未断可以指向同一个
first = newLink;
} public void insertLast(int key){
Link newLink = new Link(key);
if(first == null){
first = newLink;
}
else{
last.next = newLink;
newLink.previous = last;
}
last = newLink;
} //插入指定值的后边---其实是一种尾巴插入--此时链表非空才可以操作
public boolean insertAfter(int key,int value){
Link newLink = new Link(value);
Link current = first;
while(current.id != key){
current = current.next;
if(current == null){
return false;
}
}
if(current == last){//find it at last item
newLink.next = null;
last = newLink;
}
else{
newLink.next = current.next;
current.next.previous = newLink;
}
newLink.previous = current;
current.next = newLink;
return true;
} public Link deleteFirst(){
Link temp = first;
if(first.next == null){
last = null;
}
else{
first.next.previous = null;
}
first = first.next;
return temp;
} public Link deleteLast(){
Link temp = last;
if(first.next == null){
first = null;
}
else{
last.previous.next = null;
}
last = last.previous;
return temp;
} //按照值进行删除--可能存在找不到的时候
public Link delete(int key){
Link current = first;
while(current.id != key ){
current = current.next;
if(current == null){
return null;
}
}
if(current == first){//find it at first item并非只有一个节点
first = current.next;
}
else{ //find it not first item
current.previous.next = current.next;
} if(current == last){ //find it at last item
last = current.previous;
}
else{ //find it not last
current.next.previous = current.previous;
}
return current;
} public void diaplayFirstToLast(){
System.out.println("first to last");
Link current = first;
while(current != null){
System.out.print(current.id + " ");
current = current.next;
}
System.out.println();
} public void displayLastToFirst(){
System.out.println("last to first");
Link current = last;
while(current != null){
System.out.print(current.id + " ");
current = current.previous;
}
System.out.println();
}
}
双向链表--Java实现的更多相关文章
- 线性链表的双向链表——java实现
.线性表链式存储结构:将采用一组地址的任意的存储单元存放线性表中的数据元素. 链表又可分为: 单链表:每个节点只保留一个引用,该引用指向当前节点的下一个节点,没有引用指向头结点,尾节点的next引用为 ...
- 双向链表-java完全解析
原文:https://blog.csdn.net/nzfxx/article/details/51728516 "双向链表"-数据结构算法-之通俗易懂,完全解析 1.概念的引入 相 ...
- 剑指Offer:面试题27——二叉搜索树与双向链表(java实现)
问题描述: 输入一棵二叉搜索树,将该二叉搜索树转换成一个排序的双向链表.要求不能创建任何新的结点,只能调整树中结点指针的指向. 思路: 将树分为三部分:左子树,根结点,右子树. 1.我们要把根结点与左 ...
- 双向链表JAVA代码
//双向链表类 publicclassDoubleLinkList{ //结点类 publicclassNode{ publicObject data; ...
- LRU hashMap(拉链) + 双向链表 java实现
//基于 hash (拉链法) + 双向链表,LRUcache //若改为开放寻址,线性探测法能更好使用cpuCache public class LRU { private class Node { ...
- 《剑指offer》面试题27 二叉搜索树与双向链表 Java版
(将BST改成排序的双向链表.) 我的方法一:根据BST的性质,如果我们中序遍历BST,将会得到一个从小到大排序的序列.如果我们将包含这些数字的节点连接起来,就形成了一个链表,形成双向链表也很简单.关 ...
- 双向链表——Java实现
双向链表 链表是是一种重要的数据结构,有单链表和双向链表之分:本文我将重点阐述不带头结点的双向链表: 不带头结点的带链表 我将对双链表的增加和删除元素操作进行如下解析 1.增加元素(采用尾插法) (1 ...
- Spark案例分析
一.需求:计算网页访问量前三名 import org.apache.spark.rdd.RDD import org.apache.spark.{SparkConf, SparkContext} /* ...
- JAVA单向/双向链表的实现
一.JAVA单向链表的操作(增加节点.查找节点.删除节点) class Link { // 链表类 class Node { // 保存每一个节点,此处为了方便直接定义成内部类 private Str ...
随机推荐
- jQuery 简介,与js的对比
jquery可以说是js的封装,大多数情况下jquery比js简单,它们两个可以相互写对方的里面,使用jquery需要导入jquery文件. <script src="jquery-1 ...
- 多线程面试题系列(7):经典线程同步 互斥量Mutex
前面介绍了关键段CS.事件Event在经典线程同步问题中的使用.本篇介绍用互斥量Mutex来解决这个问题. 互斥量也是一个内核对象,它用来确保一个线程独占一个资源的访问.互斥量与关键段的行为非常相似, ...
- Geronimo tomcat: 在 Apache Geronimo 插件体系中将 Apache Tomcat 这个优秀的 Web 容器整合至其中
Apache Geronimo 灵活的插件体系将 Tomcat, OpenJPA, OpenEJB, ActiveMQ 等第三方组件集成至其中.本文从多角度介绍了在 Apache Geronimo 中 ...
- 代码的完整性:打印1到最大的n位数
输入数字n,按顺序打印出从1到最大的n位十进制数. 比如,输入3,则打印出1,2,3,.....,一直到最大的3位数即999. 全排列打印 public class Main { public sta ...
- Spring配置文件的命名空间URI
Spring配置文件介绍 <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi=" ...
- SQL 常用语法一
整理笔记,并将常用的SQL语法记录下来. 这些方法有 CASE WHEN, IFNULL,GROUP BY,LIMIT,SUBSTR 1,字段转换 CASE WHEN 意义: If(a==b) a=c ...
- day12<常见对象+>
常见对象(Scanner的概述和方法介绍) 常见对象(Scanner获取数据出现的小问题及解决方案) 常见对象(String类的概述) 常见对象(String类的构造方法) 常见对象(String类的 ...
- webpack2系列step1
第一篇:HTML 本文将一步一步的介绍webpack2的配置,从最基础的一直到与node结合. 操作都一样: midir step1 && cd step1 npm init -y n ...
- 个人从源码理解JIT模式下angular编译AppModule的过程
承接上文.笔者之前将一个angular项目的启动过程分为了两步: 创建平台得到 PlatformRef ,以及执行平台引用提供的方法编译根模块 AppModule .本文就将着眼于创建好的平台,从an ...
- 教育,创新,提升:Indiegogo和Kickstarter上受中国用户支持的10个众筹项目
中国的经济正在迅速发展,已成为世界第二大经济体.中国家庭随着经济水平的提高,越来越多父母愿意将自己的子女送到海外留学. 家长们希望自己的子女可以有机会接受国外大学优质的教育, 以便他们将来可以学成归来 ...