Consider the LinkedIntList class, which is given in the book and which was cover
ID: 3779017 • Letter: C
Question
Consider the LinkedIntList class, which is given in the book and which was covered in the Chapter 16 lecture notes.
Suppose we create an array of LinkedIntList objects, as in the code below:
and then begin to add nodes to each LinkedIntList, as in the code below:
What happens to the array, in memory, when nodes are added to the LinkedIntLists that the array holds? Is the array re-sized each time, or does it stay the same size? Are the LinkedIntList objects added to the array, or are they all deleted when the for loop is finished?
Answer with complete sentences.
LinkedIntList a new LinkedIntList[n] for i 0; ikn; i++) LinkedIntList list new LinkedIntListo; a[i] listExplanation / Answer
This type of DS we can use for graph representation i.e. an array of linkedlists where array size will be equal to number of vertices and every element of array represent a linkedlist of vertices those are connected to that vertice.
So if nodes are added to the LinkedIntLists that array holds, nothing is going to happen with array , size of array will remain same and always will be equal to n*sizeof(LinkedIntList) , so if size of linkedIntList is say 8(4 for data & 4 for next pointer) then array size will always remain n*8.
But LinkedIntLists will expand as we will keep adding elements to them,so initially size of linkedlist represented by a[0] will be 8. but after adding the 5 , 6 , 7 , 99 elements, its size will be 40 bytes. same with other linkedlists too.
When you write
LinkedInList[] a = new LinkedInList[n];
it will create an array of linkedlist ,but each linkedlist element of array is uninitialized(data as well as next pointers)
So we have intialize the value of data and pointers and here it is done by constructor(assuming LinkedIntList as class which is having data and next pointer objects).
So after the completion of loop all the elements of array(each linkedlist) will be initialized according to the constructor.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.