Я пытался написать функцию 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));
}
}
Поскольку моя реализация списка не требует двух отдельных классов для узлов и самого списка, яне может найти какую-либо ценную информацию в Интернете (большинство людей используют два класса).