//=================================================
// File Name : DoublyLinked_demo
//------------------------------------------------------------------------------
// Author : Common //类名:DoublyLinkedList
//属性:
//方法:
class DoublyLinkedList{ //双向链表
private Link_long first;
private Link_long last; public DoublyLinkedList(){ //构造函数
this.first = null;
this.last = null;
} public boolean isEmpty(){
return first==null;
} public void insertFirst(long dd){ //从链表的头开始插入
Link_long newLink = new Link_long(dd);
if(isEmpty()){
last = newLink; //不用改变first
}else{
first.previous = newLink; //插入新的元素
}
newLink.next = first; //插入新的元素
first = newLink; //修改first的位置
} public void insertLast(long dd){ //从链表的尾开始插入
Link_long newLink = new Link_long(dd);
if(isEmpty()){
first = newLink; //不用改变last
}else{
last.next = newLink; //在last后面添加新元素,并修改last的位置
newLink.previous = last;
}
last = newLink; //注意:只有一个元素的时候,插入要把last也赋为newLink
} public Link_long deleteFirst(){ //从链表的头删除一个元素
Link_long temp = first; //暂存first
if(first.next == null){ //如果只有一个元素,把last也赋为null
last = null;
}else{
first.next.previous = null;
}
first = first.next; //把next设为first
return temp; //返回原来的first
} public Link_long deleteLast(){ //从链表的头删除一个元素
Link_long temp = last; //暂存last
if(first.next == null){ //如果只有一个元素,把first也赋为null
first = null;
}else{
last.previous.next = null;
}
last = last.previous; //把previous设为last
return temp; //返回原来的last
} public boolean insertAfter(long key,long dd){ //在特定元素后面插入
Link_long current = first;
while(current.dData != key){
current = current.next;
if(current == null){
return false;
}
}
Link_long newLink = new Link_long(dd); if(current == last){
newLink.next = null;
last = newLink;
}else{
newLink.next = current.next;
current.next.previous = newLink;
}
newLink.previous = current;
current.next = newLink;
return true;
} public Link_long deleteKey(long key){ //删除指定元素
Link_long current = first;
while(current.dData != key){
current = current.next;
if(current == null){
return null;
}
}
if(current == first){ //如果第一个元素匹配,则删除,并把first向后移动
first = current.next;
}else{
current.previous.next = current.next; //改变current上一个元素的next的指向
}
if(current == last){
last = current.previous; //如果最后一个元素匹配,则删除,并把previous向前移动
}else{
current.next.previous = current.previous; //改变current后一个元素的previous的指向
}
return current;
} public void displayForward(){
System.out.println("List(first-->last):");
Link_long current = first; //用于不断改变位置实现遍历
while(current != null){
current.displayLink();
current = current.next;
}
} public void displayBackward(){
System.out.println("List(last-->first):");
Link_long current = last; //用于不断改变位置实现遍历
while(current != null){
current.displayLink();
current = current.previous;
}
} } //主类
//Function : DoublyLinked_demo
public class DoublyLinked_demo { public static void main(String[] args) {
// TODO 自动生成的方法存根
DoublyLinkedList theList = new DoublyLinkedList(); theList.insertFirst(40);
theList.insertFirst(30);
theList.insertFirst(20);
theList.insertFirst(10);
theList.displayForward();
theList.displayBackward(); theList.insertAfter(20, 25);
theList.displayForward();
theList.displayBackward(); theList.deleteFirst();
theList.displayForward(); theList.deleteKey(20);
theList.displayForward();
} }

