You are on page 1of 8

//package lecture;

public class IntList {


public int head;
public IntList tail;

public IntList(int h, IntList t) {


head = h;
tail = t;
}

/** Retuns the size of this IntList */


public int size() {
if (tail == null) {
return 1;
}
return 1 + tail.size();
}

/** Returns the size of this IntList. */


public int iterativeSize() {
IntList p = this;
int size = 0;
while (p != null) {
size += 1;
p = p.tail;
}
return size;
}

/** In class exercise 1:


* Returns ith item of this IntList. For
* simplicity, assume the item exists. */
public int get(int i) {
/** your code here */

if (i == 0) {
return head;
}

return tail.get(i - 1);

public String toString() {


if (tail == null)
return Integer.toString(head);
return Integer.toString(head) + " " + tail.toString();
}

/** Returns an IntList identical to L, but with


* each element incremented by x. L is not allowed
* to change. */
public static IntList incrList(IntList L, int x) {
/* Your code here. */
/* I've used recurison, but iteration also works. */

/* other base cases are possible */


if (L == null) {
return null;
}

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

/** Returns an IntList identical to L, but with


* each element incremented by x. Not allowed to use
* the 'new' keyword. */
public static IntList dincrList(IntList L, int x) {
/* Your code here. */

if (L == null) {
return null;
}

L.head=L.head+x;
L.tail= dincrList(L.tail,x);
return L;

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

if(A.tail==null){

A.tail=B;

return A;

return dcatenate(A.tail,B);

public static IntList squareListRecursive(IntList L) {


if (L == null) {
return null;
}
return new IntList(L.head * L.head, squareListRecursive(L.tail));
}

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

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

import java.util.Formatter;

import static org.junit.Assert.*;

import java.util.Scanner;

import org.junit.Test;

public class IntList {


/**
* First element of list.
*/
public int head;
/**
* Remaining elements of list.
*/
public IntList tail;

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

/** DO NOT MODIFY ANYTHING ABOVE THIS LINE! */

/**
* Returns a list consisting of the elements of A followed by the
* * elements of B. May modify items of A. Don't use 'new'.
*/

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


//TODO: fill in method

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;

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