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实现双向链表(两个版本)的相关知识,希望对大家有所帮助。
相关文章
SpringBoot整合Docker实现一次构建到处运行的操作方法
本文讲解的是 SpringBoot 引入容器化技术 Docker 实现一次构建到处运行,包括镜像构建、Docker仓库搭建使用、Docker仓库可视化UI等内容,需要的朋友可以参考下2022-10-10
SpringBoot使用mybatis-plus分页查询无效的问题解决
MyBatis-Plus提供了很多便捷的功能,包括分页查询,本文主要介绍了SpringBoot使用mybatis-plus分页查询无效的问题解决,具有一定的参考价值,感兴趣的可以了解一下2023-12-12
解决gateway报netty堆外内存溢出io.netty.util.internal.OutOfDirectMemor
这篇文章主要介绍了解决gateway报netty堆外内存溢出io.netty.util.internal.OutOfDirectMemoryError,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教2023-12-12
mybatis+springboot发布postgresql数据的实现
本文主要介绍了mybatis+springboot发布postgresql数据的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2023-11-11
JAVA匿名内部类(Anonymous Classes)的具体使用
本文主要介绍了JAVA匿名内部类,匿名内部类在我们JAVA程序员的日常工作中经常要用到,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下2021-08-08


最新评论