Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please write in C++ Homework - Linked List Iterators Goal: To understand how to work with iterators of a linked list. To also learn how

image text in transcribedimage text in transcribedimage text in transcribed

Please write in C++

Homework - Linked List Iterators Goal: To understand how to work with iterators of a linked list. To also learn how iterators are used, and why iterators are useful. To be exposed to using algorithms and lambda expressions alongside iterators Note: that this assignment does not immediately compile. You must implement the begin() and operator*() to get the first tests to pass. Then you should comment out tests one-by-one and implement the missing parts for those. Instructions: Create and implement the following methods: - The DoublyLinkedList begin() method: This should create a temporary iterator object. Then set the iterator object's pastTheEnd to false (unless the list is empty, then set it to true), and the iterator object's pointer should be assigned to the list's head. Then the iterator object is returned. A diagram is given below: - The DoublyLinkedList end() method: This should create a temporary iterator object. Then set the iterator object's pastTheEnd to true, and the pointer should be assigned to the list's tail. Then the object is returned. Note that the end iterator acts as though it's really "past the end". See operator- - to get a better understanding what this means. A diagram is given below: - overloaded operator: This just returns the data that is in the node that iterator object is pointing to. A oneliner. Make sure the return type is a T \& and not a T. A diagram of the data object you need to return is given below. - overloaded != operator: This checks to see if both iterator objects have the same pastTheEnd and the same pointer values. If both objects have the same values, then return false. Otherwise just return true. Note that you are comparing the "this" object with another Iterator object passed in as an argument into operator!=, which requires that operator!= has a single Iterator parameter (const and by-reference is a good idea here). A diagram is given below: equality. - overloaded ++ prefix operator: The return type is Iterator T. The iterator should be moved forward one node, if possible. If moving it forward would mean it would become emptyullptr, don't move it forward, and instead set the boolean pastTheEnd to true. If pastTheEnd is already true, you don't need to do anything. You need a return type, you need to return a copy of the iterator, and you can do that with: return *this. - overloaded ++ postfix operator: For C++ to know which is a prefix and which is a postfix, the prefix operator ++ has no arguments, the postfix operator ++ has a single (int) in the parameter section, such as e.g. operator ++ (int). The return type is Iterator T. This method is similar to the prefix operator with one change. You first have to make a copy of itself (e.g. auto copyOfItself = this;). Now use similar incrementing logic as the operator++ prefix operator logic. For the return object, return the copy object. - overloaded -- prefix operator: The return type is to be Iterator T. Somewhat like to the operator++ prefix, but going backwards. You need to return a copy of the iterator, and you can do that with return *this. Note that while operator ++ uses pastTheEnd to prevent the iterator from doing undesirable things on the right side of the collection, operator - - has no equivalent mechanism for the left side. The behavior of going off the left side of the collection is undefined, or in other words, don't worry about supporting it. Note that I added one additional test on this assignment, testIterationTricky( ). Those test the ability to handle a one node and a zero node linked list. Make sure you use the data member pastTheEnd appropriately. Hint: pastTheEnd should never be false if the list is empty. - The assignmentReverse() method also has you test working with iterators and not with the collection itself. Note that the parameters begin and end are iterator objects. You must use only these two iterators to find a way to reverse the containers. These iterators are not node pointers, so you cannot request a node's forward pointer. You can only use the operators ++ (prefix), ++ (postfix), -- (prefix), ==,!=, and . Note that it is easy to solve this for a container of an odd number of items. An ugly solution is to somehow count up the number of nodes, then start over and loop half of that count. A better way is given below. Suppose a container contains these items: A B C D E. An iterator returned by begin() points to item A, and an iterator returned by end ( points to the past-the-end virtual state just past item E. The first step should be to decrement that iterator at past-the-end so it is on item E. Now swap the values of the two iterators. The container should now be E B C D A. Verify if the iterators are the same (operator== or operator!=). They are not the same. Move the two iterators inwards so they point to items B and D. Swap data. The container should now be E D C B A. Move the two iterators inwards, so they both point to item C. Verify if the iterators are the same (operator== or operator!=). They are the same, so the algorithm is done. When the list contains an even number of items, reversing gets a bit tricker

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Introduction To Data Mining

Authors: Pang Ning Tan, Michael Steinbach, Vipin Kumar

1st Edition

321321367, 978-0321321367

More Books

Students also viewed these Databases questions

Question

What does stickiest refer to in regard to social media

Answered: 1 week ago

Question

=+What is the procedure for labor relations in the workplace?

Answered: 1 week ago

Question

=+ Are ballots compulsory?

Answered: 1 week ago