如何在java中实现循环双链接列表添加方法

如何在java中实现循环双链接列表添加方法,java,nodes,doubly-linked-list,Java,Nodes,Doubly Linked List,我在循环双链接列表类和节点内部类中实现add(E)方法 节点应实现为私有内部类 DoublyLinkedList的“first”属性应指向列表中的第一个节点。它的“size”属性应该存储列表中元素的数量 我正在努力使用add方法,因为我觉得没有什么不对的地方,我不知道我还能添加哪些代码来修复它 因此,简单介绍add方法应该做什么 add(E)方法应将value参数添加到列表的末尾。确保解决列表为空和/或添加的元素是列表中第一个元素的情况 这是我的密码: public class DoublyLi

我在循环双链接列表类和节点内部类中实现add(E)方法

节点应实现为私有内部类

DoublyLinkedList的“first”属性应指向列表中的第一个节点。它的“size”属性应该存储列表中元素的数量

我正在努力使用add方法,因为我觉得没有什么不对的地方,我不知道我还能添加哪些代码来修复它

因此,简单介绍add方法应该做什么

add(E)方法应将value参数添加到列表的末尾。确保解决列表为空和/或添加的元素是列表中第一个元素的情况

这是我的密码:

public class DoublyLinkedList<E>
{
private Node first;
private int size;

@SuppressWarnings("unchecked")
public void add(E value)
{
    if(first == null)
    {
        first = new Node(value, null, null);
        first.next = first;
        first.prev = first;
    }
    else
    {
        first = new Node(value, first.next, first.prev);
        first.next = first.prev;
        first = first.next;
        first.prev = first;
    }
    size++;
}
private class Node<E>
{
    private E data;
    private Node next;
    private Node prev;

    public Node(E data, Node next, Node prev)
    {
        this.data = data;
        this.next = next;
        this.prev = prev;
    }
  }
 }
公共类双链接列表
{
私有节点优先;
私有整数大小;
@抑制警告(“未选中”)
公共无效添加(E值)
{
if(first==null)
{
第一个=新节点(值,null,null);
first.next=first;
first.prev=first;
}
其他的
{
first=新节点(值,first.next,first.prev);
first.next=first.prev;
first=first.next;
first.prev=first;
}
大小++;
}
私有类节点
{
私人电子数据;
私有节点下一步;
私有节点prev;
公共节点(E数据、节点下一个、节点上一个)
{
这个数据=数据;
this.next=next;
this.prev=prev;
}
}
}
编辑:

这里有一个错误:不是此行,而是this.next=prev;它应该有这个。prev=prev

但是,如果您修复了这一行,代码仍然无法工作。这是代码的简化版本,可以正常工作

public class DoublyLinkedList<E> {

    private static class Node<E> {

        private final E data;
        private Node<E> next;
        private Node<E> prev;

        Node(E data) {
            this.data = data;
            this.next = this;
            this.prev = this;
        }

        Node(E data, Node<E> next) {
            this.data = data;
            this.next = next;
            next.prev = this;
        }
    }

    private Node<E> head;
    private Node<E> tail;
    private int size;

    public void add(E value) {
        if (this.head == null) {
            this.head = new Node<>(value);
            this.tail = head;
        } else {
            this.head = new Node<>(value, head);
            this.head.prev = this.tail;
            this.tail.next = head;
        }
        size++;
    }

    public void forEach(Consumer<E> consumer) {
        Node<E> node = this.head;

        if (node != null) {
            do {
                consumer.accept(node.data);
                node = node.next;
            } while (node != this.head);
        }
    }

