容器第四课,JDK源代码分析,自己实现LinkedList,双向链表的概念_节点定义

来源:互联网 发布:轮播图原生js代码 编辑:程序博客网 时间:2024/06/06 18:27
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;}}


0 0