Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

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.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8428985
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T05:12:31+00:00 2026-06-10T05:12:31+00:00

Im making a doublyLinkedList. The error is to do with my getIterator method. I

  • 0

Im making a doublyLinkedList. The error is to do with my getIterator method. I cant figure this out. does anyone know?

Here is where the error is?
    // ------------------------------------------------------------------------------
    //  Name:           GetIterator
    //  Description:    Generates an iterator pointing towards the current head node
    //  Arguments:      None.
    //  Return Value:   <Datatype> Iterator
    // ------------------------------------------------------------------------------
    DoublyListIterator<Datatype> getIterator()
    {
        return DoublyListIterator<Datatype>(this, m_head);
    }
};

#ifndef DOUBLYLINKEDLIST_H
#define DOUBLYLINKEDLIST_H

#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;

template<class Datatype> class DoublyListNode;
template<class Datatype> class DoublyLinkedList;
template<class Datatype> class DoublyListIterator;

// -------------------------------------------------------
// Name:        DListNode
// Description: This is the Doubly-linked list node class.
// -------------------------------------------------------
template<class Datatype>
class DListNode
{
public:
    Datatype m_data;
    DListNode<Datatype>* m_next;
    DListNode<Datatype>* m_prev;

    // ----------------------------------------------------------------
    //  Name:           InsertAfter
    //  Description:    This adds a node after the current node.
    //  Arguments:      p_data - The data to store in the new node.
    //  Return Value:   None.
    // ----------------------------------------------------------------
    void InsertAfter(Datatype p_data)
    {
        // create the new node.
        DListNode<Datatype>* newNode= new DListNode<Datatype>;
        newNode->m_data = p_data;
        // make the new node point to the next node.
        newNode->m_next = m_next;
        newNode->m_prev = this;

        // make the node before it, point to the new node
        if(m_next != 0)
            m_next->m_prev= newNode;

        m_next = newNode;
    }
    // ----------------------------------------------------------------
    //  Name:           InsertBefore
    //  Description:    This adds a node before the current node.
    //  Arguments:      p_data - The data to store in the new node.
    //  Return Value:   None.
    // ----------------------------------------------------------------
    void InsertBefore(Datatype p_data)
    {
        //Create new Node
        DListNode<Datatype>* newNode = new DListNode<Datatype>;
        newNode->m_data = p_data;
        //Set up new Node Pointers
        newNode->m_next = this;
        newNode->m_prev = m_prev;
        //if theres a node before it, make it point to new node
        if(m_prev != 0)
            m_prev->m_next = newNode;

        m_prev = newNode;
    }
};
// -------------------------------------------------------------
// Name:        DLinkedList
// Description: This is the Doubly-linked list container class.
// -------------------------------------------------------------
template<class Datatype>
class DoublyLinkedList
{
public:
    DListNode<Datatype>* m_head;
    DListNode<Datatype>* m_tail;
    int m_count;

    // -----------------------------------------------------------------------------------------
    //  Name:           DLinkedList
    //  Description:    Constructor, creates a head node, tail node & a count.
    //  Arguments:      m_head: the head node of the list.
    //                  m_tail: the tail node of the list.
    //  Return Value:   None.
    // -----------------------------------------------------------------------------------------
    DoublyLinkedList()
    {
        m_head= 0;
        m_tail= 0;
        m_count= 0;
    }

    // -----------------------------------------------------------------------------------------
    //  Name:           ~DLinkedList
    //  Description:    Destructor, creates a pointer for the head node,
    //                  deletes the value in the list and iterates.
    //  Arguments:      m_head: the head node of the list.
    //                  itr:    moves through the list.
    //  Return Value:   None.
    // -----------------------------------------------------------------------------------------
    ~DoublyLinkedList()
    {
        // temporary node pointers.
        DListNode<Datatype>* itr= m_head;
        DListNode<Datatype>* next;
        while(itr != 0)
        {
            // save the pointer to the next node.
            next = itr->m_next;
            // delete the current node.
            delete itr;
            // make the next node the current node.
            itr= next;
        }
    }
    // ----------------------------------------------------------------
    //  Name:           Length
    //  Description:    Gets the size of the list
    //  Arguments:      None.
    //  Return Value:   size of the list.
    // ----------------------------------------------------------------
    int Length()
    {
        return m_count;
    }
    // -------------------------------------------------------------------------------------
    //  Name:           Append
    //  Description:    This adds a node to the end of the list, then points to the newNode
    //  Arguments:      p_data - The data to store in the new node.
    //                  m_count is increased.
    //  Return Value:   None.
    // -------------------------------------------------------------------------------------
    void Append(Datatype p_data)
    {
        if(m_head == 0)
        {
            // create a new head node.
            m_head= m_tail= new DListNode<Datatype>;
            m_head->m_data= p_data;
            m_head->m_next= 0;
            m_head->m_prev= 0;
        }
        else
        {
            // insert a new node after the tail and reset the tail.
            m_tail->InsertAfter(p_data);
            m_tail= m_tail->m_next;
        }
        m_count++;
    }
    // -----------------------------------------------------------------------------------
    //  Name:           Prepend
    //  Description:    This adds a node before the head node, then points to the newNode
    //  Arguments:      p_data - The data to store in the new node.
    //                  m_count is increased.
    //  Return Value:   None.
    // -----------------------------------------------------------------------------------
    void Prepend(Datatype p_data)
    {
        // create the new node.
        DListNode<Datatype>* newNode= new DListNode<Datatype>;
        newNode->m_data= p_data;

        newNode->m_next= m_head;
        // set the head node and the tail node if needed.
        m_head= newNode;
        if(m_tail== 0)
            m_tail= m_head;
        m_count++;
    }
    // ----------------------------------------------------------------------------------------------------------------
    //  Name:           Insert
    //  Description:    Inserts data before the iterator, this works whether the iterator is backwards of forwards
    //                  through the list.Inserts at the end of the list if iterator is invalid.
    //  Arguments:      p_iterator: The iterator to insert before
    //                  p_data: the data to insert
    //  Return Value:   None.
    // ----------------------------------------------------------------------------------------------------------------
    void Insert(DoublyListIterator<Datatype>& p_itr, Datatype p_data)
    {
        if(p_itr.m_node != 0)
        {
            // insert the data before the iterator
            p_itr.m_node->InsertBefore(p_data);

            //if the iterator was at the head of the list,
            //reset the head pointer
            if(p_itr.m_node == m_head)
            {
                m_head = m_head->m_prev;
            }
            // increment the count
            m_count++;
        }
        else
        {
            Append(p_data);
        }
    }

