Data structure and algorithm -- linked list

Keywords: Java Algorithm data structure

catalogue

1, Single linked list

1.1 basic introduction

1.2 schematic diagram of single linked list logic structure

1.3 adding and deleting data in single linked list (sorted by data number)

one point four   Find the penultimate node in the single lin k ed list

1.5 reverse single chain table

one point six   Disadvantages of single linked list

2, Bidirectional linked list

2.1 basic introduction

2.2 adding and deleting data in single linked list (sorted by data number)

3, Joseph Ring

3.1 basic introduction

three point two   Ring linked list to solve Joseph problem

1, Single linked list

1.1 basic introduction

The linked list is stored in the form of nodes. Each node contains the data field and the next field (pointing to the next node).

The nodes of the linked list are not necessarily stored continuously.

The linked list is divided into the linked list with the leading node and the linked list without the head node.

1.2 schematic diagram of single linked list logic structure

1.3 adding and deleting data in single linked list (sorted by data number)

code:

public class SingleLinkedListDemo {
    public static void main(String[] args) {
        MyNode myNode1 = new MyNode(10);
        MyNode myNode2 = new MyNode(30);
        MyNode myNode3 = new MyNode(20);
        MyNode myNode4 = new MyNode(40);
        MyNode myNode5 = new MyNode(10);

        SingleLinkedList singleLinkedList = new SingleLinkedList();
        singleLinkedList.add(myNode1);
        singleLinkedList.add(myNode2);
        singleLinkedList.add(myNode3);
        singleLinkedList.add(myNode4);
        singleLinkedList.add(myNode5);
        singleLinkedList.delete(40);
        singleLinkedList.list();
        System.out.println("Number of nodes:" + singleLinkedList.getLength());
    }
}

class SingleLinkedList {
    //Initialize header node
    private MyNode head = new MyNode(0);

    //Add node
    public void add(MyNode myNode) {
        if (head.next == null) {
            head.next = myNode;
            return;
        }
        MyNode cur = head.next;

        while (cur != null) {
            //If the current node is at the end of the linked list, add a node at the end
            if (cur.next == null) {
                cur.next = myNode;
                break;
            }
            //Number already exists
            else if (cur.no == myNode.no) {
                System.out.println("The number of added data already exists" + myNode.no);
                return;
            }
            //Insert node after current node
            else if (cur.next.no > myNode.no) {
                //Add a node after the temp node. The next node of myNode points to the next node of temp.
                myNode.next = cur.next;
                //The next node of temp points to myNode.
                cur.next = myNode;
                break;
            }
            //Backward node
            cur = cur.next;
        }
    }

    //Delete node
    public void delete(int no) {
        MyNode cur = head;

        while (cur != null) {
            if (cur.next == null) {
                System.out.println("The data to be deleted does not exist");
            } else if (cur.next.no == no) {
                cur.next = cur.next.next;
                return;
            }
            cur = cur.next;
        }
    }

    //Traversal linked list
    public void list() {
        if (head.next == null) {
            System.out.println("The linked list is empty!");
            return;
        }
        //cur represents the current node
        MyNode cur = head.next;

        while (cur != null) {
            System.out.println(cur);
            cur = cur.next;
        }
    }

    //Get the number of nodes in the linked list
    public int getLength() {
        int sum = 0;
        if (head.next == null) {
            return 0;
        }

        //cur represents the current node
        MyNode cur = head.next;

        while (cur != null) {
            cur = cur.next;
            sum++;
        }
        return sum;
    }
}

class MyNode {
    public int no;
    public MyNode next; //Point to next node

    //constructor 
    public MyNode(int no) {
        this.no = no;
    }

    @Override
    public String toString() {
        return "MyNode{" +
                "no=" + no +
                '}';
    }
}

result:

one point four   Find the penultimate node in the single lin k ed list

Using fast and slow pointers

code:

    //Find the penultimate node in the single lin k ed list
    public MyNode findLastIndexNode(int k) {
        if (head.next == null) {
            return null;
        }
        if (k < 0 || k > getLength()) {
            return null;
        }
        //Define fast pointer
        MyNode fast = head;
        //Define slow pointer
        MyNode slow = head;
        //Go k steps first
        for (int i = 0; i < k; i++) {
            fast = fast.next;
        }
        //The fast pointer stops the cycle when it reaches the end of the linked list
        while (fast != null) {
            fast = fast.next;
            slow = slow.next;
        }
        return slow;
    }

1.5 reverse single chain table

code:

    //Reverse linked list
    public void reverseList() {
        if (head.next == null || head.next.next == null) {
            System.out.println("No need to reverse!");
            return;
        }

        //cur represents the current node
        MyNode cur = head.next;
        //Define reverse linked list
        MyNode reverseHead = new MyNode(0);
        while (cur != null) {
            //Save the next node of the current node of the original linked list
            MyNode nextNode = cur.next;
            //Use the head insertion method to insert the nodes of the original linked list into the inverted linked list
            cur.next = reverseHead.next;
            reverseHead.next = cur;
            //Move the current node of the original linked list backward
            cur = nextNode;
        }
        head.next = reverseHead.next;
    }

one point six   Disadvantages of single linked list

Only one direction can be found.

It cannot be deleted by itself and needs to rely on auxiliary nodes.

2, Bidirectional linked list

2.1 basic introduction

Each node of a two-way linked list contains a data field, a next field (pointing to the next node) and a pre field (pointing to the previous node). Using a two-way linked list can solve the shortcomings of a single linked list.

2.2 adding and deleting data in single linked list (sorted by data number)

code:

public class DoubleLinkedListDemo {
    public static void main(String[] args) {
        MyNode myNode1 = new MyNode(10);
        MyNode myNode2 = new MyNode(30);
        MyNode myNode3 = new MyNode(20);
        MyNode myNode4 = new MyNode(40);
        MyNode myNode5 = new MyNode(10);

        DoubleLinkedList doubleLinkedList = new DoubleLinkedList();
        doubleLinkedList.add(myNode1);
        doubleLinkedList.add(myNode2);
        doubleLinkedList.add(myNode3);
        doubleLinkedList.add(myNode4);
        doubleLinkedList.add(myNode5);
        doubleLinkedList.delete(40);
        doubleLinkedList.list();
    }
}

class DoubleLinkedList {
    //Initialize header node
    private MyNode head = new MyNode(0);

    //Add node
    public void add(MyNode myNode) {
        if (head.next == null) {
            head.next = myNode;
            myNode.pre = head;
            return;
        }

        //cur represents the current node
        MyNode cur = head.next;

        while (cur != null) {
            //If the current node is at the end of the linked list, add a node at the end
            if (cur.next == null) {
                cur.next = myNode;
                myNode.pre = cur;
                break;
            }
            //Number already exists
            else if (cur.no == myNode.no) {
                System.out.println("The number of added data already exists" + myNode.no);
                return;
            }
            //Insert node after current node
            else if (cur.next.no > myNode.no) {
                //Add a node after the temp node. The next node of myNode points to the next node of temp.
                myNode.next = cur.next;
                cur.next.pre = myNode;
                //The next node of temp points to myNode.
                cur.next = myNode;
                myNode.pre = cur;
                break;
            }
            //Backward node
            cur = cur.next;
        }
    }

    //Delete node
    public void delete(int no) {
        //cur represents the current node
        MyNode cur = head.next;

        while (cur != null) {
            if (cur == null) {
                System.out.println("The data to be deleted does not exist");
            } else if (cur.no == no) {
                cur.pre.next = cur.next;
                //When the next node of the current node is not empty, the pre node is processed
                if (cur.next != null) {
                    cur.next.pre = cur.pre;
                }
                return;
            }
            cur = cur.next;
        }
    }

    //Traversal linked list
    public void list() {
        if (head.next == null) {
            System.out.println("The linked list is empty!");
            return;
        }
        //cur represents the current node
        MyNode cur = head.next;

        while (cur != null) {
            System.out.println(cur);
            cur = cur.next;
        }
    }
}

class MyNode {
    public int no;
    public MyNode next; //Point to next node
    public MyNode pre; //Point to previous node

    //constructor 
    public MyNode(int no) {
        this.no = no;
    }

    @Override
    public String toString() {
        return "MyNode{" +
                "no=" + no +
                '}';
    }
}

result:

3, Joseph Ring

3.1 basic introduction

Let the number be 1, 2,...... n, and n people sit around. It is agreed that the person with the number k (k is greater than or equal to 1 and less than or equal to n) will count off from 1, and the person who counts to m will be listed. Its next digit continues to count from 1, and those who count to m are listed, and so on until everyone is listed.

three point two   Ring linked list to solve Joseph problem

code:

public class Josephus {
    public static void main(String[] args) {
        CircleSingleLinkedList circleSingleLinkedList = new CircleSingleLinkedList();
        circleSingleLinkedList.add(5);
        circleSingleLinkedList.list();
        //Joseph Ring out
        circleSingleLinkedList.count(1, 2, 5);
    }
}

class CircleSingleLinkedList {
    //Initialize header node
    private MyNode first = new MyNode(1);

    //Add nodes to build a ring linked list
    public void add(int nums) {
        if (nums < 1) {
            System.out.println("nums The value of is incorrect!");
            return;
        }
        MyNode cur = first;

        //Building circular linked list
        for (int i = 2; i <= nums; i++) {
            MyNode myNode = new MyNode(i);
            cur.next = myNode;
            myNode.next = first;
            cur = myNode;
        }
    }

    //Joseph rings the circle, startNo is the number of starting counting, countNum is the number of counting, and num is the total number of counting
    public void count(int startNo, int countNum, int nums) {
        if (first == null || startNo < 1 || startNo > nums) {
            System.out.println("Unreasonable parameters!");
            return;
        }

        //Define a temporary pointer to point to the end node of the ring linked list
        MyNode temp = first;
        while (temp.next != first) {
            temp = temp.next;
        }

        //Move the first pointer to startNo and the temp pointer to the previous node of first
        for (int i = 1; i < startNo; i++) {
            first = first.next;
            temp = temp.next;
        }

        //Go out of the circle until there is only one person left in the circle
        while (temp != first) {
            //number off
            for (int j = 1; j < countNum; j++) {
                first = first.next;
                temp = temp.next;
            }
            System.out.println(first.no + "Out of circle");
            //Remove node
            first = first.next;
            temp.next = first;
        }
        System.out.println(first.no + "In circle");
    }

    //Traversal linked list
    public void list() {
        if (first == null) {
            System.out.println("The linked list is empty!");
            return;
        }
        MyNode cur = first;
        System.out.println(cur);

        while (cur.next != first) {
            cur = cur.next;
            System.out.println(cur);
        }
    }
}

class MyNode {
    public int no;
    public MyNode next; //Point to next node

    //constructor 
    public MyNode(int no) {
        this.no = no;
    }

    @Override
    public String toString() {
        return "MyNode{" +
                "no=" + no +
                '}';
    }
}

result:

 

Posted by azaidi on Mon, 20 Sep 2021 14:08:44 -0700