Java数据结构——双向链表的更多相关文章

  1. Java数据结构--双向链表的实现

    #java学习经验总结------双向链表的实现 双向链表的建立与单链表类似,只是需要使用pre指针指向前一个结点,并且在删除添加时不仅仅考虑next package datastructure; p ...

  2. Java数据结构之线性表

    从这里开始将要进行Java数据结构的相关讲解,Are you ready?Let's go~~ java中的数据结构模型可以分为一下几部分: 1.线性结构 2.树形结构 3.图形或者网状结构 接下来的 ...

  3. Java数据结构和算法 - 链表

    Q: 为什么要引入链表的概念?它是解决什么问题的? A: 数组作为数据存储结构有一定的缺陷,在无序数组中,搜索是低效的:而在有序数组中,插入效率又很低:不管在哪一个数组中删除效率都很低:况且一个数组创 ...

  4. (6)Java数据结构-- 转:JAVA常用数据结构及原理分析

    JAVA常用数据结构及原理分析  http://www.2cto.com/kf/201506/412305.html 前不久面试官让我说一下怎么理解java数据结构框架,之前也看过部分源码,balab ...

  5. Java简单双向链表实现 @version 1.0

    package com.list; /** * 数据结构和算法Java表示 双向链表 * * @version 1.0 * @author 小明 * */ public class MyDoublel ...

  6. Java数据结构和算法(一)线性结构

    Java数据结构和算法(一)线性结构 数据结构与算法目录(https://www.cnblogs.com/binarylei/p/10115867.html) 线性表 是一种逻辑结构,相同数据类型的 ...

  7. 一文掌握关于Java数据结构所有知识点(欢迎一起完善)

    在我们学习Java的时候,很多人会面临我不知道继续学什么或者面试会问什么的尴尬情况(我本人之前就很迷茫).所以,我决定通过这个开源平台来帮助一些有需要的人,通过下面的内容,你会掌握系统的Java学习以 ...

  8. Java数据结构和算法(四)--链表

    日常开发中,数组和集合使用的很多,而数组的无序插入和删除效率都是偏低的,这点在学习ArrayList源码的时候就知道了,因为需要把要 插入索引后面的所以元素全部后移一位. 而本文会详细讲解链表,可以解 ...

  9. Java数据结构之队列的实现以及队列的应用之----简单生产者消费者应用

    Java数据结构之---Queue队列 队列(简称作队,Queue)也是一种特殊的线性表,队列的数据元素以及数据元素间的逻辑关系和线性表完全相同,其差别是线性表允许在任意位置插入和删除,而队列只允许在 ...

随机推荐

  1. JSON与js对象序列化

    JavaScript对象表示法(JavaScript Object Notation,简称JSON)是一种轻量级的数据交换格式,它基于js字面量表示法,是js的一个子集.虽然是一个js的子集但是他与语 ...

  2. 【POJ 3243】Clever Y 拓展BSGS

    调了一周,我真制杖,,, 各种初始化没有设为1,,,我当时到底在想什么??? 拓展BSGS,这是zky学长讲课的课件截屏: 是不是简单易懂.PS:聪哥说“拓展BSGS是偏题,省选不会考,信我没错”,那 ...

  3. Shiro 学习笔记(二)——shiro身份验证

    身份验证: 在应用中证明他就是他本人.一般上用身份证.用户/密码 来证明. 在shiro中,用户需要提供principals (身份)和credentials(证明)给shiro,从而应用能验证用户身 ...

  4. 使用android studio时提示Unable to access Android SDK add-on list

    这个界面不用管,点击Cancel.到一个界面下边有个”Android SDK Location:“这个选路径的时候选你的android sdk的路径就好了.以后再打开就不会再报这个错了.

  5. Python开发最常犯错误总结10种

    不管是在学习还是工作过程中,人都会犯错.虽然Python的语法简单.灵活,但也一样存在一些不小的坑,一不小心,初学者和资深Python程序员都有可能会栽跟头.本文是Toptal网站的程序员梳理的10大 ...

  6. C# 对多个集合和数组的操作(合并、去重复、判断)

        例如:List  listA=new List{1,2,4,5,7}List  listB=new List{1,3,6,8} listA.AddRange(listB);List Resul ...

  7. Maven项目自动生成mybaties配置文件

    1.把mysql-connector-java-5.1.31.jar包放到C盘的mysqljar文件夹下 2.在generatorConfig.xml文件中配置实体.dao.service等包 < ...

  8. Spark 与 MapReduce的区别

    学习参考自 http://spark-internals.books.yourtion.com/markdown/4-shuffleDetails.html 1.  Shuffle read 边 fe ...

  9. hadoop 2.x 完全分布式搭建

    HDFS HA 集群搭建: DN(DataNode):3个:NN(NameNode):2:ZK(ZooKeeper):3(大于1的奇数个):ZKFC:和NN在同一台机器:JN:3:RM(Resourc ...

  10. 【poj3084】 Panic Room

    http://poj.org/problem?id=3084 (题目链接) 题意 一个房子里面有m个房间,一些房间之间有门相连,而门的开关只在一个房间有,也就是说只有一个房间可以控制该扇门的开关.现在 ...