a. Given the IntListElement class below write a sequence of statements (a progra
ID: 3799236 • Letter: A
Question
a. Given the IntListElement class below write a sequence of statements (a program fragment) that would create a variable of type IntListElement named listA that refers to a list with two elements, storing 2014 in the first element of the list and 2015 in the second element of the list. (1 point bonus if you can do it with just one statement.)
class IntListElement {
IntListElement(int value, IntListElement e) {
data = value;
next = e;
}
IntListElement next;
int data;
}
b. Write a method times that takes a reference to an IntListElement, list, and an integer, n. The method does not return anything but instead multiples the data field of each element in the list by n. E.g. If the list contained 3, 4, 5, then the call times(list, 10) would change the list to contain 30, 40, 50.
c. Write a method set(), to be added to the IntListElement class from problem 1, that takes two integer parameters, n and value. The method does not return
anything but sets the data field of the nth element in the list to value (starting with the first element being at position 0). For example if myList pointed to the list containing the 5 elements 100, 200, 300, 400, 500 then executingmyList.set(2,99); would result in the list elements being 100, 200, 99, 400, 500. If n is less than zero or greater than the length of the list minus 1, then the method does nothing. Your solution must be a recursive solution.
Explanation / Answer
a) IntListElement ListA[2] { { "2014", arr[1] }, { "2015", NULL } };
Explnation: this is similar to the decelration of an array, and using the initilization list within it to populate the same. As you have given this will give bonus I have done like this. Alternatively you can do it in 2 steps as well
b)times(IntListElement arr[],n)
{
for (IntListElement e: arr)
e.setdata(n*(e.getdata());//e.data = e.data*n // alternatively this can also be used
}
c) set(integer n, integer val)
{ if(n==0)
{
e.setdata(val);
}
else
set(n-1, val);
}
// This is very much self explanatory
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.