[Data Structure] that can take you off into the king Part 2: Linked List of Data Structures 1

content

Foreword:

1. What is a linked list

1. The concept of linked list

2. The structure of the linked list

How linked lists store data

3. Implementation of linked list  

Exhaustive method to create linked list

print linked list

Find out if the keyword key is in the singly linked list 

Get the length of a singly linked list:

 head plug

tail insertion

Insert at any position, the first data node is subscript 0

Delete the node whose first occurrence of the keyword is key

 delete all nodes whose value is key

Summarize:

I am with you.


Foreword :

Problems and Thoughts on the Sequence List

1. Insertion and deletion in the middle/head of the sequence table, the time complexity is O(N)

2. To increase capacity, you need to apply for new space, copy data, and release old space. There will be some consumption.

3. The capacity expansion is generally a 2-fold increase, and there is bound to be a certain amount of space waste. For example, the current capacity is 100, and the capacity is increased to 200 when it is full. We continue to insert 5 data, and no data is inserted later, so 95 data spaces are wasted.

Thinking: How to solve the above problems? The structure of the linked list is given below.

1. What is a linked list

1. The concept of linked list

A linked list is a non-contiguous storage structure on a physical storage structure, and the logical order of data elements is realized through the reference link order in the linked list.

2. The structure of the linked list

There are 8 types of linked list structures:

 

Here we only talk about the bottom two, because these two linked lists are used in work, business, exam questions, brushed linked list questions, and interview questions. 

How linked lists store data

A linked list is made up of nodes. (Here we take a singly linked list as an example)

What is a node?

Nodes are divided into two domains, assuming one is called the val domain and the other is called the next domain.

val: data field

next: the address of the next node

3. Implementation of linked list  

//ListNode代表一个节点

class ListNode{
    public int val;
    public ListNode next;

    //构造方法
    public ListNode(int val){
        this.val = val;
    }
}
//MyLinkedList 代表这是一个链表

public class MyLinkedList {
    public ListNode head;//链表的头引用,所以定义在链表里,head是链表的头,不是节点的头,节点只有两个属性,一个属性是val值,一个属性是next值,所以不能定义在ListNode类里面
    ListNode listNode = new ListNode(2);//节点实例化,val域赋值为2
}

Exhaustive method to create linked list

/MyLinkedList 代表这是一个链表
public class MyLinkedList {
    public ListNode head;//链表的头引用,所以定义在链表里
    public  void createList(){
        ListNode listNode0 = new ListNode(11);
        ListNode listNode1 = new ListNode(26);
        ListNode listNode2 = new ListNode(23);
        ListNode listNode3 = new ListNode(45);
        ListNode listNode4 = new ListNode(56);
        listNode0.next = listNode1;
        listNode1.next = listNode2;
        listNode2.next = listNode3;
        listNode3.next = listNode4;
        this.head = listNode0;
    }

print linked list

//打印链表
    public  void display(){
         ListNode cur = this.head;
         while (cur != null){
             System.out.print(cur.val+" ");
             cur = cur.next;
         }
        System.out.println();
    }

 print result:

Find out if the keyword key is in the singly linked list 

 //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key) {
        ListNode cur = this.head;
        while (cur != null) {
            if (cur.val == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

print result:

Get the length of a singly linked list:

    //得到单链表的长度
    public int size(){
        ListNode cur = this.head;
        int count = 0;
        while(cur != null){
            count++;
            cur = cur.next;
        }
        return count;
    }

 print result:

 head plug

 //头插法

    public void addFirst(int data) {
        ListNode node = new ListNode(data);
        if (this.head == null) {
            this.head = node;
        } else {
            node.next = this.head;
            head = node;
        }
    }

print result:

tail insertion

//尾插法

    public void addLast(int data){
        ListNode node = new ListNode(data);
        ListNode cur = this.head;
        if(this.head == null){
            this.head = node;
        }else {
            while(cur.next != null){
                cur = cur.next;
            }
            cur.next = node;

        }
    }

print result:

Insert at any position, the first data node is subscript 0

public ListNode findIndex(int index){
        ListNode cur = this.head;
        while(index -1 != 0){
            cur = cur.next;
            index--;
        }
        return cur;

    }
    //任意位置插入,第一个数据节点为0号下标
    public void addIndex(int index,int data){
        if(index < 0 || index > size()){
            System.out.println("位置不合法");
            return;
        }
            if(index == 0){
                addFirst(data);
                return;
            }
            if(index == size()){
                addLast(data);
                return;
            }

            ListNode cur = findIndex(index);
            ListNode node = new ListNode(data);
            node.next = cur.next;
            cur.next = node;

    }

print result:

Delete the node whose first occurrence of the keyword is key

//删除第一次出现关键字为key的节点
    public void remove(int key){
        if(this.head == null){
            System.out.println("没有你要删除的节");
            return;
        }
       if (this.head.val == key){
           this.head = this.head.next;
           return;
        }
       ListNode cur = this.head;
       while (cur.next != null){
           if(cur.next.val == key){
               cur.next = cur.next.next;
               return;
           }
           cur = cur.next;
       }
       if(cur.next == null){
           System.out.println("没有该节点");
           return;
       }

    }

 print result:

 delete all nodes whose value is key

    //删除所有值为key的节点
    public ListNode removeAllKey(int key){
        if(this.head == null) return null;
        ListNode prev = this.head;
        ListNode cur = this.head;
        while (cur != null){
            if(cur.val == key){
                prev.next = cur.next;
                cur = cur.next;
            }else{
                    prev = cur;
                    cur = cur.next;
            }
        }
        if(this.head.val == key){
            this.head = this.head.next;
        }
        return this.head;

    }

print result:

Summary :

This article briefly introduces the linked list of data structures, how to create a linked list, and how to operate data on the linked list. Deepen the understanding of the sequence table by means of simple examples. The above is the content of today. If you have any questions, you can privately message me at any time. I will actively correct any problems in the article. I also hope that everyone can master the knowledge they want faster, let’s work together! ! ! ! !

I am with you . _

Guess you like

Origin blog.csdn.net/m0_64397675/article/details/123401757