Wrox Programmer Forums
|
C++ Programming General discussions for the C++ language. For questions specific to Microsoft's Visual C++ variant, see the Visual C++ forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C++ Programming section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old April 5th, 2005, 11:05 PM
Registered User
 
Join Date: Apr 2005
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default singly linked list

I can't think of a way to access the nth member from the end of a singly linked list on one pass, unless the count of items is somehow published. Can anyone see a way that I am blind to?

Reply With Quote
  #2 (permalink)  
Old April 6th, 2005, 12:14 AM
Authorized User
 
Join Date: Mar 2005
Posts: 58
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via MSN to Alan-LB Send a message via Yahoo to Alan-LB
Default

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


Reply With Quote





Similar Threads
Thread Thread Starter Forum Replies Last Post
Linked List? pmcizhere Java Basics 2 March 6th, 2008 09:02 PM
2D Linked List sjf905 C# 0 October 8th, 2006 11:02 PM
C# Object Linked List millsbruce C# 5 July 12th, 2005 04:15 PM
Linked List....!!! amahja56 C++ Programming 4 April 6th, 2004 01:05 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.