双向链表--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 ...
随机推荐
- eclipse版本选择
Eclipse最初是由IBM公司开发的替代商业软件Visual Age for Java的下一代IDE开发环境,2001年11月贡献给开源社区,现在它由非营利软件供应商联盟Eclipse基金会. Ec ...
- Spring Boot Maven Plugin(一):repackage目标
简介 Spring Boot Maven Plugin插件提供spring boot在maven中的支持.允许你打包可运行的jar包或war包. 插件提供了几个maven目标和Spring Boot ...
- Shiro第四篇【Shiro与Spring整合、快速入门、Shiro过滤器、登陆认证】
Spring与Shiro整合 导入jar包 shiro-web的jar. shiro-spring的jar shiro-code的jar 快速入门 shiro也通过filter进行拦截.filter拦 ...
- xml解析案例
步骤:Channel是java bean类 public static List<Channel> parsexml(InputStream inputStream) {//注意服务器种是 ...
- MySQL索引优化实例说明
下面分别创建三张表,并分别插入1W条简单的数据用来测试,详情如下: [1] test_a 有主键但无索引 CREATE TABLE `test_a` ( `id` int(10) unsign ...
- Java基础——集合源码解析 List List 接口
今天我们来学习集合的第一大体系 List. List 是一个接口,定义了一组元素是有序的.可重复的集合. List 继承自 Collection,较之 Collection,List 还添加了以下操作 ...
- Android忽略文件以及.gitignore规则不生效的可行解决方案
github官方的忽略规则:https://github.com/github/gitignore/blob/master/Android.gitignore 我司项目中的忽略规则: *.iml .g ...
- JAVA 一步一步向上爬
Java分为基本数据类型和引用数据类型(类.接口.数组) Integer.MAX_VALUE 浮点型默认为double java采用Unicode char为两个字节 Unicode为每一个字符定制了 ...
- 51nod 1414 冰雕 思路:暴力模拟题
题意是现在有n个雕像把一个圆等分了,每一个雕像有一个吸引力. 叫你不移动雕像只去掉雕像让剩下的雕像还能等分这个圆,求剩下的雕像的吸引力之和的最大值. 显然去掉后剩下雕像的间隔应该是n的因子,因为这样才 ...
- Linux Bash Shell字符串截取
#!/bin/bash#定义变量赋值时等号两边不能有空格,否则会报命令不存在 # 运行shell脚本两种方式 # 1.作为解释参数 /bin/sh test.sh ; 2.作为可执行文件 chmo ...