S. What contalner should yeu chse? White V if a etor is a better cholce, and L i
ID: 3723847 • Letter: S
Question
S. What contalner should yeu chse? White V if a etor is a better cholce, and L if a inked list (doubly linked, with a front pointer) is a better choice For one, the answer is that they are the same; write VL. (a) You frequently have to modify the middle of the container. (b) You frequentiy need to seres elements in the middle in the coutainer. V (e) You only nend to modtify and access at one end. V L (d) You need to modify and access both ends. (e) you need a simple basic container that is “ step up from an array, VExplanation / Answer
Vectors are usually good for reads, insertions and deletions at the back as it takes constant time. However they are not very efficient when it comes to insertion in the front or anywhere else(middle also) because that causes all other elements to be shifted.
Similarly doubly linked list with a front pointer takes O(n) time to insert / delete/ read element in the middle of the list.
Hence for (a) You frequently have to modify the middle of the container, : Both V and L will perform with same efficiency
Similarly for (b) You frequently need to access the elements in the middle of the container: Both V and L will perform with similar efficiency
If modifications and accesses occur at only one end, both V and L will do fine because it we use a list then we can perform accesses and modifications at the FRONT end in constant time.
Whereas if we use vector then we can perform accesses and modifications at the BACK end in constant time.
Hence for (c) You need to modify and access at one end: VL is the answer
If you need to modify and access at both ends, we must remember vectors add at the end and are best for insertions/access/deletions at the back. List are good for insertions/deletions in front(since we have only front pointer)
Hence for (d) You need to modify and access both ends: VL is the answer
If we need a basic step-up from an array we have to choose vectors because the most characteristic feature of arrays which is maintained by vectors is contiguous memory allocation.
So in case of (e) You need a simple basic container that is a step-up from an array: V is the answer
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.