错误将新成员添加到链接的位置列表

发布于 2025-02-06 16:45:30 字数 6385 浏览 2 评论 0原文

我是与Java编程的新手,并实现了算法。 我正在尝试在Java中实现位置链接列表,并且我编写了以下方法,并且在此列表中添加新方法很难。 方法之间的添加是为此目的,无法正常工作。 您能告诉我,方法之间的ADDBETBET方法是怎么回事,我该如何完成?

import Position;
import EmptyListException;
import IllegalValueException;
import InvalidPositionException;

import java.util.Comparator;
import java.util.Iterator;
import java.util.Spliterator;
import java.util.function.Consumer;


public final class LinkedPositionalList<E> implements PositionalList<E> {


private class Node implements Position {
    private E element;
    private Node next;
    private Node before;


    protected Node(E element, Node next, Node before) {
        this.element = element;
        this.next = next;
        this.before = before;
    }

    public void setElement(E element) {
        this.element = element;
    }

    public E getElement() throws IllegalStateException {
        if (next == null)
            throw new IllegalArgumentException("Position no longer valid");
        return element;
    }

    public Node getBefore() {
        return before;
    }

    public void setBefore(Node before) {
        this.before = before;
    }

    public void setNext(Node next) {
        this.next = next;
    }

    public Node getNext() {
        return next;
    }

    @Override
    public Object element() throws InvalidPositionException {
        return null;
    }
}

private int size = 0;
private Node header;
private Node trailer;

public LinkedPositionalList() {
    header = new Node(null, null, null);
    trailer = new Node(null, null, header);
    header.setNext(trailer);
}

private Node validate(Position<E> p) throws IllegalArgumentException {
    Node node = (Node) p;
    if (node.getNext() == null)
        throw new IllegalArgumentException("p is no longer in the list");
    return node;
}

private Node position(Node node) {
    if (node == header || node == trailer)
        return null;
    return node;
}

@Override
public int size() {
    return size;
}

@Override
public boolean isEmpty() {
    if (size == 0) {
        return true;
    }
    return false;
}

@Override
public void clear() {
    size = 0;
    header = null;
    trailer = null;
}

@Override
public boolean contains(E e) {
    Node temp = header;
    for (int i = 0; i < size; i++) {
        if (temp.element == e) {
            return true;
        }
        temp = temp.next;
    }
    return false;
}

@Override
public Position<E> first() throws EmptyListException {
    if (isEmpty()) {
        throw new EmptyListException("List is Empty");
    }
    System.out.println(header.element);
    return (Position<E>) header.element;
}

@Override
public Position<E> last() throws EmptyListException {
    if (isEmpty()) {
        throw new EmptyListException("List is Empty");
    }
    return (Position<E>) header.getBefore();
}

@Override
public boolean isFirst(Position<E> p) throws InvalidPositionException {

    if (header.getBefore() != null) {

        return false;
    } else {
        return true;
    }
}

@Override
public boolean isLast(Position<E> p) throws InvalidPositionException {
    if (trailer.getNext() != null) {
        return false;
    } else {
        return true;
    }
}

@Override
public Position<E> before(Position<E> p) throws InvalidPositionException {
    Node node = validate(p);
    return (Position<E>) node.getBefore();
}

@Override
public Position<E> after(Position<E> p) throws InvalidPositionException {
    Node node = validate(p);
    return (Position<E>) node.getNext();
}

private Position<E> addBetween(E e, Node pred, Node succ) {
    Node newest = new Node(e, pred, succ);
    pred.setNext(newest);
    succ.setBefore(newest);
    size++;
    return (Position<E>) (newest);
}

@Override
public Position<E> insertFirst(E e) throws IllegalValueException {
    if (size > 0){
          insertBefore(header,e);
    }else {
        Node temp = new Node(e, header, null);
        header = temp;
        size++;
    }
    return (Position<E>) header;
    //        return addBetween(e, header, header.getNext());
}

@Override
public Position<E> insertLast(E e) throws IllegalValueException {

    Node temp = new Node(e, null, trailer);
    trailer = temp;
    size++;
    return (Position<E>) trailer;
    //        return addBetween(e, trailer.getBefore(), trailer);
}

@Override
public Position<E> insertBefore(Position<E> p, E e) throws InvalidPositionException, IllegalValueException {
    Node node = validate(p);
    return addBetween(e, node.getBefore(), node);
}

@Override
public Position<E> insertAfter(Position<E> p, E e) throws InvalidPositionException, IllegalValueException {
    Node node = validate(p);
    return addBetween(e, node, node.getNext());

}

@Override
public E remove(Position<E> p) throws InvalidPositionException {
    Node node = validate(p);
    Node predecessor = node.getBefore();
    Node successor = node.getNext();
    predecessor.setNext(successor);
    successor.setBefore(predecessor);
    size--;
    E answer = node.getElement();
    node.setElement(null);
    node.setBefore(null);
    node.setNext(null);
    return answer;
}

@Override
public E replaceElement(Position<E> p, E e) throws InvalidPositionException, IllegalValueException {
    Node node = validate(p);
    node.setElement(e);
    return node.getElement();
}

@Override
public void swapElements(Position<E> p, Position<E> q) throws InvalidPositionException {

}

@Override
public Iterator<Position<E>> positionalIterator() {
    return null;
}

@Override
public void sort(Comparator<E> comparator) {

}

@Override
public Position<E> find(E e) {
    int counter = 0;
    for (Node i = header; counter < size; i = i.next) {
        if (e.equals(i)) {
            return (Position<E>) e;
        }
        counter++;

        return null;
    }return null;
}

@Override
public Iterator<Position<E>> findAll(E e) {
    return null;
}

@Override
public Iterator<E> iterator() {
    return null;
}

@Override
public void forEach(Consumer<? super E> action) {
    PositionalList.super.forEach(action);
}

@Override
public Spliterator<E> spliterator() {
    return PositionalList.super.spliterator();
}

}

