Java-Điều Technical sin và Dual-Điều

1. What is a Linked List?

2. Key Characteristics of Linked Lists:

3. Principles of Linked List Implementation:

4. How to Implement a Linked List from Scratch?

1. What is a Linked List?

-Linked technical là một cấu trúc dữ liệu trong không gian physical, non-chains, non-chains sử dụng technical non-chains để lưu trữ các phần tử. Các phần tử trong technical được sắp xếp theo thứ tự logic với help từ các node.

  • Mỗi technical bao gồm một phần tử dữ liệu và một reference đến phần tử trước hoặc sau (điều Technical Forward và Dual-Điều).

  • Ví dụ minh họa với Dual-Điều:

// Node class for Dual-Điều
public class Node {
    public Object data;
    public Node next;
    public Node prev;

    public Node(Object e) {
        this.data = e;
        next = null;
        prev = null;
    }
}
  • Node class for Single-Điều:
// Node class for Single-Điều
public class Node {
    public Object data;
    public Node next;

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

2. Key Characteristics of Linked Lists:

  • Tách biệt between Single-Điều và Dual-Điều: Single-Điều chỉ có reference đến phần tử tiếp theo, trong khi Dual-Điều có reference đến cả phần tử tiếp theo và trước.

  • Giá trị của Linked Lists: Dù có tính chất khó可达, nhưng nó cung cấp một cách hiệu quả để thêm hoặc 删除 các phần tử ở đầu mút, không cần di chuyển qua các phần tử trong giữa.

  • **Hữu ích trong các trường hợp nào?**Linked Lists thường được sử dụng trong cáccafeteria có các phần tử được thêm hoặc 删除 thường xuyên, như danh sách người dined, các list công dụng hoặc các list dữ liệu có kích thước không xác định trước.

3. Principles of Linked List Implementation:

  1. Tạo một class Node, có hai phần tử: data và reference.
  2. Tạo một class Linked List, bao gồm head, tail và kích thước.
  3. Thêm các phương thức như thêm phần tử, 删除 phần tử, tìm kiếm với index, v.v.

Ví dụ:

package mutualLink;
import java.util.Random;

public class MyList {
    private Node head;
    private Node tail;
    private int size = 0;

    public MyList() {
        head = new Node();
        tail = new Node();
        head.next = null;
        tail.prev = null;
    }

    public boolean isEmpty() {
        return head.next == null;
    }

    public Node findPre(int index) {
        Node rnode = head;
        int dex = -1;
        while (rnode.next != null) {
            if (dex == index - 1) {
                return rnode;
            }
            rnode = rnode.next;
            dex++;
        }
        return null;
    }

    public Node findThis(int index) {
        Node rnode = head;
        int dex = -1;
        while (rnode.next != null) {
            if (dex == index) {
                return rnode;
            }
            rnode = rnode.next;
            dex++;
        }
        if (dex == size - 1) {
            return rnode;
        }
        return null;
    }

    public void add(Object e) {
        Node node = new Node(e);
        Node rnode = head;
        if (this.isEmpty()) {
            rnode.next = node;
            rnode.next.prev = null;
            tail.prev = node;
            size++;
        } else {
            while (rnode.next != null) {
                rnode = rnode.next;
            }
            rnode.next = node;
            node.prev = rnode;
            tail.prev = node;
            size++;
        }
    }

    public boolean add(int index, Object e) {
        if (index < 0 || index >= size) {
            return false;
        }
        Node node = new Node(e);
        Node preNode = findPre(index);
        node.next = preNode.next;
        preNode.next.prev = node;
        preNode.next = node;
        node.prev = preNode;
        size++;
        return true;
    }

    public boolean add(int index, MyList myl) {
        if (index < 0 || index >= size) {
            return false;
        }
        Node preNode = findPre(index);
        myl.tail.prev.next = preNode.next;
        preNode.next.prev = myl.tail.prev;
        tail.prev = null;
        preNode.next = myl.head.next;
        myl.head.next.prev = preNode;
        head.next = null;
        size += myl.size;
        return true;
    }

    public Object remove(int index) {
        Object ob = this.get(index);
        if (index < 0 || index >= size) {
            return null;
        }
        if (index == size - 1) {
            Node preNode = findPre(index);
            tail.prev = preNode.prev;
            preNode.prev.next = null;
            tail.prev.next = null;
            size--;
            return ob;
        }
        Node preNode = findPre(index);
        preNode.next = preNode.next.next;
        preNode.next.prev = preNode.prev;
        size--;
        return ob;
    }

    public Node get(int index) {
        Node thisNode = findThis(index);
        return thisNode != null ? thisNode.data : null;
    }

    public int getSize() {
        return size;
    }
}

4. How to Implement a Linked List from Scratch?

  1. Tạo một class Node với data và reference.
  2. Tạo một class Linked List với head, tail và kích thước.
  3. Tích hợp các phương thức để thêm, xóa và tìm kiếm.
  4. Sử dụng các vòng lặp để di chuyển các phần tử khi cần.

Ví dụ đoạn code:

import java.util.Random;

public class manage {
    public static void main(String[] args) {
        String name = "";
        int credit;
        int age;
        int size;
        MyList myl = new MyList();
        Random random = new Random();
        size = random.nextInt(5) + 1;
        for (int i = 0; i < size; i++) {
            credit = random.nextInt(5);
            age = random.nextInt(5) + 18;
            for (int j = 0; j < 4; j++) {
                name += (char) (random.nextInt(26) + 97);
            }
            Students stu = new Students(name, credit, age);
            myl.add(stu);
            name = "";
        }
        System.out.println("Size of myl1 is " + myl.getSize());
        for (int i = 0; i < myl.getSize(); i++) {
            Students stu2 = (Students) myl.get(i);
            stu2.show();
        }
        myl.add(1, myl2);
        System.out.println("Size is of myl1 " + myl.getSize());
        for (int i = 0; i < myl.getSize(); i++) {
            Students stu2 = (Students) myl.get(i);
            stu2.show();
        }
    }
}

Các phần tử được thêm vào list theo cách dễ dàng, không須 di chuyển các phần tử trong list.

Và đây là một đoạn code để thêm một list vào list khác:

public boolean add(int index, MyList myl) {
    if (index < 0 || index >= size) {
        return false;
    }
    Node preNode = findPre(index);
    myl.tail.prev.next = preNode.next;
    preNode.next.prev = myl.tail.prev;
    tail.prev = null;
    preNode.next = myl.head.next;
    myl.head.next.prev = preNode;
    head.next = null;
    size += myl.size;
    return true;
}

Kết thúc là một đối tượng list mới đã được thêm vào list gốc.

Và đây là một đoạn code để xóa một phần tử từ list:

public Node remove(int index) {
    Node thisNode = get(index);
    if (index == size - 1) {
        Node preNode = findPre(index);
        tail.prev = preNode.prev;
        preNode.prev.next = null;
        tail.prev.next = null;
        size--;
        return ob;
    }
    Node preNode = findPre(index);
    preNode.next = preNode.next.next;
    preNode.next.prev = preNode.prev;
    size--;
    return ob;
}

Các phần tử có thể được xóa dễ dàng mà không cần di chuyển các phần tử khác.

Thẻ: Java Programming technology

Đăng vào ngày 26 tháng 6 lúc 09:43