1. Iterative Square Mutate MyList 迭代完成 void iterSquareMutList(MyList list)方法。该方法修改 list,以便其所有元 素都是平方的。使用循环。
循环使用 while 只要列表不为空 则将值平方 随后将列表移动到 list.next
2.Recursive Square Mutate MyList 递归完成 void recSquareMutList(MyList list)方法。方法修改/变异列表,使其所有元素 都是平方的。不要使用循环。
3. Iterative Square Immutate MyList 迭代地完成方法 MyList iterSquareList(MyList list)。该方法不改变 list,而是创建一个所 有元素均为平方的新 MyList 对象。使用循环。
4.Recursive Square Immutate MyList 递归地完成方法 MyList recSquareList(MyList list)。该方法不改变 list,但创建一个所有 元素都为平方的新 MyList 对象。不要使用循环。
5. Iterative Catenate Mutate MyList 迭代地完成 MyList iterCatMutList(MyList list A,MyList listB)方法,返回一个由 listA 的 所有元素组成的列表,后跟 listB 的所有元素。 如果 listA 不为空,则该方法修改/变异 listA,使其也与 listB 连接。 使用循环。
6. Recursive Catenate Mutate MyList 递归地完成 MyList recCatMutList(MyList listA, MyList listB) 方法,返回一个由 listA 的所 有元素组成的列表,后跟 listB 的所有元素。 如果 listA 不为空,则该方法修改/变异 listA,使其也与 listB 连接。 不要使用循环。
7. Iterative Catenate Immutate MyList 迭代完成 MyList iterCatList(MyList listA, MyList listB)方法,返回一个由 listA 的所有元素 组成的列表,后跟 listB 的所有元素。 此方法不会更改列表 A。 使用循环。
8. Recursive Catenate Immutate MyList 递归地完成方法 MyList recCatList(MyList list A,MyList listB),返回一个由 listA 的所 有元素组成的列表,后跟 listB 的所有元素。 该方法不改变 listA。 不要使用循环。
上述八题,直接上代码:
package MyList;
public class MyList {
private int value;
private MyList next;
public MyList(int value, MyList next) {
this.value = value;
this.next = next;
}
// EXERCISE 6.1 ITERATIVE SQUARE MUTATE MYLIST
/**
* Square the elements of a MyList. Mutates the MyList.
* @param list is a MyList object.
*/
public static void iterSquareMutList(MyList list) {
//TODO: fill in method
if(list==null||list.iterSize()==0) return;
while(list!=null) {
int v = list.value;
list.value = v*v;
list = list.next;
}
}
// EXERCISE 6.2 RECURSIVE SQUARE MUTATE MYLIST
/**
* Square the elements of a MyList. Mutates the MyList.
* @param list is a MyList object.
*/
public static void recSquareMutList(MyList list) {
//TODO: fill in method
// base case
if(list==null||list.iterSize()==0) return;
if(list.next==null) {
int v = list.value;
list.value = v*v;
return;
}
// recursive step
int v = list.value;
list.value = v*v;
recSquareMutList(list.next);
}
// EXERCISE 6.3 ITERATIVE SQUARE IMMUTATE MYLIST
/**
* Square the elements of a MyList. Does not mutate the MyList.
* @param list is a MyList object.
* @return another MyList with all of input MyList's element squared.
*/
public static MyList iterSquareList(MyList list) {
//TODO: fill in method
if(list==null||list.iterSize()==0) return null;
if(list.iterSize()==1) {
MyList copy = new MyList(((list.value)*(list.value)), null);
return copy;
}else {
MyList head = null,tail = null;
int i=0;
while (list!=null) {
MyList node = new MyList(((list.value)*(list.value)), null);
if(i==0) {
head = node;
tail = node;
}else {
tail.next = node;
tail = node;
}
list = list.next;
i++;
}
return head;
}
}
// EXERCISE 6.4 RECURSIVE SQUARE IMMUTATE MYLIST
/**
* Square the elements of a MyList. Does not mutate the MyList.
* @param list is a MyList object.
* @return another MyList with all of input MyList's element squared.
*/
public static MyList recSquareList(MyList list) {
//TODO: fill in method
// base case
if(list==null||list.iterSize()==0) return null;
// recursive step
return new MyList(((list.value)*(list.value)), recSquareList(list.next));
}
// ASSIGNMENT 6.1 ITERATIVE CATENATE MUTATE MYLIST
/**
* Catenate two MyLists, listA and listB. Mutate listA.
* @param listA is a MyList object.
* @param listB is a MyList object.
* @return a list consisting of the elements of listA followed by the
* elements of listB.
*/
public static MyList iterCatMutList(MyList listA, MyList listB) {
//TODO: fill in method
if(listA==null) {
if(listB==null) return null;
else {
return listB;
}
}
if(listB==null) {
return listA;
}
MyList p = listA;
while(p.next!=null) {
p = p.next;
}
p.next = listB;
return listA;
}
// ASSIGNMENT 6.2 RECURSIVE CATENATE MUTATE MYLIST
/**
* Catenate two MyLists, listA and listB. Mutate listA.
* @param listA is a MyList object.
* @param listB is a MyList object.
* @return a list consisting of the elements of listA followed by the
* elements of listB.
*/
public static MyList recCatMutList(MyList listA, MyList listB) {
//TODO: fill in method
// base case
if(listA==null) {
if(listB==null) return null;
else {
return listB;
}
}
if(listB==null) {
return listA;
}
if(listA.next==null) {
listA.next = listB;
return listA;
}
// recursive step
return new MyList(listA.value, recCatMutList(listA.next,listB));
}
// ASSIGNMENT 6.3 ITERATIVE CATENATE IMMUTATE MYLIST
/**
* Catenate two MyLists, listA and listB. Does not mutate listA.
* @param listA is a MyList object.
* @param listB is a MyList object.
* @return a list consisting of the elements of listA followed by the
* elements of listB.
*/
public static MyList iterCatList(MyList listA, MyList listB) {
//TODO: fill in method
if(listA==null) {
if(listB==null) return null;
else {
return listB;
}
}
if(listB==null) {
return listA;
}
MyList p = listA,head = null,hp = null;
int i = 0;
while(p!=null) {
if(i==0) {
MyList node = new MyList(p.value, null);
head = node;
hp = head;
i++;
p = p.next;
continue;
}
MyList node = new MyList(p.value, null);
hp.next = node;
p = p.next;
hp = hp.next;
}
hp.next = listB;
return head;
}
// ASSIGNMENT 6.4 RECURSIVE CATENATE IMMUTATE MYLIST
/**
* Catenate two MyLists, listA and listB. Does not mutate listA.
* @param listA is a MyList object.
* @param listB is a MyList object.
* @return a list consisting of the elements of listA followed by the
* elements of listB.
*/
public static MyList recCatList(MyList listA, MyList listB) {
//TODO: fill in method
// base case
if(listA==null) {
if(listB==null) return null;
else {
return listB;
}
}
if(listB==null) {
return listA;
}
if(listA.next==null) return new MyList(listA.value, listB);
// recursive step
return new MyList(listA.value, recCatList(listA.next,listB));
}
/*
*
***** Do NOT modify the codes below from the lecture notes! *****
***** Only for your JUnit Testing purposes! *****
*
*/
/**
* @return the size of the MyList iteratively.
*/
public int iterSize() {
MyList p = this;
int size = 0;
while (p != null) {
size += 1;
p = p.next;
}
return size;
}
/**
* @return the size of the MyList recursively.
*/
public int recSize() {
// base case
if (next == null) {
return 1;
}
// recursive step
return 1 + this.next.recSize();
}
/**
* @param i is a valid index of MyList.
* @return the ith value of this MyList.
*/
public int get(int i) {
// base case
if (i == 0) {
return value;
}
// recursive step
return next.get(i - 1);
}
/**
* @param args is a variable number of integers.
* @return a new MyList containing the integers in args.
*/
public static MyList ofEntries(Integer... args) {
MyList result, p;
if (args.length > 0) {
result = new MyList(args[0], null);
} else {
return null;
}
int k;
for (k = 1, p = result; k < args.length; k += 1, p = p.next) {
p.next = new MyList(args[k], null);
}
return result;
}
/**
* @param l is a MyList object.
* @return true iff l is a MyList object containing the same sequence of
* integers as this.
*/
public boolean equals(Object l) {
if (!(l instanceof MyList)) {
return false;
}
MyList list = (MyList) l;
MyList p;
for (p = this; p != null && list != null; p = p.next, list = list.next) {
if (p.value != list.value) {
return false;
}
}
if (p != null || list != null) {
return false;
}
return true;
}
public String toString() {
int size = this.recSize();
String output= "[";
for (int i = 0; i < size; i++) {
output = output + this.get(i);
if (i != size-1)
output = output + ", ";
}
output = output + "]";
return output;
}
public static void main(String[] args) {
MyList list1 = MyList.ofEntries(1, 2, 3);
System.out.println(list1);
MyList emptyList = MyList.ofEntries();
System.out.println(emptyList);
}
}