I'm new to programming with Java and implementing algorithms.
I am trying to implement positional linked list in Java and I wrote the following methods and have trouble adding a new method to this list.
The add between method is for this purpose and does not work properly.
Can you tell me what is wrong with the addBetween method and how can I complete it?

import Position;
import EmptyListException;
import IllegalValueException;
import InvalidPositionException;

import java.util.Comparator;
import java.util.Iterator;
import java.util.Spliterator;
import java.util.function.Consumer;


public final class LinkedPositionalList<E> implements PositionalList<E> {


private class Node implements Position {
    private E element;
    private Node next;
    private Node before;


    protected Node(E element, Node next, Node before) {
        this.element = element;
        this.next = next;
        this.before = before;
    }

    public void setElement(E element) {
        this.element = element;
    }

    public E getElement() throws IllegalStateException {
        if (next == null)
            throw new IllegalArgumentException("Position no longer valid");
        return element;
    }

    public Node getBefore() {
        return before;
    }

    public void setBefore(Node before) {
        this.before = before;
    }

    public void setNext(Node next) {
        this.next = next;
    }

    public Node getNext() {
        return next;
    }

    @Override
    public Object element() throws InvalidPositionException {
        return null;
    }
}

private int size = 0;
private Node header;
private Node trailer;

public LinkedPositionalList() {
    header = new Node(null, null, null);
    trailer = new Node(null, null, header);
    header.setNext(trailer);
}

private Node validate(Position<E> p) throws IllegalArgumentException {
    Node node = (Node) p;
    if (node.getNext() == null)
        throw new IllegalArgumentException("p is no longer in the list");
    return node;
}

private Node position(Node node) {
    if (node == header || node == trailer)
        return null;
    return node;
}

@Override
public int size() {
    return size;
}

@Override
public boolean isEmpty() {
    if (size == 0) {
        return true;
    }
    return false;
}

@Override
public void clear() {
    size = 0;
    header = null;
    trailer = null;
}

@Override
public boolean contains(E e) {
    Node temp = header;
    for (int i = 0; i < size; i++) {
        if (temp.element == e) {
            return true;
        }
        temp = temp.next;
    }
    return false;
}

@Override
public Position<E> first() throws EmptyListException {
    if (isEmpty()) {
        throw new EmptyListException("List is Empty");
    }
    System.out.println(header.element);
    return (Position<E>) header.element;
}

@Override
public Position<E> last() throws EmptyListException {
    if (isEmpty()) {
        throw new EmptyListException("List is Empty");
    }
    return (Position<E>) header.getBefore();
}

@Override
public boolean isFirst(Position<E> p) throws InvalidPositionException {

    if (header.getBefore() != null) {

        return false;
    } else {
        return true;
    }
}

@Override
public boolean isLast(Position<E> p) throws InvalidPositionException {
    if (trailer.getNext() != null) {
        return false;
    } else {
        return true;
    }
}

@Override
public Position<E> before(Position<E> p) throws InvalidPositionException {
    Node node = validate(p);
    return (Position<E>) node.getBefore();
}

@Override
public Position<E> after(Position<E> p) throws InvalidPositionException {
    Node node = validate(p);
    return (Position<E>) node.getNext();
}

private Position<E> addBetween(E e, Node pred, Node succ) {
    Node newest = new Node(e, pred, succ);
    pred.setNext(newest);
    succ.setBefore(newest);
    size++;
    return (Position<E>) (newest);
}

@Override
public Position<E> insertFirst(E e) throws IllegalValueException {
    if (size > 0){
          insertBefore(header,e);
    }else {
        Node temp = new Node(e, header, null);
        header = temp;
        size++;
    }
    return (Position<E>) header;
    //        return addBetween(e, header, header.getNext());
}

@Override
public Position<E> insertLast(E e) throws IllegalValueException {

    Node temp = new Node(e, null, trailer);
    trailer = temp;
    size++;
    return (Position<E>) trailer;
    //        return addBetween(e, trailer.getBefore(), trailer);
}

@Override
public Position<E> insertBefore(Position<E> p, E e) throws InvalidPositionException, IllegalValueException {
    Node node = validate(p);
    return addBetween(e, node.getBefore(), node);
}

@Override
public Position<E> insertAfter(Position<E> p, E e) throws InvalidPositionException, IllegalValueException {
    Node node = validate(p);
    return addBetween(e, node, node.getNext());

}

@Override
public E remove(Position<E> p) throws InvalidPositionException {
    Node node = validate(p);
    Node predecessor = node.getBefore();
    Node successor = node.getNext();
    predecessor.setNext(successor);
    successor.setBefore(predecessor);
    size--;
    E answer = node.getElement();
    node.setElement(null);
    node.setBefore(null);
    node.setNext(null);
    return answer;
}

@Override
public E replaceElement(Position<E> p, E e) throws InvalidPositionException, IllegalValueException {
    Node node = validate(p);
    node.setElement(e);
    return node.getElement();
}

@Override
public void swapElements(Position<E> p, Position<E> q) throws InvalidPositionException {

}

@Override
public Iterator<Position<E>> positionalIterator() {
    return null;
}

@Override
public void sort(Comparator<E> comparator) {

}

@Override
public Position<E> find(E e) {
    int counter = 0;
    for (Node i = header; counter < size; i = i.next) {
        if (e.equals(i)) {
            return (Position<E>) e;
        }
        counter++;

        return null;
    }return null;
}

@Override
public Iterator<Position<E>> findAll(E e) {
    return null;
}

@Override
public Iterator<E> iterator() {
    return null;
}

@Override
public void forEach(Consumer<? super E> action) {
    PositionalList.super.forEach(action);
}

@Override
public Spliterator<E> spliterator() {
    return PositionalList.super.spliterator();
}

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蹲在坟头点根烟 2025-02-13 16:45:30

没有查看整个代码,而是节点的构造函数,接下来和之前。当您构建在Addbetweed之间的新节点时,您将以辅助的方式发送pred,然后像以前一样发送,我想您打算以相反的方式进行操作。

Didn't look at the whole code but the constructor of Node recieves elment, next and before. When you construct the new node in addBetween you send pred as next and succ as before, and I assume you meant to do it the opposite way.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文