Java 带有添加和删除方法的链表
获得了创建链接元素的类的任务。 方法get()、add()和remove()是由赋值预定义的 实际上,我成功地编写了创建这种链表的代码, 但是除了值为“Yo!”的实例被覆盖之外,当 向列表中添加新元素,而且我无法使remove方法工作。 我真的不能用这种方式来处理引用对象。 你能帮我更正代码吗Java 带有添加和删除方法的链表,java,linked-list,Java,Linked List,获得了创建链接元素的类的任务。 方法get()、add()和remove()是由赋值预定义的 实际上,我成功地编写了创建这种链表的代码, 但是除了值为“Yo!”的实例被覆盖之外,当 向列表中添加新元素,而且我无法使remove方法工作。 我真的不能用这种方式来处理引用对象。 你能帮我更正代码吗 /** * * Represents a linked list of elements. * * @param <T> */ class LinkedElement<T&g
/**
*
* Represents a linked list of elements.
*
* @param <T>
*/
class LinkedElement<T> {
/**
* Adds a new linked element holding the given value at the end of the linked
* elements.
*
* @param newVal the new value.
*/
public void add(T newVal) {
if (head == null) {
head = new Element(newVal);
}
Element next = new Element(newVal);
Element current = head;
if (current != null) {
while (current.getNext() != null) {
current = current.getNext();
}
current.setNext(next);
}
increaseListSize();
}
/**
* Removes the i-th element from the linked elements. If {@code i == 0}, this
* will effectively remove the head element. Thus, this method returns the
* linked element that is the new head element.
*
* @param i index of the element to remove.
* @return the new head element.
*/
public LinkedElement<T> remove(int i) {
if (i < 1 || i > getListSize())
return null;
Element current = head;
if (head != null) {
for (int e = 0; e < i; i++) {
if (current.getNext() == null)
return null;
current = current.getNext();
}
current.setNext(current.getNext().getNext());
decreaseListSize();
}
return null;
}
/**
*
*表示元素的链接列表。
*
*@param
*/
类链接删除{
/**
*添加一个新的链接元素,该元素在链接的末尾保留给定的值
*元素。
*
*@param newVal指定新值。
*/
公共无效添加(T newVal){
if(head==null){
head=新元素(newVal);
}
下一个元素=新元素(newVal);
元件电流=水头;
如果(当前!=null){
while(current.getNext()!=null){
current=current.getNext();
}
当前设置下一步(next);
}
增加列表大小();
}
/**
*从链接元素中删除第i个元素。如果{@code i==0},则此
*将有效地删除head元素。因此,此方法返回
*链接元素,该元素是新的头元素。
*
*@param i要删除的元素的索引。
*@返回新的head元素。
*/
公共LinkedElement删除(int i){
if(i<1 | | i>getListSize())
返回null;
元件电流=水头;
if(head!=null){
对于(int e=0;e
您的get
方法稍有错误,您需要使用head
而不是head.next()启动current
publictget(inti){
if(i<0)
返回null;
元件电流=水头;
如果(当前!=null){
对于(int e=0;e
您的get方法中有一个错误:元素current=head;
正确
在您的删除方法中,有几项不起作用。此项应该起作用:
public void remove(int i) {
if (i==0) {
head = head.getNext();
decreaseListSize();
return;
}
if (i < 1 || i > getListSize()) {
return;
}
Element current = head;
if (head != null) {
for (int e = 1; e < i; e++) {
if (current.getNext() == null)
return ;
current = current.getNext();
}
current.setNext(current.getNext().getNext());
decreaseListSize();
}
}
public void remove(inti){
如果(i==0){
head=head.getNext();
decreaseListSize();
返回;
}
if(i<1 | | i>getListSize()){
返回;
}
元件电流=水头;
if(head!=null){
对于(int e=1;e
请注意,我将返回类型更改为void,因为您的方法在任何情况下都返回null,并且不需要返回head。如果您想要返回head元素,您可以轻松地调整它并返回head,而不是什么都不返回。
此外,需要注意的是,计数(在计算机科学中经常是这样)从0开始。要删除第一个元素,必须编写headElement.remove(0);
Oh没有看到这一点。现在它看起来像add()-方法已经很好地工作了。:)
public void remove(int i) {
if (i==0) {
head = head.getNext();
decreaseListSize();
return;
}
if (i < 1 || i > getListSize()) {
return;
}
Element current = head;
if (head != null) {
for (int e = 1; e < i; e++) {
if (current.getNext() == null)
return ;
current = current.getNext();
}
current.setNext(current.getNext().getNext());
decreaseListSize();
}
}