What would be the advantage of using a linked list of arrays?
I have read that if the data is large and insertions are allowed (for example, a text buffer), then a common halfway measure is to use a linked list of arrays .
But there was no explanation on what is the benefit of using such a structure.
Or at least I did not get it.
So what would be the gain in using a linked list of arrays and when?
6 Answers 6
With Linked list of arrays you can easily combine both the advantages of linked lists and static arrays.
Check this question.
Comments
I think it gives an ability to insert a new portion of data (quite big, might be) simply as a new array into a list, without touching existing arrays in the list which go after an inserting position.
Generally, inserting into a linked list is faster than inserting into an array, because it doesn't require shifting elements after new one. In case you described, the benefit should even more significant because you don't need to shift all subsequent data which is large (in average) by definition.
At the same time, we should take into account that access to such data structure is more complex and slower.
P.S. Also, if speed is really critical for you, firstly I would do a small investigation with comparison of existing real implementation of data structures (ArrayList, LinkedList, arrays, etc.).
Comments
When you'll need fast object obtaining from collection and you'll not be sure, that you have enough memory to allocate "whole" array (ArrayList implementation), you might write your own implementation which uses list of arrays to rapidly fast find the object you're looking for.
But there are definitely better data structures for this approach.
Comments
I can think of opposite situation: array of linked lists (actually hashmap) where every array element is a bucket that points to the list that can dynamically grow.Think of Dictionary that is being built and new words are added but the number of letters is constant.
Some example of what you describe can be a skip list based solution I can also think of some efficient hashing or implementing a dynamic queue of constant sets of batch jobs.
Think about this problem:
Imagine a (literal) stack of plates If the stack gets too high, it might topple There- fore, in real life, we would likely start a new stack when the previous stack exceeds some threshold Implement a data structure SetOfStacks that mimics this SetOf- Stacks should be composed of several stacks, and should create a new stack once the previous one exceeds capacity (Cracking Tech Code Interview)
Comments
No need to create custom data structure if unnecessarily. Beacause,as Linked List of Array, there's no difference between HashMap.
1 Comment
Explore related questions
See similar questions with these tags.
linkedlistwhich has slow access but fast insertions and deletions and anarraywhich is exactly the opposite.I thought that may be this is a common "trick" I am not awareArrayListis the same asstd::vectorin C++, it's a pretty thin wrapper for an array, and if the insertion index exceeds the initially allocated size, the whole array gets realloced and copied. This is unnoticable for small amounts of data, but once you start reallocating and copying a few GBs, you get the gist... Hence the name Array List.LinkedListofArrayList, right? You don't want to mix up java collections with standard arrays, or do you?