请求修正LinkedElement类add/remove方法:元素覆盖与移除失效问题
Let's break down your problems and fix the code step by step, while clarifying the object reference logic you're confused about.
First: The add() Method Problem (Duplicate/Overwritten "Yo!" Elements)
Your current add() method creates two instances of Element with the same value when the list is empty, which makes it look like your "Yo!" value is being overwritten (or duplicated unexpectedly). Here's why:
- When
head == null, you setheadto a newElement(newVal), but then immediately create anotherElement(newVal)and append it to the list. - You only call
increaseListSize()once, so yourlistSizecount doesn't match the actual number of elements in the list.
Fixed add() Method
private Element<T> head; private int listSize; // Assume this member variable exists in your class public void add(T newVal) { // Create ONE new element per add call Element<T> newElement = new Element<>(newVal); if (head == null) { // List is empty: new element becomes the head head = newElement; } else { // Traverse to the last element in the list Element<T> current = head; while (current.getNext() != null) { current = current.getNext(); } // Append the new element to the end current.setNext(newElement); } increaseListSize(); }
What changed:
- We only create one
Elementperadd()call, eliminating duplicates. - Used an
elsebranch to separate empty-list and non-empty-list logic, so we don't accidentally append a second element when initializing the list.
Second: The remove() Method Not Working
Your remove() has several critical bugs that prevent it from functioning correctly:
- Invalid index check: You blocked
i == 0(the head element) withi < 1, but your comment says this should be allowed. - Dead loop: The
forloop incrementsiinstead of the loop variablee, so it never exits. - No head removal logic: You never handle the case where we need to update the
headreference. - Incorrect return value: You always return
null, even when the removal succeeds.
Fixed remove() Method
public LinkedElement<T> remove(int i) { // Validate index: must be between 0 and listSize - 1 if (i < 0 || i >= getListSize()) { return this; // Return current instance since no change was made } if (i == 0) { // Remove the head element: update head to point to the next node head = head.getNext(); decreaseListSize(); } else { // Traverse to the element BEFORE the one we want to remove Element<T> current = head; for (int e = 0; e < i - 1; e++) { current = current.getNext(); } // Skip over the target element by updating the next reference current.setNext(current.getNext().getNext()); decreaseListSize(); } // Return the current instance (its head now points to the new head of the list) return this; }
What changed:
- Fixed the index check to allow
i == 0and properly reject out-of-bounds indices. - Corrected the
forloop to incrementeinstead ofi, so we reach the correct predecessor node. - Added logic to update the
headwhen removing the first element. - Return
this(the updated list instance) instead ofnull, matching your method's documentation.
Clarifying Object Reference Logic
In Java, when you create an Element like new Element(newVal), you're creating a reference to an object stored in memory. Linked lists work by having each node's next reference point to another node object:
- The
add()bug wasn't about overwriting values—it was about creating duplicate objects and linking them incorrectly. - When you remove a node, you're not deleting the object itself; you're just breaking the reference to it (by having the previous node's
nextskip over it). The Java garbage collector will then clean up the unused node object.
Assumed Element Inner Class
For completeness, here's the inner Element class I assumed exists in your LinkedElement (since you didn't include it):
private static class Element<T> { private T value; private Element<T> next; public Element(T value) { this.value = value; this.next = null; } public Element<T> getNext() { return next; } public void setNext(Element<T> next) { this.next = next; } }
内容的提问来源于stack exchange,提问作者DameVomSee

