package com.se.day03.aGenericity.eDataStructrue;
/**
* 自定义一个双链表的数据结构。
*
*/
public class MyDoubleList<E> {
//新创建容器时,头部和尾部元素都是null,size0;
private Node head; //头部元素
private Node tail; //尾部元素
private int size; // 元素个数
public void add(E e){
//先创建一个节点,当前节点的element是e ,
Node newNode = new Node(e);
if(head == null){
//说明该节点是第一个节点添加进来,所以既是头也是尾
head = newNode;
}else{
tail.nextNode = newNode;
newNode.preNode = tail;
}
tail = newNode;
size++;
}
public void addFirst(E e){
Node newNode = new Node(e);
if(head == null){
tail = newNode;
}else{
newNode.nextNode =head;
head.preNode = newNode;
}
head = newNode;
size++;
}
/**
* 向指定索引index处添加索引
* @param e
* @param index
*/
public void add(E e,int index){
Node newNode = head;
Node indexNode = new Node(e);
if(index < 0 || index > size){
throw new IndexOutOfBoundsException();
}
//添加到末尾
if(index == size){
add(e);
} else if (index == 0){
indexNode.nextNode=head;
head.preNode=indexNode;
head = indexNode;
size++;
} else if(index<size) {
for (int i = 0; i < index; i++) {
newNode =newNode.nextNode;
}
indexNode.nextNode =newNode;
indexNode.preNode = newNode.preNode;
newNode.preNode.nextNode =indexNode;
newNode.preNode =indexNode;
size++;
}
}
public E get(int index){
if(index < 0 || index > size){
throw new IndexOutOfBoundsException();
}
Node newNode = head;
if(index == 0){
return head.element;
}
for (int i = 0; i < index; i++) {
newNode = newNode.nextNode;
}
return newNode.element;
}
public E set(int index, E e){
if(index < 0 || index > size){
throw new IndexOutOfBoundsException();
}
Node newNode = head;
if(index == 0){
E ret= head.element;
head.element = e;
return ret;
}else{
for (int i = 0; i < index; i++) {
newNode = newNode.nextNode;
}
E ret= newNode.element;
newNode.element=e;
return ret;
}
}
public E remove(int index){
Node newNode = head;
if(index < 1 || index > size){
throw new IndexOutOfBoundsException();
}
for(int i = 1; i < index; i++){
newNode =newNode.nextNode;
}
E e = newNode.element;
newNode.preNode.nextNode =newNode.nextNode;
newNode.nextNode =newNode.preNode;
size--;
return e;
}
public String toString(){
StringBuilder str = new StringBuilder();
Node temp = head;
if(temp == null){
return str.toString();
}
for (int i= 0; i <size-1; i++){
str.append(temp.element).append("<===>");
temp = temp.nextNode;
}
str.append(temp.element).append(" size:").append(size);
return str.toString();
}
public int getSize(){
return size;
}
public void initate(){
head = null;
tail = null;
size = 0;
System.out.println("----初始化完毕----");
}
private class Node{
E element ;
Node nextNode;//后一个节点
Node preNode; //前一个节点
public Node(){}
//提供一个参数构造器给当前元素赋值
public Node(E element){
this.element = element;
}
public Node(E element, Node nextNode, Node preNode){
this.element = element;
this.nextNode = nextNode;
this.preNode = preNode;
}
}
public MyDoubleList(){}
public MyDoubleList(Node head, Node tail){
this.head = head;
this.tail = tail;
}
public static void main(String[] args) {
MyDoubleList<Integer> list = new MyDoubleList<>();
for (int i = 0; i < 10; i++) {
list.add(i);
}
System.out.println(list);
list.add(111,10);
System.out.println(list);
System.out.println("下标为10的元素是"+list.get(10));
System.out.println("替换的元素是: "+list.set(1,5));
System.out.println(list);
System.out.println("删除的元素是"+list.remove(8));
System.out.println(list);
list.initate();
}
}
输出结果: