链表常见操作java实现一:链表初始化,求链表长度

来源:互联网 发布:卷积运算公式矩阵 编辑:程序博客网 时间:2024/05/22 03:11

转自http://blog.csdn.net/bitcarmanlee/article/details/52155181

1.前言

链表是一种非常基础也非常重要的数据结构,在实际中使用非常广泛,也是各种面试里特别容易出现的类型。尤其在各大IT公司的校招中,不管笔试还是面试,链表几乎都是必出现的题型。因此不管从实际工作场景中,还是在找工作的过程中,熟练掌握链表的相关操作都显得非常重要。

看看wiki里给链表的介绍: 
链表(Linked list)是一种常见的基础数据结构,是一种线性表,但是并不会按线性的顺序存储数据,而是在每一个节点里存到下一个节点的指针(Pointer)。由于不必须按顺序存储,链表在插入的时候可以达到O(1)的复杂度,比另一种线性表顺序表快得多,但是查找一个节点或者访问特定编号的节点则需要O(n)的时间,而顺序表相应的时间复杂度分别是O(logn)和O(1)。

使用链表结构可以克服数组链表需要预先知道数据大小的缺点,链表结构可以充分利用计算机内存空间,实现灵活的内存动态管理。但是链表失去了数组随机读取的优点,同时链表由于增加了结点的指针域,空间开销比较大。

在计算机科学中,链表作为一种基础的数据结构可以用来生成其它类型的数据结构。链表通常由一连串节点组成,每个节点包含任意的实例数据(data fields)和一或两个用来指向上一个/或下一个节点的位置的链接(”links”)。链表最明显的好处就是,常规数组排列关联项目的方式可能不同于这些数据项目在记忆体或磁盘上顺序,数据的访问往往要在不同的排列顺序中转换。而链表是一种自我指示数据类型,因为它包含指向另一个相同类型的数据的指针(链接)。链表允许插入和移除表上任意位置上的节点,但是不允许随机存取。链表有很多种不同的类型:单向链表,双向链表以及循环链表。

理论部分就介绍到这里,相信只要稍微有点计算机基础的同学都能看懂是什么意思。接下来,我们上代码,看看怎么在Java中实现链表的相关操作。

2.链表的初始化

一般链表初始化的时候,会将Node节点作为内部类至于LinkedList中。但是因为这次我们的操作比较多,代码比较复杂,为了方便起见,将Node类单独拎出来作为一个类。

public class Node<T> {    public T data;    public Node next;    public Node(T data) {        this.data = data;    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

Node类很简单,一个数值域data,一个指针域next,另外加一个构造方法。 
接下来看我们的MyLinkedList类:

public class MyLinkedList {    public Node head;    public Node current;    public void add(int data) {        //如果头结点为空,为头结点        if(head == null) {            head = new Node(data);            current = head;        } else {            current.next = new Node(data);            current = current.next;        }    }    //打印链表    public void print(Node node) {        if(node == null) {            return;        }        current = node;        while(current != null) {            System.out.print(current.data + " ");            current = current.next;        }    }    //初始化链表,并且返回表头    public Node init() {        for(int i=0; i<10; i++) {            this.add(i);        }        return head;    }    //求链表长度    public int get_length(Node head) {        if (head == null) {            return -1;        }        int length = 0;        current = head;        while(current != null) {            length++;            current = current.next;        }        return length;    }}
  • 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
  • 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

这个MyLinkedList类中,包含有添加节点,打印链表,初始化链表,以及求链表长度的操作。MyLinkedList类以及Node类,将在我们后续的代码中多处被使用到。

3.初步测试

public class TestDemo {    public static void main(String[] args) {        MyLinkedList list = new MyLinkedList();        Node head = list.init();        list.print(head);        int length = list.get_length(head);        System.out.println();        System.out.println("The length of list is: " + length);    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

将测试代码run起来:

0 1 2 3 4 5 6 7 8 9 The length of list is: 10

原创粉丝点击