Documente Academic
Documente Profesional
Documente Cultură
if (i == 0) {
return head;
}
/* You can do the two lines on one line, but I've opted
* for two lines for maximum pedagogical clarity. */
IntList incrementedList = new IntList(L.head + x, null);
incrementedList.tail = incrList(L.tail, x);
return incrementedList;
}
if (L == null) {
return null;
}
L.head=L.head+x;
L.tail= dincrList(L.tail,x);
return L;
if(A.tail==null){
A.tail=B;
return A;
return dcatenate(A.tail,B);
return res;
*/
if (A == null) {
// ptr=B; // no need to point to B as we can return B when Null of A
is reached.
// this will simplify the the neeed to iterate over entire B.
return B;
}
/* if(A==null){
ptr=B;
} */
// System.out.println(squareListRecursive(L));
System.out.println(catenate(A,B));
}
}
===================================================================================
============
import java.util.Formatter;
import java.util.Scanner;
import org.junit.Test;
/**
* A List with head HEAD0 and tail TAIL0.
*/
public IntList(int head0, IntList tail0) {
head = head0;
tail = tail0;
}
/**
* A List with null tail, and head = 0.
*/
public IntList() {
/* NOTE: public IntList () { } would also work. */
this(0, null);
}
/**
* Returns a list equal to L with all elements squared. Destructive.
*/
public static void dSquareList(IntList L) {
while (L != null) {
L.head = L.head * L.head;
L = L.tail;
}
}
/**
* Returns a list equal to L with all elements squared. Non-destructive.
*/
public static IntList squareListIterative(IntList L) {
if (L == null) {
return null;
}
IntList res = new IntList(L.head * L.head, null);
IntList ptr = res;
L = L.tail;
while (L != null) {
ptr.tail = new IntList(L.head * L.head, null);
L = L.tail;
ptr = ptr.tail;
}
return res;
}
/**
* Returns a list equal to L with all elements squared. Non-destructive.
*/
public static IntList squareListRecursive(IntList L) {
if (L == null) {
return null;
}
return new IntList(L.head * L.head, squareListRecursive(L.tail));
}
/**
* Returns a list consisting of the elements of A followed by the
* * elements of B. May modify items of A. Don't use 'new'.
*/
while (A.tail!=null){
A= A.tail;
}
if(A.tail==null){
A.tail=B;
}
return A;
}
/**
* Returns a list consisting of the elements of A followed by the
* * elements of B. May NOT modify items of A. Use 'new'.
*/
public static IntList catenate(IntList A, IntList B) {
//TODO: fill in method
return null;
}
/**
* DO NOT MODIFY ANYTHING BELOW THIS LINE! Many of the concepts below here
* will be introduced later in the course or feature some form of advanced
* trickery which we implemented to help make your life a little easier for
* the lab.
*/
@Override
public int hashCode() {
return head;
}
/**
* Returns a new IntList containing the ints in ARGS. You are not
* expected to read or understand this method.
*/
public static IntList list(Integer... args) {
IntList result, p;
if (args.length > 0) {
result = new IntList(args[0], null);
} else {
return null;
}
int k;
for (k = 1, p = result; k < args.length; k += 1, p = p.tail) {
p.tail = new IntList(args[k], null);
}
return result;
}
/**
* Returns true iff X is an IntList containing the same sequence of ints
* as THIS. Cannot handle IntLists with cycles. You are not expected to
* read or understand this method.
*/
public boolean equals(Object x) {
if (!(x instanceof IntList)) {
return false;
}
IntList L = (IntList) x;
IntList p;
/**
* If a cycle exists in the IntList, this method
* returns an integer equal to the item number of the location where the
* cycle is detected.
* <p>
* If there is no cycle, the number 0 is returned instead. This is a
* utility method for lab2. You are not expected to read, understand, or
* even use this method. The point of this method is so that if you convert
* an IntList into a String and that IntList has a loop, your computer
* don't get stuck in an infinite loop.
*/
if (A == null)
return 0;
int cnt = 0;
while (true) {
cnt++;
if (hare.tail != null)
hare = hare.tail.tail;
else
return 0;
tortoise = tortoise.tail;
if (hare == tortoise)
return cnt;
}
}
@Override
/** Outputs the IntList as a String. You are not expected to read
* or understand this method. */
public String toString() {
Formatter out = new Formatter();
String sep;
sep = "(";
int cycleLocation = detectCycles(this);
int cnt = 0;
cnt++;
if ((cnt > cycleLocation) && (cycleLocation > 0)) {
out.format("... (cycle exists) ...");
break;
}
}
out.format(")");
return out.toString();
}