java下一个lst版本_java_Java实现双向链表(两个版本),临近春节,项目都结束了,都 - phpStudy...

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

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

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

public class LinkedList {

private Node head;

public LinkedList() {

}

public E getFirst(){

if(head==null){

return null;

}

return head.value;

}

public LinkedList addFirst(E e){

head.pre=new Node(e, null, head);

head=head.pre;

return this;

}

public LinkedList 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 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 {

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 addFirst(E e){

head.pre=new Node(e, null, head);

head=head.pre;

return this;

}

public LinkedList 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 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实现双向链表(两个版本)的相关知识,希望对大家有所帮助。相关阅读:

php使用GD实现颜色渐变实例

C语言中数组的一些基本知识小结

简单介绍PHP的责任链编程模式

汉字转拼音软件制件示例(汉字转字母)

Android实现授权访问网页的方法

SQLite3 命令行操作指南

探寻JavaScript中this指针指向

C++之类和对象课后习题简单实例

Win10怎么添加游客账户?Win10添加游客账户的方法

php自定义类fsocket模拟post或get请求的方法

JSON遍历方式实例总结

C++编程中将引用类型作为函数参数的方法指南

第二篇Bootstrap起步

MySQL ALTER命令使用详解

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值