Java实现双向链表(两个版本)

 更新时间:2016年02月06日 16:44:28   投稿:mrr  
这篇文章主要介绍了Java实现双向链表(两个版本)的相关资料,需要的朋友可以参考下

临近春节,项目都结束了,都等着回家过年了。下面是小编给大家研究数据结构的相关知识,链表算是经常用到的一种数据结构了,现将自己的实现展示如下,欢迎大神赐教。

第一个版本,没有最后一个节点,每次从根节点开始遍历

public class LinkedList<E> {
private Node head;
public LinkedList() {
}
public E getFirst(){
if(head==null){
return null;
}
return head.value;
}
public LinkedList<E> addFirst(E e){
head.pre=new Node(e, null, head);
head=head.pre;
return this;
}
public LinkedList<E> addNode(E e){
Node lst=head;
if(lst==null){
this.head=new Node(e, null, null);
return this;
}else{
while(true){
if(lst.next==null){
break;
}else{
lst=lst.next;
}
}
lst.next=new Node(e, lst, null);
return this;
}
}
public LinkedList<E> remove(E e){
Node lst=head;
if(lst==null){
throw new NullPointerException("the LinkedList is empty.");
}else{
while(true){
if(e.equals(lst.value)){
//移除这个元素
if(lst.pre!=null){
lst.pre.next=lst.next;
}
if(lst.next!=null){
lst.next.pre=lst.pre;
}
lst=null;
break;
}
lst=lst.next;
}
return this;
}
}
@Override
public String toString() {
StringBuffer buff=new StringBuffer("[");
Node lst=this.head;
while(lst!=null){
buff.append(lst.value+",");
lst=lst.next;
}
return buff.substring(0, buff.length()-1)+"]";
}
/**节点信息*/
private class Node{
public Node pre;
public E value;
public Node next;

public Node(E value,Node pre,Node next) {
this.value=value;
this.pre=pre;
this.next=next;
}
} 
}

第二个版本,有了最后一个节点

public class LinkedList<E> {
private Node head;
private Node last;
public LinkedList() {
}
public E getFirst(){
if(head==null){
return null;
}
return head.value;
}
public E getLast(){
if(last==null){
return null;
}
return last.value;
}
public LinkedList<E> addFirst(E e){
head.pre=new Node(e, null, head);
head=head.pre;
return this;
}
public LinkedList<E> addNode(E e){
Node lst=last;
if(lst==null){//如果最后一个节点是空的则这个链表就是空的
this.last=new Node(e, null, null);
this.head=this.last;
return this;
}else{
while(true){
if(lst.next==null){//
break;
}else{
lst=lst.next;
}
}
lst.next=new Node(e, lst, null);
last=lst.next;
return this;
}
}
public LinkedList<E> remove(E e){
Node lst=head;
if(lst==null){
throw new NullPointerException("the LinkedList is empty.");
}else{
while(true){
if(e.equals(lst.value)){
//移除这个元素
if(lst.pre!=null){
lst.pre.next=lst.next;
}
if(lst.next!=null){
lst.next.pre=lst.pre;
}
lst=null;
break;
}
lst=lst.next;
}
return this;
}
}
@Override
public String toString() {
StringBuffer buff=new StringBuffer("[");
Node lst=this.head;
while(lst!=null){
buff.append(lst.value+",");
lst=lst.next;
}
return buff.substring(0, buff.length()-1)+"]";
}
/**节点信息*/
private class Node{
public Node pre;
public E value;
public Node next;

public Node(E value,Node pre,Node next) {
this.value=value;
this.pre=pre;
this.next=next;
}
}
}

注:以上两个版本都没有考虑在多线程下使用的情况。

以上所述是小编给大家介绍的Java实现双向链表(两个版本)的相关知识,希望对大家有所帮助。

相关文章

  • Java阻塞队列中的BlockingQueue接口详解

    Java阻塞队列中的BlockingQueue接口详解

    这篇文章主要介绍了Java阻塞队列中的BlockingQueue接口详解,对于Queue而言,BlockingQueue是主要的线程安全的版本,具有阻塞功能,可以允许添加、删除元素被阻塞,直到成功为止,BlockingQueue相对于Queue而言增加了两个方法put、take元素,需要的朋友可以参考下
    2023-09-09
  • SpringMVC Interceptor拦截器使用教程

    SpringMVC Interceptor拦截器使用教程

    SpringMVC中拦截器(Interceptor)用于对URL请求进行前置/后置过滤,Interceptor与Filter用途相似,但实现方式不同。Interceptor底层就是基于Spring AOP 面向切面编程实现
    2023-01-01
  • autoMapping和autoMappingBehavior的区别及说明

    autoMapping和autoMappingBehavior的区别及说明

    这篇文章主要介绍了autoMapping和autoMappingBehavior的区别及说明,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-01-01
  • SpringBoot浅析Redis访问操作使用

    SpringBoot浅析Redis访问操作使用

    Redis是一个速度非常快的非关系数据库(Non-Relational Database),它可以存储键(Key)与多种不同类型的值(Value)之间的映射(Mapping),可以将存储在内存的键值对数据持久化到硬盘,可以使用复制特性来扩展读性能,还可以使用客户端分片来扩展写性能
    2022-11-11
  • java实现航班信息查询管理系统

    java实现航班信息查询管理系统

    这篇文章主要为大家详细介绍了java实现航班信息查询管理系统,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-12-12
  • Java实现办公文档在线预览功能

    Java实现办公文档在线预览功能

    java实现办公文件在线预览功能是一个大家在工作中也许会遇到的需求,这篇文章就教大家如何实现这一功能,感兴趣的小伙伴可以了解一下
    2021-12-12
  • Java集合使用 Iterator 删除元素

    Java集合使用 Iterator 删除元素

    这篇文章主要介绍了Java集合使用 Iterator 删除元素,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-11-11
  • 浅谈将JNI库打包入jar文件

    浅谈将JNI库打包入jar文件

    这篇文章主要介绍了浅谈将JNI库打包入jar文件,具有一定借鉴价值,需要的朋友可以参考下。
    2017-12-12
  • java右下角弹窗示例分享

    java右下角弹窗示例分享

    这篇文章主要介绍了java右下角弹窗示例,需要的朋友可以参考下
    2014-04-04
  • Maven中央仓库发布的实现方法

    Maven中央仓库发布的实现方法

    最近做了个项目,希望能够上传到maven中央仓库,给更多的人使用,于是就产生了这次项目发布经历。感兴趣的可以一起来参考一下
    2021-06-06

最新评论