How to, Sequentially distribute data from a list to arrays (or some other structure) of fixed size/weight.
For example, lets say I have a list of 10 elements. I want to sequentially distribute the elements to 3 arrays A, B, and C. The arrays are given a weight lets say A=3, B=5, and C=2.
The element would be distributed in the following fashion.
A - list[0] list[3] list[6]
B - list[1] list[4] list[7] list[8] list[9]
C - list[2] list[5]
If the size of the list was 13, then after the above distribution, it would start again after the cycle completes
A - list[0] list[3] list[6] list[10]
B - list[1] list[4] list[7] list[8] list[9] list[11]
C - list[2] list[5] list[12]
The size of the list is dynamic, so in the above example, the list size would be 3, 4, or 100, etc. And the number of arrays (or structure) and weight of the arrays are available in runtime only.
Is there a good way to solve it?
The easiest way to sequentially distribute element across different different weighted array (fixed size) array is to auto pollulate the stacks using array size. And put the stack in a circular array. sequentially get the next stack and pop the element of the stack. If stack has not more element remove it from the circular array until the distribution list is empty. if distribution list size is not predefined, or if circular array becomes empty, reset the circular array, and start from the beginning.
A=3, B=5, and C=1.
//add stack to array
CircularLinkedList list = new CircularLinkedList();
list.add(s1);
list.add(s2);
list.add(s3);
//iterate till the stack is empty, reset the stack if distribution list is remaining
// or if the lead comes from another source for example website, simply get the next stack
Here is an example of circular list //http://www.cs.princeton.edu/algs4/13stacks/CircularLinkedList.java.html