Injecting a circular linked list in java

I had a problem implementing my circular list. I am working on a problem that requires you to implement any ADT yourself. It seems to me that everything is in order with the addition of nodes to the list, however, I am in unfamiliar territory when it comes to deletion. I have included the first two removal methods to give you an idea of ​​where my head is, how can I delete the last node in the list?

public class LinkedList {
    private Node head;
    private Node tail;
    private int size = 0;
    LinkedList() {
        head = null;
        current = null;
        previous = null;
        tail = null;
        size = 0;
    }

    //checks if list is empty
    public boolean isEmpty() {
        return head == null;
    }
    //add new node to front of circularly linked list
    public void addToFront(E x) {
        if (head == null) {
            head = new Node(x);
        } else {
            Node n = new Node(x);
            x.next() = head;
            head = x;
        }
    }

    public void addtoMiddle(E x) {
        x.next = current.next();
        current.next = x;
        size = size + 1;
    }

    public void addToEnd(E x) {
        x.next = null;
        tail.next() = x;
        tail = x;
        size = size + 1;
    }

    public void removeFirst(E x) {
        if (head = null) {
            System.out.println("Error! List is empty!");
        } else {
            head = current.next();
            size = size + 1;
        }
    }

    public void removeMiddle(E x) {
        previous.next() = current.next();
        current.next() = null;
        size = size + 1;
    }
+4
source share
2 answers

node next , node.next.equals( head ). , next , node, head.next = head.

previous node, . node - head.previous.

ascii:

head -next---> node -next---> node -next---> last
 | ^  <---prev-      <---prev-      <---prev- | ^
 | |                                          | |
 | |_____________________________________next_| |
 |_prev_________________________________________|      
+3

, , , Thomas .

, . , .

:

, , . current, previous.

, next .

Node ( ), LinkedList.

0

Source: https://habr.com/ru/post/1609638/


All Articles