双向链表的实现
class Node{
public Node next;
public Node prey;
public int data;
public Node(int data){
this.data = data;
}
}
class DoubleList{
public Node head;
public Node last;
public DoubleList(){
this.head = null;
this.last = null;
}
public void addFirst(int data){
Node node = new Node(data);
if(head == null){
this.head = node;
this.last = node;
}else{
head.prey = node;
node.next = head;
head = node;
head.prey = null;
}
}
public void addLast(int data){
Node node = new Node(data);
if(head == null){
this.head = node;
this.last = node;
}else{
last.next = node;
node.prey = last;
last = node;
}
}
private void checkIndex(int index){
if(index < 0 || index > size()) {
throw new IndexOutOfBoundsException("index越界异常");
}
}
private Node foundIndex(int index){
int count = 0;
Node cur = this.head;
while(count < index){
count++;
cur = cur.next;
}
return cur;
}
public boolean addIndex(int index,int data){
checkIndex(index);
Node node = new Node(data);
Node cur = foundIndex(index);
if(cur == head){
addFirst(data);
return true;
}else if(cur == last){
addLast(data);
}
node.next = cur;
node.prey = cur.prey;
cur.prey.next = node;
cur.prey = node;
return true;
}
public int size(){
Node cur = this.head;
int count = 0;
if(head == null){
return 0;
}
while(cur != null){
cur = cur.next;
count++;
}
return count;
}
public boolean contains(int key){
Node cur = this.head;
while(cur != null){
if(cur.data == key){
return true;
}
cur = cur.next;
}
return false;
}
public void remove(int key){
Node cur = this.head;
while(cur != null){
if(cur.data == key && cur == head){
head = head.next;
head.prey = null;
return ;
}
else if(cur.data == key && cur == last){
last = last.prey;
last.next = null;
return ;
}
else if(cur.data == key){
cur.prey.next = cur.next;
cur.next.prey = cur.prey;
return ;
}
cur = cur.next;
}
}
public void removeAllKey(int key){
Node cur = this.head;
while(cur != null){
if(cur.data == key && cur == head){
head = head.next;
head.prey = null;
}
else if(cur.data == key && cur == last){
last = last.prey;
last.next = null;
}
else if(cur.data == key){
cur.prey.next = cur.next;
cur.next.prey = cur.prey;
}
cur = cur.next;
}
}
public void display(){
Node cur = this.head;
while(cur != null){
System.out.print(cur.data+" ");
cur = cur.next;
}
System.out.println();
}
public void clear(){
Node cur = this.head;
while(cur != null){
Node curNext = cur.next;
cur.prey = null;
cur.next = null;
cur = curNext;
}
this.head = null;
this.last = null;
}
}
测试
public class TestDamo{
public static void main(String[] args) {
DoubleList doubleList = new DoubleList();
doubleList.addFirst(1);
doubleList.addFirst(2);
doubleList.addFirst(3);
doubleList.addFirst(4);
doubleList.addFirst(5);
doubleList.addFirst(6);
doubleList.addLast(3);
doubleList.display();
System.out.println("长度:"+doubleList.size());
doubleList.addIndex(2,10);
doubleList.display();
doubleList.remove(2);
doubleList.display();
doubleList.removeAllKey(3);
doubleList.display();
System.out.println("========下面是清空双向链表");
doubleList.clear();
doubleList.display();
}
}
测试结果
6 5 4 3 2 1 3
长度:7
6 5 10 4 3 2 1 3
6 5 10 4 3 1 3
6 5 10 4 1
========下面是清空双向链表
========上面是清空双向链表