Java 实施';设置';链接列表的方法?
我正在尝试实现set方法,在该方法中,您传入所需的链接列表中的位置,然后值和set函数将该值添加到链接列表中指定的位置。我已经实现了set函数,但由于某种原因,最后一个元素在我的实现中消失了。我将非常感谢任何帮助。提前谢谢。我希望任何能看到我所缺少的东西的专家眼睛Java 实施';设置';链接列表的方法?,java,linked-list,implementation,singly-linked-list,Java,Linked List,Implementation,Singly Linked List,我正在尝试实现set方法,在该方法中,您传入所需的链接列表中的位置,然后值和set函数将该值添加到链接列表中指定的位置。我已经实现了set函数,但由于某种原因,最后一个元素在我的实现中消失了。我将非常感谢任何帮助。提前谢谢。我希望任何能看到我所缺少的东西的专家眼睛 /** * A basic singly linked list implementation. */ public class SinglyLinkedList<E> implements Cloneable, I
/**
* A basic singly linked list implementation.
*/
public class SinglyLinkedList<E> implements Cloneable, Iterable<E>, List<E> {
//---------------- nested Node class ----------------
/**
* Node of a singly linked list, which stores a reference to its
* element and to the subsequent node in the list (or null if this
* is the last node).
*/
private static class Node<E> {
E value;
Node<E> next;
public Node(E e)
{
value = e;
next = null;
}
}
//----------- end of nested Node class -----------
// instance variables of the SinglyLinkedList
private Node<E> head = null; // head node of the list (or null if empty)
private int size = 0; // number of nodes in the list
public SinglyLinkedList() {
} // constructs an initially empty list
// access methods
/**
* Returns the number of elements in the linked list.
*
* @return number of elements in the linked list
*/
public int size() {
return size;
}
/**
* Adds an element to the end of the list.
*
* @param e the new element to add
*/
public void addLast(E e) {
// TODO
}
/**
* Tests whether the linked list is empty.
*
* @return true if the linked list is empty, false otherwise
*/
public boolean isEmpty() {
return size == 0;
}
@Override
public E get(int i) throws IndexOutOfBoundsException {
Node<E> a = head;
if(i<=this.size()) {
int count = 0;
while(count < i) {
count ++;
a = a.next;
}
return a.value;
}
return null;
}
@Override
public E set(int i, E e) throws IndexOutOfBoundsException {
Node<E> current = head;
Node<E> setNode = new Node<E>(e);
if(i==0) {
this.addFirst(e);
}
else if(i==this.size){
this.addLast(e);
}
else {
for(int j=0; current != null && j < (i-1);j++) {
current = current.next;
}
Node<E> temp = current.next;
current.next = setNode;
setNode.next = temp;
}
return setNode.value;
}
// update methods
/**
* Adds an element to the front of the list.
*
* @param e the new element to add
*/
public void addFirst(E e) {
Node<E> first = new Node<>(e);
first.next = this.head;
this.head = first;
this.size++;
}
@SuppressWarnings({"unchecked"})
public boolean equals(Object o) {
// TODO
return false; // if we reach this, everything matched successfully
}
@SuppressWarnings({"unchecked"})
public SinglyLinkedList<E> clone() throws CloneNotSupportedException {
// TODO
return null;
}
/**
* Produces a string representation of the contents of the list.
* This exists for debugging purposes only.
* @return
*/
public String toString() {
for(int i=0;i<this.size();i++) {
System.out.println(this.get(i));
}
return "end of Linked List";
}
public static void main(String[] args) {
SinglyLinkedList <Integer> ll =new SinglyLinkedList <Integer>();
ll.addFirst(5);
ll.addFirst(4);
ll.addFirst(3);
ll.addFirst(2);
ll.set(1,0);
System.out.println(ll);
}
}
/**
*一个基本的单链表实现。
*/
公共类SingleyLinkedList实现可克隆、可编辑、列表{
//----------------嵌套节点类----------------
/**
*单链表的节点,存储对其
*元素和列表中的后续节点(如果
*是最后一个节点)。
*/
私有静态类节点{
E值;
节点下一步;
公共节点(E)
{
值=e;
next=null;
}
}
//-----------嵌套节点类的结尾-----------
//SingleLinkedList的实例变量
private Node head=null;//列表的head节点(如果为空,则为null)
private int size=0;//列表中的节点数
公共单链接列表(){
}//构造一个初始为空的列表
//访问方法
/**
*返回链表中的元素数。
*
*@返回链接列表中的元素数
*/
公共整数大小(){
返回大小;
}
/**
*将元素添加到列表的末尾。
*
*@param e要添加的新元素
*/
公共无效地址(E){
//待办事项
}
/**
*测试链接列表是否为空。
*
*@如果链表为空,则返回true,否则返回false
*/
公共布尔值为空(){
返回大小==0;
}
@凌驾
public E get(int i)抛出IndexOutOfBoundsException{
节点a=头部;
if(iAsumptions)
代码中缺少addLast
方法
错误也可能存在
已知原因
此代码不会增加集合
方法的for
循环中的大小
。大小
在addLast
(可能)和addFirst
中增加,在其他情况下不增加(finalelse
部分)目前尚不清楚如何使用set
方法。它现在的实现方式更像insert
,这意味着它将添加新节点,但不会增加元素总数(size
)
如果set
方法正在更改节点的值(名称表示),则此部分错误:
else {
for(int j=0; current != null && j < (i-1);j++) {
current = current.next;
}
Node<E> temp = current.next;
current.next = setNode;
setNode.next = temp;
}
else{
对于(int j=0;当前!=null&&j<(i-1);j++){
当前=当前。下一步;
}
节点温度=当前。下一步;
current.next=setNode;
setNode.next=temp;
}
它应该替换该值,而不是添加新值:
else {
for(int j=0; current != null && j < (i-1);j++) {
current = current.next;
}
current.value=e
}
else{
对于(int j=0;当前!=null&&j<(i-1);j++){
当前=当前。下一步;
}
当前值=e
}
另外,它看起来像索引i
是基于1的,而其他一切都是基于0的。我没有检查上面的代码,但概念应该如图所示。您可以添加addLast
实现吗?它在上面的代码中丢失了。另外,您没有更新set
方法for
loopYes,tur我们发现问题是尺寸变大了。非常感谢。