Implementing a Doubly Linked List Iterator
This lesson contains approximately 29 minutes of video content.
Set-up
Implementing a DoublyLinkedListIterator<T>
Updating DoublyLinkedList<T> to use DoublyLinkedListIterator<T>
Using DoublyLinkedList<T>'s iterators
Activity: Doubly linked list iterator implementation
Graded Playground Autograder
Activity Prompt:
Member functions you must implement for
Member functions provided to you for
In this programming problem, you will implement a forward iterator type DoublyLinkedListIterator<T>
for a DoublyLinkedList<T>
. To understand the DoublyLinkedList<T>
's structure, you should read includes/doubly_linked_list.hpp
and includes/node.hpp
. Understanding the data structure for which you're writing an iterator is important.
After reviewing the structure of DoublyLinkedList<T>
, you will implement a forward iterator type DoublyLinkedListIterator<T>
for it. The member functions that you are responsible for follow.
Member functions you must implement for DoublyLinkedListIterator<T>
public: | |
DoublyLinkedListIterator(Node<T>* ptr); |
Parameterized constructor. Initializes current_ with the value stored in ptr . |
T& operator*(); |
Returns a reference to the data member of the node pointed to by current_ . |
DoublyLinkedListIterator<T>& operator++(); |
Pre-increment Operator (invoked as ++obj ). This will move current_ one node forward in the sequence of nodes contained within the doubly linked list. This operator will return a self-reference (i.e., return *this ) after the increment has been performed. |
DoublyLinkedListIterator<T> operator++(int); |
Post-increment Operator (invoked as obj++ ). A temporary DoublyLinkedListIterator<T> tmp is initialized with the state of this iterator object. You can accomplish this using the copy constructor in conjunction with *this . After recording the state of this iterator object in tmp , this->current_ is moved forward to the next node in the sequence of nodes contained within the doubly linked list. You then return tmp , which represents the state of this iterator prior to the increment.
|
bool DoublyLinkedListIterator<T>::operator!=(
const DoublyLinkedListIterator<T>& other); |
Returns true if the two iterators are not pointing to the same node in the sequence; false otherwise.
|
Member functions provided to you for DoublyLinkedListIterator<T>
public: | |
DoublyLinkedListIterator() = default; |
Default constructor. |
DoublyLinkedListIterator<T>
's data members
private: | |
Node<T>* current_ |
Pointer to the node in the doubly linked list's sequence to which the iterator currently "points". |
We will only transmit the contents of your doubly_linked_list_iterator.hpp
file to the grader. Let's get started!
#include <iostream>
#ifndef DOUBLY_LINKED_LIST_ITERATOR_HPP
#define DOUBLY_LINKED_LIST_ITERATOR_HPP
#ifndef DOUBLY_LINKED_LIST_HPP
#define DOUBLY_LINKED_LIST_HPP
#ifndef NODE_HPP
#define NODE_HPP
Submit is not supported in this site.