I am writing a linked list for my homework assignment and I need to implement begin() and end() for my program’s requirement.
Begin() is fairly easy
Node* List::begin(){return head;}
How should I implement end()?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You can return a
NULLpointer, or if you have a customtailnode, return that. If it’s a circular doubly-linked list, you can return the sentinel node.Keep in mind that the proper way to check for the end of the list is to call
List::end(), so if you’ve properly setup yourListclass, then it doesn’t exactly matter what you return, as long asworks and you exit the
while-loop when you reach the end of the list. So that main thing is thatList::end()needs to return something unique that you will never encounter if you’re traversing the middle of the list.