验证中...
本周日【珠海源创会】一起聊聊:PingCAP分布式事务、支付宝移动端实践、GSBN技术框架选型,点此报名占座
自定义DoubleLinkList
原始数据 复制代码
package com.cooper.dubbolink;
/**
* @author 10400
* @create 2018-06-14 16:09
*/
public class DoubleLinkList<E> {
private Node first;
private Node last;
private int length = 0;
class Node{
private Node previons;
private Node next;
private E e;
public Node(Node previons,Node next,E e){
this.e = e;
this.previons = previons;
this.next=next;
}
}
/**
* 从链表头添加元素
* @param e
*/
public void addFirst(E e){
if(first == null){
Node node = new Node(null, null, e);
this.first = node;
this.last = node;
length ++;
}else{
Node node = new Node(null, first, e);
this.first.previons = node;
this.first = node;
length++;
}
}
/**
* 尾部添加
* @param e
*/
public void addLast(E e){
if(last == null){
Node node = new Node(null, null, e);
this.last = node;
this.first = node;
length ++;
}else{
Node node = new Node(last,null,e);
this.last.next = node;
this.last = node;
length ++;
}
}
/**
* 节点前插入
* @param baseElement
* @param e
*/
public void insertPrevoids(E baseElement , E e){
Node index = this.first;
while(index != null){
if(baseElement == index.e){break;}
index = index.next;
}
Node newNode = new Node(index.previons,index,e);
index.previons.next = newNode;
index.previons = newNode;
length ++;
}
/**
* 节点之后插入
* @param baseElement
* @param e
*/
public void insertNext(E baseElement , E e){
Node index = this.first;
while(index != null){
if(index.e == baseElement){break;}
index = index.next;
}
Node newNode = new Node(index, index.next, e);
index.next.previons = newNode;
index.next = newNode;
length++;
}
/**
* 删除元素
* @param element
*/
public void removeElement(E element){
Node index = this.first;
while(index != null){
if(index.e == element){break;}
index = index.next;
}
index.previons.next = index.next;
index.next.previons = index.previons;
length --;
}
@Override
public String toString() {
StringBuffer sb = new StringBuffer();
Node index = this.first;
while(index != null){
sb.append(index.e + " -> ");
index = index.next;
}
return sb.toString();
}
public static void main(String[] args) {
DoubleLinkList<String> doubleLinkList = new DoubleLinkList<>();
doubleLinkList.addFirst("234");
doubleLinkList.addFirst("4535");
doubleLinkList.addFirst("6856");
doubleLinkList.insertPrevoids("234","1");
doubleLinkList.insertNext("1","5");
System.out.println("doubleLinkList = " + doubleLinkList);
doubleLinkList.removeElement("1");
System.out.println("doubleLinkList = " + doubleLinkList);
}
}

评论列表( 0 )

你可以在登录后,发表评论

搜索帮助

12_float_left_people 12_float_left_close