muks
BAN USERHi, here's one piece of code which was developed with reference to the one I noticed at geeksforgeeks
public Link addLists (Link link1, Link link2) {
Link res = null;
Link prev = null;
Link temp = null;
int carry = 0, sum;
while (link1 != null || link2 != null) {
//sum = carry + ( (l1.data1 != null) ? l1.data1: 0) + ( (l2.data1 != null) ? l2.data1: 0);
sum = carry + link1.data1 + link2.data1;
carry = (sum >= 10)? 1 : 0; // update carry for next calulation
sum = sum % 10; // update sum if it is greater than 10
temp = new Link(sum); // Create a new node with sum as data
// if this is the first node then set it as head of the resultant list
if(res == null) {
res = temp;
} else { // If this is not the first node then connect it to the res.
prev.nextLink = temp;
}
// Set prev for next insertion
prev = temp;
// Move first and second pointers to next nodes
if (link1 != null) {
link1 = link1.nextLink;
}
if (link2 != null) {
link2 = link2.nextLink;
}
}
if (carry > 0) {
temp.nextLink = new Link(carry);
}
return res;
} // addLists()
Try this java code here....
public Link addLists (Link link1, Link link2) {
Link res = null;
Link prev = null;
Link temp = null;
int carry = 0, sum;
while (link1 != null || link2 != null) {
//sum = carry + ( (l1.data1 != null) ? l1.data1: 0) + ( (l2.data1 != null) ? l2.data1: 0);
sum = carry + link1.data1 + link2.data1;
carry = (sum >= 10)? 1 : 0; // update carry for next calulation
sum = sum % 10; // update sum if it is greater than 10
temp = new Link(sum); // Create a new node with sum as data
// if this is the first node then set it as head of the resultant list
if(res == null) {
res = temp;
} else { // If this is not the first node then connect it to the res.
prev.nextLink = temp;
}
// Set prev for next insertion
prev = temp;
// Move first and second pointers to next nodes
if (link1 != null) {
link1 = link1.nextLink;
}
if (link2 != null) {
link2 = link2.nextLink;
}
}
if (carry > 0) {
temp.nextLink = new Link(carry);
}
return res;
} // addLists()
One can build large number of test cases if we take the following approach.
1. Build test case around input like, Check if it is not too glossy so that your shadow appears or the marker ink doesn't hold up to the board and so on.
2. Build tests around the output - like board being with too glossy surface, showing up higher level of reflection for which stuff written on the board is not visible and so on..
3. Build tests around user acceptance tests - What is the board intended to (kind of user, drawing, writing - the width-lenght of the bard), How heavy is the board, is it portable or not and so on.
- muks June 01, 2013