[Java data structure] learning linked lists and some simple operations

learning target:

Goal: quickly master the knowledge of Java data structure


Learning Content:

The content of this article: Learning Linked List


1. The principle of linked lists

Insert picture description here
Element (Element): real content of the table in a linear
junction (node): a list structure organization introduced, in addition to preserve our element other than, also stored at a node reference points

class Node{
    
    
	int val;//保存元素
	Node next;//保存指向下一个结点的引用,其中尾结点的next=null
}

At present, the entire linked list is represented by the head node

//head是一条链表的头结点,通过head可以找到链表所有结点,所以用头结点完全代表整个链表
Node head;

//某条链表的头结点是空,表示头结点不存在,也就是空链表
Node head=null;

2. Code implementation of linked list

2.1 The node definition of the linked list

public class Node {
    
    
    int val;
    String sval;
    Node next;

    public Node(int val) {
    
    
        this.val = val;
    }

    public Node(String sval) {
    
    
        this.sval = sval;
    }

    @Override
    public String toString() {
    
    
        return "[" + val +"]";
    }
}

2.2 Create a linked list

Create a [1 2 3 4] linked list

public static Node creatList() {
    
    
     Node a = new Node(1);
     Node b = new Node(2);
     Node c = new Node(3);
     Node d = new Node(4);
     
     a.next = b;
     b.next = c;
     c.next = d;
     d.next = null;
     
     return a;
}

3. Some simple operations of linked lists

3.1 Through traversal, print all elements of the linked list

 Node head = creatList();
 System.out.println("for循环遍历并打印链表:");
 for (Node cur = head; cur != null; cur = cur.next) {
    
    
	 System.out.println(cur.val);
}

System.out.println("while循环打印链表:");
Node cur = head;
while (cur != null) {
    
    
    System.out.println(cur.val);
    cur = cur.next;
}
//运行结果
for循环打印链表:
1
2
3
4
while循环打印链表:
1
2
3
4

3.2 Find the last node of the linked list by traversing

 private static void findEnd() {
    
    
        System.out.println("链表最后一个元素:");
        Node head = creatList();
        for (Node cur = head; cur != null; cur = cur.next) {
    
    
            if (cur.next == null) {
    
    
                System.out.println(cur.val);
            }
        }
    }
//运行结果
链表最后一个元素:
4

3.3 Through traversal, find the penultimate node of the linked list

    private static void findEndTwo() {
    
    
        System.out.println("链表倒数第二个元素:");
        Node head = creatList();
        Node cur = head;
        while (cur != null && cur.next != null) {
    
    
            if (cur.next.next == null) {
    
    
                System.out.println(cur.val);
            }
            cur = cur.next;
        }
    }
//运行结果
链表倒数第二个元素:
3        

3.4 Find the nth node of the linked list by traversing

 private static void findN(int n) {
    
    
        System.out.println("链表第"+n+"个元素");
        Node head = creatList();
        Node cur = head;
        for (int i = 1; i < n; i++) {
    
    
            cur = cur.next;
        }
        System.out.println(cur.val);

    }
 //当n=2时的运行结果
链表第2个元素
2

3.5 Find the nth node from the bottom of the linked list by traversing

 private static void findEndN(int n) {
    
    
        System.out.println("链表倒数第"+n+"个元素");
        int count = 0;//计算链表长度
        Node head = creatList();
        for (Node cur = head; cur != null; cur = cur.next) {
    
    
            count++;
        }
        Node cur = head;
        for (int i = 1; i < count + 1 - n; i++) {
    
    
            cur = cur.next;
        }
        System.out.println(cur.val);
    }
 //当n=2时的运行结果
 链表倒数第2个元素
 3

3.6 Determine whether the linked list contains an element

    //断链表是否包含某个元素
    private static boolean contains(int e) {
    
    
        Node head = creatList();
        for (Node cur = head; cur != null; cur = cur.next) {
    
    
            if (cur.val == e) {
    
    
                return true;
            }
        }
        return false;
    }

Guess you like

Origin blog.csdn.net/zhangxxin/article/details/113565227