双向链表,每个节点有一个属性值,一个指向前一个节点prev,一个指向后一个节点next,
Link接口:
public interface Link {
boolean add(Object obj); //添加
boolean remove(Object obj); //删除
int length(); //求长度
Object[] toArray(); //转换为数组
boolean contains(Object obj);//包含
int indexOf(Object obj); //查找第几个
boolean set(int index,Object newElement);//修改
Object get(int index); //返回index的节点
void clear(); //清空
void print();
}
实现Link接口的LinkImpl类:
public class LinkImpl implements Link {
private Node first;
private Node last;
private int size=0;
private class Node{
Object item;
private Node prev;
private Node next;
private Node(Object item, Node prev, Node next) {
this.item = item;
this.prev = prev;
this.next = next;
}
}
@Override
public boolean add(Object obj) {
if(this.first==null) {
this.first = new Node(obj,null,null);
this.last = this.first;
this.size++;
return true;
}
Node newNode = new Node(obj,this.last,null);
this.last.next = newNode;
this.last = newNode;
this.size++;
return true;
}
@Override
public int length() {
return this.size;
}
@Override
public Object[] toArray() {
if(this.size==0) {
return null;
}
Object[] objects =new Object[this.size];
Node cur = this.first;
for(int i=0;i<this.size;i++) {
objects[i]=cur.item;
cur=cur.next;
}
return objects;
}
@Override
public boolean contains(Object obj) {
return indexOf(obj)!=-1;
}
@Override
public int indexOf(Object obj) {
int index = 0;
Node cur = this.first;
if(obj==null) {
while(cur!=null) {
if(cur.item==null) {
return index;
}
cur=cur.next;
index++;
}
}else {
while(cur!=null) {
if(obj.equals(cur.item)) {
return index;
}
cur=cur.next;
index++;
}
}
return -1;
}
private Node node(int index) {
Node cur = this.first;
if(index<(this.size>>1)) {
for(int i=0;i<index;i++) {
cur=cur.next;
}
}else {
cur = this.last;
for(int i=this.size-1;i>index;i--) {
cur = cur.prev;
}
}
return cur;
}
@Override
public boolean set(int index, Object newElement) {
if(index>=this.size||index<0) {
return false;
}
Node cur = node(index);
cur.item=newElement;
return true;
}
@Override
public Object get(int index) {
if(index>=this.size||index<0) {
return null;
}
Node cur = node(index);
return cur.item;
}
@Override
public void clear() {
Node cur = this.first;
while(cur!=null) {
Node next = cur.next;
cur.item=null;
cur.prev=null;
cur.next=null;
cur=next;
}
this.first=null;
this.last=null;
this.size=0;
}
@Override
public boolean remove(Object obj) {
int index = indexOf(obj);
if(index==-1) {
return false;
}
Node div = this.first;
for(int i=0;i<index;i++) {
div = div.next;
}
if(div==this.first) {
if(div==this.last) {
this.first=this.last=null;
}else {
Node tmp = this.first;
this.first = tmp.next;
this.first.prev = null;
tmp.next = null;
tmp = null;
}
}
else if(div==this.last) {
Node tmp = this.last;
this.last = tmp.prev;
this.last.next = null;
tmp.prev = null;
tmp = null;
}else {
div.prev.next=div.next;
div.next.prev=div.prev;
div.prev=null;
div.next=null;
}
this.size--;
return true;
}
@Override
public void print() {
Node cur = this.first;
while(cur!=null) {
System.out.print(cur.item+" ");
cur=cur.next;
}
System.out.println();
}
}
产生Link的工厂类:
public class Factory {
private Factory() {
}
public static Link getLinkList() {
return new LinkImpl();
}
}
Test主类:
public class Test {
public static void main(String[] args) {
Link list = Factory.getLinkList();
list.add("一");
list.add("二");
list.add("三");
list.add("四");
list.add("五");
list.print();
System.out.println(list.get(-1));
System.out.println(list.get(0));
System.out.println(list.get(1));
System.out.println(list.get(5));
int index=list.indexOf("四");
System.out.println("四在第几个位置:"+index);
System.out.println("length:"+list.length());
System.out.println(list.remove(list.get(7)));
System.out.println(list.remove(list.get(2)));
System.out.println(list.remove(list.get(0)));
list.print();
Object[] objects = list.toArray();
for(int i=0;i<objects.length;i++) {
System.out.print(objects[i]+" ");
}
System.out.println();
System.out.println("##########");
list.clear();
System.out.println(list.get(0));
list.print();
}
}
测试结果: