You are on page 1of 4

public static IntList catenate(IntList A, IntList B) {

/* IntList res = new IntList(A.head, null);


IntList ptr = res;
A = A.tail;
while (A != null) {
ptr.tail = new IntList(A.head , null);
A = A.tail;
ptr = ptr.tail;
}
if( A==null)
{
// B= B.tail;
while (B != null) {

ptr.tail = new IntList(B.head , null);


B = B.tail;
ptr = ptr.tail;

return res;
*/

//TODO: fill in method

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;
} */

return new IntList(A.head , catenate(A.tail, B));

public static void main(String[] args) {


IntList A = new IntList(1, null);
A.tail = new IntList(2, null);
A.tail.tail = new IntList(3, null);

IntList B = new IntList(4, null);


B.tail = new IntList(5, null);
B.tail.tail = new IntList(6, null);

/* IntList L = new IntList(4, null);


L.tail = new IntList(5, null);
L.tail.tail = new IntList(6, null);*/
// System.out.println(L.size());
// System.out.println(L.iterativeSize());

// Test your answers by uncommenting. Or use the Visualizer.


//System.out.println(L.get(1));
// System.out.println(L);
//System.out.println(incrList(L, 3));
// System.out.println(dincrList(L, 3));
// System.out.println(dcatenate(A,B));

// System.out.println(squareListRecursive(L));

System.out.println(catenate(A,B));
}
}

===================================================================================
============

/**
* 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;

for (p = this; p != null && L != null; p = p.tail, L = L.tail) {


if (p.head != L.head) {
return false;
}
}
if (p != null || L != null) {
return false;
}
return true;
}

/**
* 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.
*/

private int detectCycles(IntList A) {


IntList tortoise = A;
IntList hare = A;

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 (tortoise == null || hare == null)


return 0;

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;

for (IntList p = this; p != null; p = p.tail) {


out.format("%s%d", sep, p.head);
sep = ", ";

cnt++;
if ((cnt > cycleLocation) && (cycleLocation > 0)) {
out.format("... (cycle exists) ...");
break;
}
}
out.format(")");
return out.toString();
}

public static void main(String[] args) {


// TODO Auto-generated method stub

System.out.print("What is your age? ");

IntList A = IntList.list(1, 2, 3);


IntList B = IntList.list(4, 5, 6);
IntList exp = IntList.list(1, 2, 3, 4, 5, 6);
assertEquals(exp, IntList.dcatenate(A, B));
assertEquals(IntList.list(1, 2, 3, 4, 5, 6), A);

You might also like