Brief discussion list

1. The chain storage
(1) is a chain of data elements stored in a storage unit for storing an arbitrary set of linear form (which may be contiguous group of memory cells may be discontinuous);
(2) In order to represent each data the elements a [i] a direct relationship with its subsequent element [i + 1], the data elements a [i], not only needs to store information about itself, the need to store an indication of its direct successor information, both information part is referred to as nodes.
2. Definition of nodes
node pointer field comprises a data field and a two-part
data field: the information element storing data
pointer field: a storage location directly subsequent element (referred to as a domain information storing pointers or pointer chains), an n linked list of nodes connected to each linear table is referred to.

/**
 * 创建一个结点
 * 结点包括两个数据域和指针域两个部分
 */
class Node{
    private int data;
    private Node next;//指针将一个个结点链接在一起

    public int getData() {
        return data;
    }

    public void setData(int data) {
        this.data = data;
    }

    public Node getNext() {
        return next;
    }

    public void setNext(Node next) {
        this.next = next;
    }
}
public class 链表 {
    public static void main(String[] args) {
        /**
         * 向链表里面存储元素
         */
        Node head = new Node();//创建一个头结点
        head.setData(21);//通过set方法存储数据
        Node n = new Node();//想在放第二个数据,就需要再创建一个新的结点
        n.setData(34);//将第二个数据放进去
        head.setNext(n);
        /**
         * n把自己的地址通过set方法交给头结点head里面的指针Next,
         * 通过这种方法将两个数据连接起来,这样就可以通过指针来访问下一个结点的数据
         */
        n = new Node();//在添加第三个数据
        n.setData(23);
        head.getNext().setNext(n);//getNext拿到下一个结点的引用,再把新的地址交给上一个结点保存

        n = new Node();
        n.setData(18);
        head.getNext().getNext().setNext(n);
        System.out.println(head.getData());//用get方法取出数据第一个元素
        System.out.println(head.getNext().getData());
        System.out.println(head.getNext().getNext().getData());
        System.out.println(head.getNext().getNext().getNext().getData());
    }
}

Some operation on the list of elements

public class LinkedList1 {
    /**
     * 封装一个链表
     *
     * @param
     * @return
     */
    private class Node {//创建一个节点类(包括数据域和指针域两部分)
        private int data;
        private Node next;//指针将一个个结点链接在一起

        public Node getNext() {
            return next;
        }

        public int getData() {
            return data;
        }

        public void setData(int data) {
            this.data = data;
        }

        public void setNext(Node next) {
            this.next = next;
        }
    }

    private Node head = null;//一开始头结点为空
    private int size = 0;//元素个数
    public Integer get(int i) {
        if(i == 0) return head.getData();//取出头结点指向的结点数据
        Node temp = head;
        for(int t = 0;t < i;t++){
            temp = temp.getNext();
            if(temp == null) return null;
        }
       return temp.getData();
    }

    public void remove(int i) {

    }

    public void add(int n) {
        Node d = new Node();
        d.setData(n);//把n通过set方法赋值给d
        if(head == null){
            head = d;
        }else{
            Node temp = head;//将head的地址赋值给temp
         for(int i = 0;i < size - 1;i++){
            temp = temp.next;
         }
         temp.setNext(d);//给头结点所指向的next赋值
        }
        size++;
    }
}

public class TestLinkdlist1 {
    public static void main(String[] args) {
        LinkedList1 l = new LinkedList1();
        l.add(23);
        l.add(45);
        l.add(67);

        System.out.println(l.get(2));
    }
}

Guess you like

Origin blog.csdn.net/weixin_44084434/article/details/90734809