简单的双向循环链表

```public class LinkedList<E> {

int size = 0;
Node<E> head = new Node<E>(null, null, null);

public LinkedList() {
head.next = head.previous = head;
}

public void add(E date) {
//核心 循环双向链表
Node<E> newNode = new Node<E>(head.previous, date, head);   //新节点的prev指向头结点的prev 新节点的next指向头结点
newNode.previous.next = newNode;    //调整，新节点的前一个的后一个
newNode.next.previous = newNode;    //调整，新节点的后一个的前一个
size++;
}

public E get(int index) {
if (index < 0 || index >= size) {
throw new IndexOutOfBoundsException("Index:" + index + ",size:" + size);
}
Node<E> node = head;
if (index < (size >> 1)) {
for (int i = 0; i <= index; i++) {  //head是哑元，i<=index当index=0时，返回head.next
node = node.next;           //对头结点进行迭代
}
}else{
for(int i=size;i>index;i--){
node=node.previous;
}
}
return node.getData();
}

public int size() {
return size;
}

public static void main(String[] args) {
LinkedList<Integer> list = new LinkedList<Integer>();
list.add(1);
list.add(2);
list.add(3);
list.add(4);
list.add(5);
list.add(6);
list.add(7);
list.add(8);
System.out.println("list.get(3)="+list.get(3));
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
}

class Node<E> {

E data;
Node<E> next;
Node<E> previous;

public Node(Node<E> previous, E data, Node<E> next) {
this.data = data;
this.next = next;
this.previous = previous;
}
public E getData(){
return data;
}

public void setData(E data){
this.data=data;
}
}

}```

C:\test>java   LinkedList
list.get(3)=4
1
2
3
4
5
6
7
8

• 0

开心

• 0

板砖

• 0

感动

• 0

有用

• 0

疑问

• 0

难过

• 0

无聊

• 0

震惊