    public static void main(String[] args) {
        DoublyLinkedList<Integer> list = new DoublyLinkedList<>();

        list.add(1);
        list.add(2);
        list.add(3);

        list.forEach(e -> System.out.print(e + ", "));
    }

}
公共类双链接列表{
私有静态类节点{
私人最终电子数据;
私有节点下一步;
私有节点prev;
节点(E数据){
这个数据=数据;
this.next=这个;
this.prev=这个;
}
节点(E数据,节点下一个){
这个数据=数据;
this.next=next;
next.prev=这个;
}
}
专用节点头;
私有节点尾部;
私有整数大小;
公共无效添加(E值){
if(this.head==null){
this.head=新节点(值);
这个尾巴=头;
}否则{
this.head=新节点(值,head);
this.head.prev=this.tail;
this.tail.next=头部;
}
大小++;
}
公共无效forEach(消费者){
Node=this.head;
如果(节点!=null){
做{
consumer.accept(node.data);
node=node.next;
}while(node!=this.head);
}
}
公共静态void main(字符串[]args){
DoublyLinkedList=新的DoublyLinkedList();
增加第(1)款;
增加(2);
增加(3);
list.forEach(e->System.out.print(e+“,”);
}
}
我所做的:为了创建一个循环的双链接列表,我保留了对尾部的引用。

编辑:

这里有一个错误:不是此行,而是this.next=prev;它应该有这个。prev=prev

但是,如果您修复了这一行,代码仍然无法工作。这是代码的简化版本,可以正常工作

public class DoublyLinkedList<E> {

    private static class Node<E> {

        private final E data;
        private Node<E> next;
        private Node<E> prev;

        Node(E data) {
            this.data = data;
            this.next = this;
            this.prev = this;
        }

        Node(E data, Node<E> next) {
            this.data = data;
            this.next = next;
            next.prev = this;
        }
    }

    private Node<E> head;
    private Node<E> tail;
    private int size;

    public void add(E value) {
        if (this.head == null) {
            this.head = new Node<>(value);
            this.tail = head;
        } else {
            this.head = new Node<>(value, head);
            this.head.prev = this.tail;
            this.tail.next = head;
        }
        size++;
    }

    public void forEach(Consumer<E> consumer) {
        Node<E> node = this.head;

        if (node != null) {
            do {
                consumer.accept(node.data);
                node = node.next;
            } while (node != this.head);
        }
    }

    public static void main(String[] args) {
        DoublyLinkedList<Integer> list = new DoublyLinkedList<>();

        list.add(1);
        list.add(2);
        list.add(3);

        list.forEach(e -> System.out.print(e + ", "));
    }

}
公共类双链接列表{
私有静态类节点{
私人最终电子数据;
私有节点下一步;
私有节点prev;
节点(E数据){
这个数据=数据;
this.next=这个;
this.prev=这个;
}
节点(E数据,节点下一个){
这个数据=数据;
this.next=next;
next.prev=这个;
}
}
专用节点头;
私有节点尾部;
私有整数大小;
公共无效添加(E值){
if(this.head==null){
this.head=新节点(值);
这个尾巴=头;
}否则{
this.head=新节点(值,head);
this.head.prev=this.tail;
this.tail.next=头部;
}
大小++;
}
公共无效forEach(消费者){
Node=this.head;
如果(节点!=null){
做{
consumer.accept(node.data);
node=node.next;
}while(node!=this.head);
}
}
公共静态void main(字符串[]args){
DoublyLinkedList=新的DoublyLinkedList();
增加第(1)款;
增加(2);
增加(3);
list.forEach(e->System.out.print(e+“,”);
}
}

我所做的:为了创建一个循环的双链接列表,我保留了一个对尾部的引用。

下面是一个示例实现-

import java.util.Iterator;

public class DoublyLinkedList<E> {

    // A reference to the root node
    private Node<E> root = null;

    // attribute for storing the size of the linked list
    private int countOfNodes = 0;

    // attribute that indicates if the linked list would be iterated in a cycle
    private boolean isCircular = false;

    /**
     * The linked list Node class
     * 
     * @param <E>
     */

    @SuppressWarnings("hiding")
    class Node<E> {
        // attribute for storing the value
        private E data;

        // attributes for storing the linked list references
        private Node<E> previousNode = null;
        private Node<E> nextNode = null;

        public Node() {
            super();
        }

        public Node(E data) {
            super();
            this.data = data;
        }

        public Node<E> getPreviousNode() {
            return previousNode;
        }

        public void setPreviousNode(Node<E> previousNode) {
            this.previousNode = previousNode;
        }

        public Node<E> getNextNode() {
            return nextNode;
        }

        public void setNextNode(Node<E> nextNode) {
            this.nextNode = nextNode;
        }

        public E getData() {
            return data;
        }

        public void setData(E data) {
            this.data = data;
        }
    }

    /**
     * The iterator implementation
     */
    @SuppressWarnings("hiding")
    class DoublyLinkedListIterator<E> implements Iterator<E> {
        private Node<E> currentNode = null;

        public DoublyLinkedListIterator(Node<E> startNode) {
            super();
            this.currentNode = startNode;
        }

        @Override
        public boolean hasNext() {
            return (this.currentNode != null);
        }

        @SuppressWarnings("unchecked")
        @Override
        public E next() {
            E data = currentNode.getData();
            currentNode = currentNode.getNextNode();

            if (currentNode == null && DoublyLinkedList.this.isCircular()) {
                this.currentNode = (Node<E>) DoublyLinkedList.this.getRoot();
            }

            return data;
        }
    }

    public DoublyLinkedList() {
        super();
    }

    public boolean isCircular() {
        return this.isCircular;
    }

    public void setCircular(boolean isCircular) {
        this.isCircular = isCircular;
    }

    public Node<E> getRoot() {
        return root;
    }

    public void setRoot(Node<E> root) {
        this.root = root;
    }

    public int size() {
        return this.countOfNodes;
    }

    public Iterator<E> iterator() {
        return new DoublyLinkedListIterator<>(this.getRoot());
    }

    public void add(E value) {
        Node<E> node = new Node<E>(value);
        addAfter(getLastNode(), node);
        this.countOfNodes++;
    }

    public Node<E> getLastNode() {  
        Node<E> lastNode = null;

        Node<E> node = getRoot();
        while (node != null) {
            lastNode = node;
            node = node.getNextNode();
        }

        return lastNode;
    }

    public void addAfter(Node<E> parentNode, E value) {
        Node<E> newNode = new Node<E>(value);
        addAfter(parentNode, newNode);
    }

    public void addAfter(Node<E> parentNode, Node<E> newNode) {
        if (parentNode == null) {
            this.setRoot(newNode);
        } else {
            parentNode.setNextNode(newNode);
        }
    }

    public void addBefore(Node<E> parentNode, E value) {
        Node<E> newNode = new Node<E>(value);
        addBefore(parentNode, newNode);
    }

    public void addBefore(Node<E> parentNode, Node<E> newNode) {
        if (parentNode == null) {
            this.setRoot(newNode);
        } else {
            Node<E> prevNode = parentNode.getPreviousNode();
            parentNode.setPreviousNode(newNode);
            newNode.setNextNode(parentNode);            
            newNode.setPreviousNode(prevNode);

            if (newNode.getPreviousNode() == null) {
                this.setRoot(newNode);
            }
        }
    }
}
import java.util.Iterator;
公共类双链接列表{
//对根节点的引用
私有节点根=null;
//用于存储链接列表大小的属性
私有int countOfNodes=0;
//属性,指示是否在循环中迭代链表
私有布尔isCircular=false;
/**
*链表节点类
* 
*@param
*/
@抑制警告(“隐藏”)
类节点{
//用于存储值的属性
私人电子数据;
//用于存储链接列表引用的属性
私有节点previousNode=null;
私有节点nextNode=null;
公共节点(){
超级();
}
公共节点(E数据){
超级();
这个数据=数据;
}
公共节点getPreviousNode(){
返回上一个节点;
}
public void setPreviousNode(节点previousNode){
this.previousNode=previousNode;
}
公共节点getNextNode(){
返回下一个节点;
}
public void setNextNode(节点nextNode){
this.nextNode=nextNode;
}
酒吧
class DoublyLinkedList<E>
{
    private Node first;
    private int size;

    public void add(E value)
    {
        if(first == null)
        {
            first = new Node(value, null, null);
            first.next = first;
            first.prev = first;
        }
        else
        {
            first.prev.next = new Node(value, first, first.prev);
            first.prev = first.prev.next;
        }
        size++;
    }

    private class Node<E>
    {
        private E data;
        private Node next;
        private Node prev;

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