    // -------------------------------------------------------------------------------------------------------
    //  Name:           Remove
    //  Description:    Removes the node that the iterator points to, moves iterator forward to the next node.
    //  Arguments:      p_iterator: The iterator to remove
    //                  isForward: Tells which direction the iterator was going through the list
    //  Return Value:   None.
    // -------------------------------------------------------------------------------------------------------
    void Remove(DoublyListIterator<Datatype>& p_itr, bool isForward)
    {
        // temporary node pointer.
        DListNode<Datatype>* newNode;

        // if node is invalid, do nothing.
        if(p_itr.m_node == 0)
            return;

        // save the pointer to the node we want to delete.
        newNode = p_itr.m_node;

        // if the node we want to remove is the head or the tail
        // nodes, then move the head or tail to the next or
        // previous node.
        if(newNode == m_head)
        {
            m_head = m_head->m_next;
        }
        else if(newNode == m_tail)
        {
            m_tail = m_tail->m_prev;
        }
        // if moving from head to tail, move the iterator forward 
        // to the next valid node
        if(isForward == true)
        {
            p_itr.Forth();

        }
        //else, we are moving from tail to head, so move the iterator
        // to the previous valid node
        else
        {
            p_itr.Back();
        }

        if(newNode->m_prev != 0)
            newNode->m_prev->m_next = newNode->m_next;
        else if(newNode->m_next != 0)
            newNode->m_next->m_prev = newNode->m_prev;
        // delink and delete the node.
        delete newNode;

        // if the head is 0, then set the tail to 0 as well.
        if(m_head == 0)
            m_tail = 0;

        m_count--;


    }
    // -----------------------------------------------------------------------------------
    //  Name:           RemoveHead
    //  Description:    Removes the Head of the List and points to the New Node
    //  Arguments:      None
    //  Return Value:   None.
    // -----------------------------------------------------------------------------------
    void RemoveHead()
    {
        DListNode<Datatype>* newNode = 0;
        if(m_head!= 0)
        {
            // make node point to the next node.
            newNode = m_head->m_next;
            // then delete the head and make the pointer
            // point to node.
            delete m_head;
            m_head= newNode;
            // if the head is null, then you’ve just deleted the only node
            // in the list. set the tail to 0.
            if(m_head== 0)
                m_tail= 0;
            m_count--;
        }
    }

    // -----------------------------------------------------------------------------------------
    //  Name:           RemoveTail
    //  Description:    The tail node is removed - but at a much slower pace than the head node, 
    //  this is because the node has to search through the list to find the node before the tail
    //  node and set it to the new tail node then deleting the tail node
    //  Arguments:      None.
    //  Return Value:   None.
    // -----------------------------------------------------------------------------------------
    void RemoveTail()
    {
        DListNode<Datatype>* newNode = m_head;
        // if the list isn’t empty, then remove a node.
        if(m_head!= 0)
        {
            // if the head is equal to the tail, then
            // the list has 1 node, and you are removing it.
            if(m_head == m_tail)
            {
                // delete the node and set both pointers
                // to 0.
                delete m_head;
                m_head= m_tail= 0;
            }
            else
            {
                // skip ahead until you find the node
                // right before the tail node
                while(newNode->m_next!= m_tail)
                    newNode = newNode->m_next;
                // make the tail point to the node before the
                // current tail and delete the old tail.
                m_tail= newNode;
                delete newNode->m_next;
                newNode->m_next= 0;
            }
            m_count--;
        }
    }

