[英]Java Singly Linked Integer List, write function thats returns a reference to element with index I in the list
我一直在尝试编写Java函数IntList get(int i)
,该函数应该返回对单链接整数列表中i-th
元素的引用。 我的问题是,即使我尝试引用现有元素,该函数也会返回null
!
public class IntList {
private int info; //the int data of this list element
private IntList next; //the rest of the list
/**
* Sets up a new instance of IntList corresponding to the given info and next.
* @param info the int data of this list element
* @param next the rest of the list
*/
public IntList(int info, IntList next) {
this.info = info;
this.next = next;
}
/**
* A new list where the given info has been prepended.
* @param info the int data of the new list element
* @return a new instance of IntList
*/
/*
public IntList prepend(int info) {
return new IntList(info, this);
}
*/
/**
* A new list where the given info has been appended.
* @param info the int data of the new list element
* @return a new instance of IntList
*/
public IntList append(int info) {
if(next == null) {
return new IntList(this.info, new IntList(info, null));
} else {
return new IntList(this.info, next.append(info));
}
}
/**
* Commputes the sum of all elements of this list.
* @return the sum of all elements
*/
public int sum() {
if(next == null) {
return info;
} else {
return info + next.sum();
}
}
/**
* Auxiliary function for the reversal of this list.
* @param acc the list elements accumulated so far
* @return a new instance of IntList
*/
private IntList reverseAux(IntList acc) {
if(next == null) {
return new IntList(info, acc);
} else {
return next.reverseAux(new IntList(info, acc));
}
}
/**
* A new list with the elements of this list in reverse order.
* @return a new instance of the IntList
*/
public IntList reverse() {
return reverseAux(null);
}
/**
* String representation of this list.
*/
@Override
public String toString() {
if(next == null) {
return "" + info;
} else {
return info + " , " + next;
}
}
/**
* An integer array is converted to a list
* @param values is an array containing integer elements
* @return a new instance of IntList
*/
public static IntList fromArray(int[] values) {
int n = values.length;
IntList res = new IntList(values[0] , null);
for(int i = 1; i < n ; i ++) {
res = res.append(values[i]);
}
return res;
}
/**
* The length of a given IntList object is determined
* @return the length of the list
*/
public int length() {
int counter = 1;
while(next != null) {
counter = counter + 1;
next = next.next;
}
return counter;
}
public IntList get(int i) {
for(int k = 0 ; k < i - 1 ; k ++) {
if(next != null) {
next = next.next;
}
}
return next;
}
public static void main(String[] args) {
IntList lst = new IntList(1, null);
for(int i = 2 ; i < 10 ; i ++) {
lst = lst.append(i);
}
System.out.println(lst);
System.out.println(lst.reverse());
System.out.println(lst.sum());
int[] values = new int[4];
values[0] = 3;
values[1] = 4;
values[2] = 5;
values[3] = 8;
System.out.println(fromArray(values));
System.out.println(lst.length());
System.out.println(fromArray(values).length());
System.out.println(lst.get(2));
}
}
由于列表的实现不需要节点和列表本身两个单独的类,因此我无法在网络上找到任何有价值的信息(大多数人使用两个类)。
这一项无需检查IndexOutOfBoundsException就可以工作:
public IntList get(int i) {
IntList current = this;
for(int k = 0 ; k < i - 1 ; k ++) {
if(current.next != null) {
current = current.next;
}
}
return current;
}
接下来,您的length
方法将进行修改。 使它递归(或创建一个新变量以逐步遍历您的列表):
public int length() {
if(next != null) {
return 1 + next.length();
}
else {
return 1;
}
}
您的get
方法还会修改原始列表。 递归解决方案是:
public IntList get(int i) {
if (i < 0) {
throw new IndexOutOfBoundsException("Index is negative!");
}
if (i == 0) {
return this;
} else if (next != null) {
return next.get(i - 1);
}
throw new IndexOutOfBoundsException("Index exceeds bounds");
}
我想你想要这样的东西...
public IntList append(int info) {
if(next == null) {
return new IntList(info, this);
} else {
return new IntList(info, next);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.