Arrays and linked lists based on Java

​ ​ The definition of an array is: a limited set of variable storage of the same type. Each variable in the array is called an element, and each element has its own subscript (starting from 0)​. Data is stored sequentially in memory​.

Simple implementation of array​:​

public class Array {
    
    //数据元素
    private int[] array;
    
    //当前数组存储数的个数
    private int size;

    public Array(int capacity){
        this.array = new int[capacity];
        this.size = 0;
    }
    /**
     * @Description
     * @Param  [num 添加的元素]
     * @Date 2019-8-18 20:31
     * @Version  1.0
     */
    public void add(int num){
        //对数组进行扩容
        if(size > array.length){
            resize();
        }
        //添加元素
        array[size++] = num;
    }
    /**
     * @Description 数组插入元素
     * @Param  [element 插入的数据, index 插入的位置]
     * @Date 2019-8-18 19:35
     * @Version  1.0
     */
    public void insert(int element,int index){
        //判断下表是否超出数组范围
        if(index < 0 || index > size){
            throw new IndexOutOfBoundsException("超出数组容量");
        }
        //对数组进行扩容
        if(size > array.length){
            resize();
        }
        for(int i = size - 1;i >= index;i--){
            array[i+1] = array[i];
        }
        //插入元素
        array[index] = element;
        size++;
    }
    /**
     * @Description 对数据进行扩容
     * @Param  null
     * @Date 2019-8-18 19:43
     * @Version  1.0
     */
    private void resize() {
        //数组扩容为原来的2倍
        int[] newArray = new int[array.length * 2];
        //将数据从旧的数组拷贝到新数组
        System.arraycopy(array,0,newArray,0,array.length);
        array = newArray;
    }
    /**
     * @Description 获取数据
     * @Param  [index 获取数据的下标]
     * @Date 2019-8-18 20:34
     * @Version  1.0
     */
    public int get(int index){
       return array[index];
    }

    public static void main(String[] args){
        Array array = new Array(10);
        array.insert(3,0);
        array.insert(2,1);
        array.insert(5,2);
        array.insert(6,3);
        array.insert(7,1);
        array.add(9);
        for (int i = 0; i < array.size; i++) {
            System.out.println(array.get(i));
        }
    }

It should be noted that the array has an initial capacity. When the stored elements reach the maximum capacity, a new array is created with a capacity twice the current array, and then the elements of the old array are copied to the new array.

The definition of a linked list: a number structure that is physically non-contiguous and non-sequentially stored and consists of nodes. Contains two parts, one is the stored data, and the other is the pointer to the next node. The first node of the linked list is called the head node, and the last is the tail node.

Simple implementation of linked list:

public class Linked {
    //链表头节点
    private Node head;
    //链表未节点
    private Node last;
    //链表长度
    private int size;//默认值为0

    /**
     * @Description 链表末尾追加数据
     * @Param  [data 添加的数据]
     * @Date 2019-8-18 21:38
     * @Version  1.0
     */
    public void add(Object data) {
        Node inner = new Node(data);
        if(size == 0){
            head = inner;
            last = inner;
        }else{
           /* 没有理解对象引用
            Node node = get(size-1); //node==last
            node.next = inner;
            last = inner;*/
            //Node node = get(size-1);
            //System.out.println(node == last);//true
            last.next = inner;
            last = inner;
        }
        size++;
    }
    /**
     * @Description 链表插入元素
     * @Param  [data 插入的数据, index 插入的位置]
     * @Date 2019-8-18 21:01
     * @Version  1.0
     */
    public void insert(Object data,int index){
        if(index < 0 || index > size){
            throw new IndexOutOfBoundsException("下标越界");
        }
        Node inner = new Node(data);
        if(size == 0 ){//链表为空
            head = inner;
            last = inner;
        }else if(index == 0 ){//插入头部
            //System.out.println(head == last);//true 操作head相当于操作last
            inner.next = head;
            head = inner;
            //System.out.println(head.next == last);//true
            //System.out.println(inner.next == last);//true
        }else if(index == size){//尾部插入
            last.next = inner;
            last = inner;
        }else {//插入中间
            Node prevNode = get(index-1);
            if(prevNode == null){
                throw new NullPointerException("下标链表为空!");
            }
            inner.next = prevNode.next;
            prevNode.next = inner;
        }
        size++;
    }
    /**
     * @Description
     * @Param  [index 删除的下标]
     * @Date 2019-8-18 21:28
     * @Version  1.0
     */
    public Node remove(int index) {
        if(index < 0 || index >= size){
            throw new IndexOutOfBoundsException("下标越界");
        }
        Node removeNode = null;
        if(index == 0){//删除头部
            removeNode = head;
            head = head.next;
        }else if(index == size){//删除尾部
            Node lastNode = get(index-1);
            removeNode = lastNode.next;
            lastNode.next = null;
            last = lastNode;
        }else{//删除中间
            Node pervNode = get(index-1);
            pervNode.next = pervNode.next.next;
            removeNode = pervNode.next;
        }
        return removeNode;
    }
    /**
     * @Description 根据下标获取链表节点
     * @Param  [index 下标]
     * @Date 2019-8-18 23:02
     * @Version  1.0
     */
    public Node get(int index) {
        if(index < 0 || index >= size){
            throw new IndexOutOfBoundsException("下标越界");
        }
        Node node = head;
        for (int i = 0; i < index; i++) {
            node = node.next;
            if(node.next == null){
                break;
            }
        }
        return node;
    }
    /**
     * @Description 输出链表数据
     * @Param  NULL
     * @Date 2019-8-18 23:06
     * @Version  1.0
     */
    public void output(){
        Node next = head;
        while (next.next != null){
            System.out.println(next.data);
            next = next.next;
        }
        System.out.println(next.data);
    }
    /**
     * @Description 链表节点
     * @Date 2019-8-18 23:04
     * @Version  1.0
     */
    public static class Node {
        //存储的数据
        public Object data;
        //下一节点
        public Node next;
        public Node(Object data){
            this.data = data;
        }
    }

    public static void main(String[] args){
        Linked linked = new Linked();
        linked.add("123");
        linked.add(1234);
        linked.add("ad");
        linked.add("trt");
        linked.insert("111",0);
        linked.insert("222",2);
        linked.insert("333",6);
        linked.output();
        System.out.println("**************************");
        linked.remove(4);
        linked.output();

    }
}

Here you need to pay attention to the storage and reference of the object. The object is stored on the heap, and the stack is just a copy of the reference of the object. Just as a data is stored in the linked list, the head node and the tail node point to the node where the data is stored. At this time, if a piece of data is inserted in the head, although the head node is operated, the head node and the tail node The nodes are the same object, which is equivalent to operating the tail node.

Comparison of data and linked list

  • The search and update of the array are fast, only one operation is required, and the insertion and deletion operations are N.
  • Linked list lookup is slow, the number of operations is N, and only one update, insertion and deletion are required.
  • Arrays are suitable for reading more and writing less, while linked lists are the opposite.

Guess you like

Origin blog.csdn.net/qq_34758074/article/details/99763875