I have N keywords which can be mapped as follows:
1,2,3,4----/5,6,7,8----/9,10,11,12----/--------
From the above line I should be able开发者_Python百科 to map 1 to 5,2 to 6, 3 to 7, so on. I should also be able to map 1 to 9, 1 to 10, 1 to 11, 1 to 12 so on like that I should be able to map N number of combinations. I should make this generic in such a way that it should work with N number of combinations. Please tell me the logic how I can do this.
I'm not sure but what I think you want is a mapping like this:
Input: 1,2,3,4\22,23,24,25,\103,104,105,106\
and end up with a map like: 1->(22)->(103), 2->(23)->(104) etc...
.
In order to manage that you would need the following elements:
- List nodes
- list
- Parsing
- Mapping
You could write your own linked list (it's a nice exercise and not too complicated), or you could use one of the STL/Boost containers. The general procedure would be
- Accept input
- Verify input
- Parse input (into a list/array, or a temp variable)
- Either create master node in master list, or add to a sub-list. (see below)
You need a two dimensional list.
1 -> 2 -> 3 -> 4
| | | |
22-> 23 -> 24-> 25
| | | |
103->104-> 105->106
So each node would need at least 2 pointers (you could call them "right" and "down", both NULL for "106"), but it would probably be better to have 4 (up and left as well... both NULL for "1"). This allows dynamic re-sizing, adding etc... and also lets you have different numbers of mappings at each level. I'm sure that there are LOTS of ways to do this.
精彩评论