    // ------------------------------------------------------------------------------
    //  Name:           GetIterator
    //  Description:    Generates an iterator pointing towards the current head node
    //  Arguments:      None.
    //  Return Value:   <Datatype> Iterator
    // ------------------------------------------------------------------------------
    DoublyListIterator<Datatype> getIterator()
    {
        return DoublyListIterator<Datatype>(this, m_head);
    }
};
// ----------------------------------------------------------
// Name:        DoublyListIterator
// Description: This is the basic linked list iterator class.
// ----------------------------------------------------------
template<class Datatype>
class DoublyListIterator
{
public:
    // ----------------------------------------------------------------
    //  Name:           m_node
    //  Description:    pointer to the current node
    // ----------------------------------------------------------------
    DoublyListNode<Datatype>* m_node;

    // ----------------------------------------------------------------
    //  Name:           m_list
    //  Description:    pointer to the current list.
    // ----------------------------------------------------------------
    DoublyLinkedList<Datatype>* m_list;

    // -----------------------------------------------------------------------------------------
    //  Name:           DListIterator
    //  Description:    Constructor, creates an iterator that points to the given list and node.
    //  Arguments:      p_list: pointer to the list the iterator belongs to.
    //                  p_node: pointer to the current node.
    //  Return Value:   None.
    // -----------------------------------------------------------------------------------------
    DoublyListIterator(DoublyLinkedList<Datatype>* p_list= 0, DoublyListNode<Datatype>* p_node= 0)
    {
        m_list= p_list;
        m_node= p_node;
    }

    // ------------------------------------------------------------------
    //  Name:           Start
    //  Description:    Resets the iterator to the beginning of the list.
    //  Arguments:      None.
    //  Return Value:   None.
    // ------------------------------------------------------------------
    void Start()
    {
        if(m_list!= 0)
            m_node= m_list->m_head;
    }

    // ----------------------------------------------------------------
    //  Name:           End
    //  Description:    Resets the iterator to the end of the list
    //  Arguments:      None.
    //  Return Value:   None.
    // ----------------------------------------------------------------
    void End()
    {
        if(m_list!= 0)
            m_node = m_list->m_tail;
    }

    // ----------------------------------------------------------------
    //  Name:           Forth
    //  Description:    Moves the iterator forward by one position
    //  Arguments:      None.
    //  Return Value:   None.
    // ----------------------------------------------------------------
    void Forth()
    {
        if(m_node!= 0)
            m_node= m_node->m_next;
    }

    // ----------------------------------------------------------------
    //  Name:           Back
    //  Description:    Moves the iterator backward by one position.
    //  Arguments:      None.
    //  Return Value:   None.
    // ----------------------------------------------------------------
    void Back()
    {
        if(m_node!= 0)
            m_node = m_node->m_prev;
    }

    // ----------------------------------------------------------------
    //  Name:           Item
    //  Description:    Gets the item that the iterator is pointing to.
    //  Arguments:      None.
    //  Return Value:   Reference to the data in the node.
    // ----------------------------------------------------------------
    Datatype& Item()
    {
        return m_node->m_data;
    }

    // ----------------------------------------------------------------
    //  Name:           Valid
    //  Description:    Determines if the node is valid.
    //  Arguments:      None.
    //  Return Value:   true if valid
    // ----------------------------------------------------------------
    bool Valid()
    {
        return (m_node!= 0);
    }
    // ----------------------------------------------------------------
    //  Name:           operator==
    //  Description:    Determines if two iterators point to the same node.
    //  Arguments:      None.
    //  Return Value:   true if they point to the same node.
    // ----------------------------------------------------------------
    bool operator==( DoublyListIterator<Datatype>& p_rhs )
    {
        if( m_node == p_rhs.m_node && m_list == p_rhs.m_list )
        {
            return true;
        }
        return false;
    }
};
#endif

If anymore code is needed just ask. I do not want to put lots of code on Stack overflow users.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T05:12:33+00:00Added an answer on June 10, 2026 at 5:12 am

    Did you mean to name your class DoublyListNode instead of DListNode?

    You forward declare

    template<class Datatype> class DoublyListNode;
    

    but you define

    template<class Datatype>
    class DListNode
    

    and use the members

    DListNode<Datatype>* m_head;
    DListNode<Datatype>* m_tail;
    

    in the list, but the iterator takes parameters:

    DoublyListIterator(DoublyLinkedList<Datatype>* p_list= 0, DoublyListNode<Datatype>* p_node= 0)
    

    To recap, the member in DoublyLinkedList is a DListNode, but the constructor for DoublyListIterator expects a DoublyListNode.

    Pick one name and stick with it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Making a server in java, First off, Here's the code. AnsiConsole.out.println(This is a Test.);
Im making a doublyLinkedList. The error is to do with my Remove method. I
Making a piano/keyboard application and trying to figure out the best way to set
making a call to a web service method with nusoap returns an error array(3)
Making a simple application, so when the user logs out of Windows, it of
Making my first steps in RIA Services (VS2010Beta2) and i encountered this problem: created
Making a new shooter game here in the vein of Galaga (my fav shooter
Making an Flex App. Just wondering if anyone has created something that fits automatically
Does making a class field volatile prevent all memory visibility issues with it in
Making sense out of an .MSI verbose trace. I created the .MSI using VisualStudio

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.