package com.pkushutong.Collection;
public class Test03 {
private Test03_01 first; //第一个节点
private Test03_01 last; //最后一个节点
private int size;
public void add(Object obj){
Test03_01 t = new Test03_01();
if(first == null){
t.setPrevious(null);
t.setObj(obj);
t.setNext(null);
first = t;
last = t;
}else{
//直接往last借点后增加新的节点
t.setPrevious(last);
t.setObj(obj);
t.setNext(null);
last.setNext(t);
last = t;
}
size++;
}
public int size(){
return size;
}
public Object get(int index){
Test03_01 temp = node(index);
return temp.obj;
}
public void remove(int index){
Test03_01 temp = node(index);
if(temp != null){
Test03_01 up = temp.previous;
Test03_01 down = temp.next;
up.next = down;
down.previous = up;
size--;
}
}
public void add(int index,Object obj){
Test03_01 temp = node(index);
Test03_01 newTest03_01 = new Test03_01();
newTest03_01.obj = obj;
if(temp != null){
Test03_01 up = temp.previous;
up.next = newTest03_01;
newTest03_01.previous = up;
newTest03_01.next = temp;
temp.previous = newTest03_01;
size++;
}
}
private Test03_01 node(int index) {
Test03_01 temp = null;
if(first != null){
temp = first;
for(int i=0; i<index; i++){
temp = temp.next;
}
}
return temp;
}
public static void main(String[] args) {
Test03 list = new Test03();
list.add("123");
list.add("234");
list.add("345");
//list.remove(1);
list.add(1, "aaaa");
System.out.println(list.get(1));
}
}
package com.pkushutong.Collection;
/**
* 用来表示一个节点
* @author dell
*
*/
class Test03_01{
Test03_01 previous;
Object obj;
Test03_01 next;
public Test03_01() {
}
public Test03_01(Test03_01 previous, Object obj, Test03_01 next) {
super();
this.previous = previous;
this.obj = obj;
this.next = next;
}
public Test03_01 getPrevious() {
return previous;
}
public void setPrevious(Test03_01 previous) {
this.previous = previous;
}
public Object getObj() {
return obj;
}
public void setObj(Object obj) {
this.obj = obj;
}
public Test03_01 getNext() {
return next;
}
public void setNext(Test03_01 next) {
this.next = next;
}
}