Home > Java > javaTutorial > body text

How to implement a custom LinkedList class in Java

王林
Release: 2023-05-17 08:31:05
forward
987 people have browsed it

1. LinkedList and ArrayList

-- LinkedList ArrayList
Contact They are all implementation classes of List, all under the java.util package
Implementation principle Operation data through linked lists Manipulate data through arrays
When to use Change and check Add and delete

2. Customize the LinkedList class (one-way linked list)

1. Implementation ideas

The LinkedList class is different from the ArrayList class. It adds, deletes, modifies and checks the linked list through the operation of pointers and nodes

Steps to customize the LinkedList class

1. Create a node class whose attributes are node nodes of Node type and data of Object type

2. Create a node class Parameterized and parameterless construction methods

3. Create your own LinkedList class to implement the List interface

4. Create a new node object in the LinkedList class and declare a size to represent the collection. Element

5. Implement the methods of size(), get(), and isEmpty() (similar to ArrayList)

6. Write an add() method with formal parameters

7. Write the add() method with two formal parameters

8. Create a test class to test the code

2. Node node class

There are two Attributes: Node and data

The type of the node is Node

The type of the data is Object (because it is not possible to determine the specific type of the incoming data)

How to implement a custom LinkedList class in Java

package MyLinkedList;

public class Node {
    // 定义数据
    Object data;
    // 定义下一结点
    Node next;

    public Node(Object data, Node next) {
        this.data = data;
        this.next = next;
    }

    public Node() {
    }
}
Copy after login

3. size(), isEmpty(), get(int index)

size() method

@Override
    public int size() {
        return size;
    }
Copy after login

isEmpty() method

 @Override
    public boolean isEmpty() {
        return size == 0;
    }
Copy after login

get(int index) method

 @Override
    public Object get(int index) {
        Node p = node;
        for (int i = 0; i < index; i++) {
            p = p.next;
        }

        return p.data;
    }
Copy after login

4, add(Object o)

Insert the received data at the end

@Override
    public boolean add(Object o) {
        add(size, o);
        return true;
    }
Copy after login

5, add(int index,Object element)

How to implement a custom LinkedList class in Java

 @Override
    public void add(int index, Object element) {
        // 找到需要插入的位置的结点
        Node p = node;
        for (int i = 0; i < index; i++) {
            p = p.next;
        }
        // 创建新结点
        Node node1 = new Node();

        // 将数据存入集合中
        node1.data = element;

        // 让node1的指针指向下一结点
        node1.next = p.next;

        // 确定node1的直接前驱结点
        p.next = node1;

        // 更新size
        size++;
    }
Copy after login

6, test class

The reason why the loop starts from 1: because an object is created at the beginning in the LinkedList class , if you start from 0, you will see that the printed result is null

package MyLinkedList;

public class test {
    public static void main(String[] args) {
        LinkedList linkedList = new LinkedList();
        linkedList.add("111");
        linkedList.add("222");
        linkedList.add("333");
        linkedList.add("444");
        for (int i = 1; i < linkedList.size(); i++) {
            System.out.println(linkedList.get(i));
        }
        System.out.println("==================");
        linkedList.add(2,999);
        for (int i = 1; i < linkedList.size(); i++) {
            System.out.println(linkedList.get(i));
        }

    }
}
Copy after login

The above is the detailed content of How to implement a custom LinkedList class in Java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template