I dont think there is a way to do what you want directly.
1 You could start from the beginning of the list and scan through to the end to get the count of records. Then start from the beginning again and count "n" records from the end. This is, of course, two passes.
2 You could make the list a doubly linked list - each record having pointers to the "Next" and the the "Previous" record. Get the pointer to the last record then you can work backwards from the end using the "Previous" pointer in one pass.
3 Use an array of pointers as you create the linked list. Each time you set the pointer to the "Next" record, also store it in an array. Then the "nth" entry in the array of pointers will give you the pointer to the record you want. This may be the best solution.
4 Consider whether your data would be better in an array and not as a list. This would depend on the number of records you have and whether you have an idea of the maximum number at the start of the program.
Hope this helps
Alan
|