简单实现LinkedList(双向链表)

简介: 看源码和视频自己写了写,当然和源码差了n个数量级. 也算复习了下链表SetLinkedList类package cn.

看源码和视频自己写了写,当然和源码差了n个数量级. 也算复习了下链表

SetLinkedList类

package cn.hjy.collection;

import java.util.List;
/** 
 * @author 543363559@qq.com
 * @date 2017年4月30日 下午4:47:34 
 */
public class SetLinkedList {
    private Node first;
    private Node last;

    private int size;
/**
 * 
 * @Description: 添加结点 建first or 往last.next添加
 * @param @param obj
 * @param @return   
 * @return boolean
 */
    public boolean 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++;
        return true;
    }
/**
 * 
 * @Description: 返回链表大小
 * @param @return   
 * @return int
 */
    public int size(){
        return size;
    }
/**
 * 
 * @Description: 删除指定索引位置对象
 * @param @param index   
 * @return void
 */
    public void remove(int index){
        //得到结点
        Node temp = node(index);

        if (temp != null) {
            Node up = temp.getPrevious();
            Node down = temp.getNext();
            up.setNext(down);
            if (down != null) {
                down.setPrevious(up);
            }
        } 
        size--;
    }
/**
 * 
 * @Description: 获得想要位置的结点
 * @param @param index
 * @param @return   
 * @return Node
 */
    public Node node(int index) {
        Node temp = null;
        if (first != null) {
            temp = first;
            for (int i = 0; i < index; i++) {
                temp = temp.getNext();
            }
        }
        return temp;
    }
/**
 * 
 * @Description: 得到index的obj值
 * @param @param index
 * @param @return   
 * @return Object
 */
    public Object get(int index){
        rangeCheck(index); //越界处理

        Node temp = node(index);
        if (temp != null) {
            return temp.getObj();           
        } else {
            return null;
        }
    }
/**
 * 
 * @Description: 在链表index位置添加结点
 * @param @param index
 * @param @param obj   
 * @return void
 */
    public void add(int index,Object obj) {
        Node temp = node(index);

        Node newNode = new Node();
        newNode.setObj(obj);

        if (temp != null) {
            Node up = temp.getPrevious();
            up.setNext(newNode);
            newNode.setPrevious(up);

            newNode.setNext(temp);
            temp.setPrevious(newNode);
        }
        size++;
    }
/**
 * 
 * @Description: 修改链表中 index的obj
 * @param @param index
 * @param @param obj   
 * @return void
 */
    public void set(int index,Object obj) {
        Node temp = node(index);
        if (temp != null) {
            temp.setObj(obj);
        }
    }
/**
 * 
 * @Description: 判断index是否超出链表范围,超出则抛出异常
 * @param @param index   
 * @return void
 */
    private void rangeCheck(int index) {
        if (index < 0 || index >= size) {
            try {
                throw new Exception();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
/**
 * 
 * @Description: 遍历显示
 * @param    
 * @return void
 */
    public void show() {
        Node temp = new Node();
        if (first != null) {
            temp = first;
            while (temp != null) {
                System.out.print(temp.getObj() + " ");
                temp = temp.getNext();
            }
        }

    }
    public SetLinkedList() { 

    }

    public static void main(String[] args) {
        SetLinkedList list = new SetLinkedList();
        list.add("aaa");
        list.add("bbb");

        list.add(1, "www");
        System.out.println(list.get(1)); //www
        list.set(1, "qqq");
        System.out.println(list.get(1)); //qqq
        list.remove(1);
        System.out.println(list.get(1)); //bbb
        list.show(); //aaa bbb
        System.out.println(list.size()); //2
    }

}

Node类

package cn.hjy.collection;

class Node {
    private Node previous;
    private Object obj;
    private Node next;

    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;
    }



}
目录
相关文章
|
3月前
|
Java
LinkedList与链表(有源码剖析)(一)
LinkedList与链表(有源码剖析)
37 0
|
1月前
|
设计模式 测试技术
在实现链表的代码中,为什么要使用`Node`类而不是直接在`LinkedList`类中定义节点?
在实现链表的代码中,为什么要使用`Node`类而不是直接在`LinkedList`类中定义节点?
20 1
|
1月前
|
算法 Java 索引
【数据结构与算法】4、双向链表(学习 jdk 的 LinkedList 部分源码)
【数据结构与算法】4、双向链表(学习 jdk 的 LinkedList 部分源码)
31 0
|
1月前
|
存储 算法 Java
【数据结构与算法】2、链表(简单模拟 Java 中的 LinkedList 集合,反转链表面试题)
【数据结构与算法】2、链表(简单模拟 Java 中的 LinkedList 集合,反转链表面试题)
42 0
|
6月前
|
存储 安全 Java
【JavaSE专栏49】Java集合类LinkedList解析,链表和顺序表有什么不同?
【JavaSE专栏49】Java集合类LinkedList解析,链表和顺序表有什么不同?
|
2月前
|
存储
数据结构 模拟实现LinkedList双向不循环链表
数据结构 模拟实现LinkedList双向不循环链表
57 1
|
2月前
|
存储
数据结构 模拟实现LinkedList单向不循环链表
数据结构 模拟实现LinkedList单向不循环链表
33 0
|
3月前
|
存储 Java 容器
LinkedList与链表(有源码剖析)(二)
LinkedList与链表(有源码剖析)(二)
30 0
|
8月前
|
存储 Java
Java数据结构之第五章、LinkedList与链表
由于其底层是一段连续空间,当在ArrayList任意位置插入或者删除元素时,就需要将后序元素整体往前或者往后搬移,时间复杂度为O(n),效率比较低,因此ArrayList不适合做任意位置插入和删除比较多的场景。
47 0
|
6月前
|
机器学习/深度学习 存储
链表oj题 && 链表与LinkedList && 栈的概念 && 队列的概念 && 树和二叉树
链表oj题 && 链表与LinkedList && 栈的概念 && 队列的概念 && 树和二叉树
110 38