标签:分享图片 containe container ber node bubuko dex 17. .com
项目中经常会用到LinkedList集合来存储数据,打算写一篇LinkedList的源码解析,而LinkedList是基于链表结构存储数据的,这篇博文将解析链表数据结构,包括单向链表和双向链表;
1:单向链表:
单向链表的链表对象维护了一个 first 引用,该引用指向节点链表中的第一个节点对象,每个节点对象维护一个 next 引用,next引用指向下一个节点对象;(这里注意:是引用指向的是节点对象:节点对象包含存储的数据和next引用)
以下是单向链表的图解:

java代码实现如下:
public class LinkedListDemo1 { //表示整个链表对象
    private Node first;        //链表对象的第一个引用
    
    public LinkedListDemo1(){
        
    }
    
    
    public Node getFirst() {
        return first;
    }
    public void setFirst(Node first) {
        this.first = first;
    }
    class Node{              //节点对象
         Item item;          //存储的数据对象
         Node next;          //下一个节点对象的引用
        public Item getItem() {
            return item;
        }
        public void setItem(Item item) {
            this.item = item;
        }
        public Node getNext() {
            return next;
        }
        public void setNext(Node next) {
            this.next = next;
        }
        
        
    }
}
当需要在首位置插入元素时,图解如下:first 引用指向需要插入到链表中的节点对象,新的节点对象的next引用指向原先的首节点对象;

java代码实现如下:
| 1 2 3 4 5 6 7 8 9 10 11 12 13 | //插入对象到链表首位置    publicvoidinsertFirst(Item item){        //创建链表对象        LinkedListDemo1 list=newLinkedListDemo1();        //原来的首个节点暂存在:用oldFirst引用指向        Node oldFirst=first;        //创建需要插入的节点对象        Node newNode=newNode();        newNode.item=item;        //新节点对象的next引用指向原先的首节点对象        newNode.next=oldFirst;            } | 
当然这里的插入没有考虑首位置的节点对象为null的情况,插入到其他位置的节点实现原理和插入到首位置的基本差不多;
下面接收双向链表的实现原理:
链表对象中维护一个first 引用和 last引用,分别指向链表中的首末节点对象;每个节点对象维护 存储的数据对象引用,prev和next引用,用来指向前后节点对象;
双向链表的图解:

java代码实现链表对象如下:
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | publicclassLinkedListDemo2 {    privateNode first;    privateNode last;            publicLinkedListDemo2(){            }            publicNode getFirst() {        returnfirst;    }    publicvoidsetFirst(Node first) {        this.first = first;    }            publicNode getLast() {        returnlast;    }    publicvoidsetLast(Node last) {        this.last = last;    }    classNode{         Item item;         Node prev;         Node next;        publicItem getItem() {            returnitem;        }        publicvoidsetItem(Item item) {            this.item = item;        }                publicNode getPrev() {            returnprev;        }        publicvoidsetPrev(Node prev) {            this.prev = prev;        }        publicNode getNext() {            returnnext;        }        publicvoidsetNext(Node next) {            this.next = next;        }}} | 
双向链表插入元素到首位:
图解:

java代码实现:
| 1 2 3 4 5 6 7 8 9 10 11 12 | publicvoidinsertFirst(Item item){        //暂存原先首节点对象        Node oldFirst=first;        //创建新的节点对象        Node newNode=newNode();        newNode.item=item;        newNode.next=first;        //first引用指向新节点对象        first=newNode;        //原先的节点对象的prev引用指向新节点对象        oldFirst.prev=newNode;    } | 
到此,单向链表结构和双向链表结构就解析完了,下一篇博客中将解析 LinkedList 的源码;
标签:分享图片 containe container ber node bubuko dex 17. .com
原文地址:https://www.cnblogs.com/austinspark-jessylu/p/9555122.html