定义Node 里面有 上一个 下一个 和自己的内容
定义LinkedList
里面有 first last size add(){}
链表中 get(i) 遍历链表 得到遍历的第几个值等于i时候 返回这个对象。
remove: 先 get(i) 然后将得到的节点的 上一个节点指向这个节点的下一个节点
add(obj) 在行尾追加,last.next = 新来的节点
add(obj, i) 得到节点 然后这个节点的上一个 下一个节点调整位置
代码:
//用来表示一个节点 public class Node { Node previous; //上一个节点 Object obj; Node next; //下一个节点 public Node() { } public Node(Node previous, Object obj, Node next) { super(); this.previous = previous; this.obj = obj; this.next = next; } public Node getPrevious() { return previous; } public void setPrevious(Node previous) { this.previous = previous; } public Object getObj() { return obj; } public void setObj(Object obj) { this.obj = obj; } public Node getNext() { return next; } public void setNext(Node next) { this.next = next; } } import java.util.LinkedList; public class SxtLinkedList /*implements List*/ { private Node first; private Node last; private int size; public void add(Object obj){ Node n = new Node(); if(first==null){ n.setPrevious(null); n.setObj(obj); n.setNext(null); first = n; last = n; }else{ //直接往last节点后增加新的节点 n.setPrevious(last); n.setObj(obj); n.setNext(null); last.setNext(n); last = n; } size++; } public int size(){ return size; } private void rangeCheck(int index){ if(index<0||index>=size){ try { throw new Exception(); } catch (Exception e) { e.printStackTrace(); } } } public Object get(int index){ //2 rangeCheck(index); // 0 1 2 3 4 Node temp = node(index); if(temp!=null){ return temp.obj; } return null; } public Node node(int index){ Node temp = null; if(first!=null){ if (index < (size >> 1)) {// 获取链表的前一半,如果查询的角标在链表前一半位置时 temp = first; for(int i=0;i<index;i++){ temp = temp.next; } }else{ temp = last; for (int i = size - 1; i > index; i--){// 获取链表后半截,如果查询的角标在链表的后一半时,从最后一个元素开始向前查询 temp = temp.previous; } } } // LinkedList l; return temp; } public void remove(int index){ Node temp = node(index); if(temp!=null){ Node up = temp.previous; Node down = temp.next; up.next = down; down.previous = up; size--; } } public void add(int index,Object obj){ Node temp = node(index); Node newNode = new Node(); newNode.obj = obj; if(temp!=null){ Node up = temp.previous; up.next = newNode; newNode.previous = up; newNode.next = temp; temp.previous = newNode; size++; } } public static void main(String[] args) { SxtLinkedList list = new SxtLinkedList(); list.add("aaa"); list.add("bbb"); // list.add(1,"BBBB"); list.add("ccc"); list.add("ddd"); list.add("eee"); // list.remove(1); System.out.println(list.get(3)); } }