create an array of size n, (e.g., fakeArray(20) creates an ‘array’ of 20 integers – but actually creates a list of 20 nodes each containing an int and a pointer to the next item on the list,

computer science

Description

Provide a class that implements an ordinary array of integers using a linked list. You must be able to:

  1. create an array of size n, (e.g., fakeArray(20) creates an ‘array’ of 20 integers – but actually creates a list of 20 nodes each containing an int and a pointer to the next item on the list,

  2. set any member of the array, (e.g., setArray(k,j) sets the value of element k in array to the value j),

  3. get any member of the array (e.g., i = getArray(k) returns the value of element k in array),

  4. reject requests for an out-of-bounds member of the array – that is, if the array is of size n, then valid values for an array index k are 0 <= k < n; anything else is invalid. (For simplicity’s sake, if index is out of bounds, print an error message and return -1; bonus points for a better solution to the excep-

    tion of an out of bounds value).

Think about what information you need to know about your fake arrays: these are the data values you’ll need in your class.

Suppose you name your class fakeArray. Then someone who wants to make one of your arrays would declare

         fakeArray foo(20)

to create a fake array of 20 integers: this tells you, in part, what your constructor needs to look like. Then

foo.setArray(0,5);

sets element 0 of this fake array to the value 5.


Related Questions in computer science category