LinkedList简单实现

package com.yys.student;

/**
 * Created by yys on 2017/5/6.
 */
public class SxtLinkedList {
    private Node first;//第一个对象
    private Node last;//第二第三第...个对象
    private int size;//list大小
    /**
     * add(Object object)链表中添加一个对象
     * 先判断第一个对象是否为空
     * 如果是第一个对象为空 添加到此节点
     * 第一次添加时双向链表的前一个指针指向是空 后一个指针指向也是空
     * 是第一个元素也是最后一个元素 但元素本身不为空 图解为(null,object,null)
     * else
     * 如果不是第一次添加 即代表第一个已有值然后不断向后添加
     * 假设此时往第二个位置添加
     * 链表第二个的前一个指针位置应该是fist 图解为(null,object,first)(first,object,null)
     */
    public void add(Object object){
        Node node = new Node();
        if(first==null){
            node.setPrevious(null);
            node.setObj(object);
            node.setNext(null);
            first = node;
            last = node;
        }else{
            node.setPrevious(last);
            node.setObj(object);
            node.setNext(null);
            last.setNext(node);
            last = node;
        }
        size++;
    }
    /**
     * get(int index)查找一个对象
     * 先判断下标是否越界
     * 先判断第一个对象是否为空
     * 如果是第一个对象不为空 从此处遍历查询
     */
    public Object get(int index){
        rangeCheck(index);
        Node temp = null;
        if(first!=null){
            temp = first;
            for(int i=0;i<index;i++){
                temp = temp.next;
            }
        }
        return temp.obj;
    }
    /**
     * get(int index)移除一个对象
     * 先判断下标是否越界
     * 获取下标位置节点 后取出此节点的上一个节点 和下一个节点
     *删除此节点就是将上一节点的尾链指向下一节点 下一节点首链向上一节点
     * 下标--
     */
    public void remove(int index){
        rangeCheck(index);
        Node temp = node(index);
        if(temp != null){
            Node up = temp.previous;
            Node down = temp.next;
            up.next = down;
            down.previous = up;
        }
        size--;
    }
    /**
     * add(int index,Object obj)指定位置添加一个对象
     * 先获取下标位置节点
     * new 一个新节点
     * 判断获取下标位置节点是否为空
     * 如果不为空 取出下标位置的上一节点 修改上一节点 (此时上一节点的下一节点是新加入节点) (新加入节点的上一节点是取出的上一节点)
     *新节点下一节点为取出的下标节点 下标节点的上一节点是新加入节点
     * 下标--
     */
    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 Node node(int index){
        Node temp = null;
        if(first!=null){
            temp = first;
            for(int i=0;i<index;i++){
                temp = temp.next;
            }
            if(temp != null){
                return temp;
            }
        }
        return  null;
    }
    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 static void main(String args[]){
        SxtLinkedList list = new SxtLinkedList();
        list.add("aaa");
        list.add("bbb");
        list.add(1,"BBB");
        list.add("ccc");
//        list.remove(1);
        System.out.println(list.get(1));
    }
}

class Node{
     Node previous;//上一个节点
     Object obj;
     Node next;//下一个节点

    public Node() {
    }

    public Node(Object obj, Node next, Node previous) {
        this.obj = obj;
        this.next = next;
        this.previous = previous;
    }

    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;
    }
}
发布了45 篇原创文章 · 获赞 36 · 访问量 18万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章