Java遞歸作業之數組鏈接

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);
    }

}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章