Thread: Question on sample code

  1. #1
    Registered User
    Join Date
    Mar 2016
    Posts
    13

    Question on sample code

    hello,

    I was going through the code here to implement an Iterator version of linked list. Implementing Iterator pattern of a single Linked List - GeeksforGeeks

    It looks like when I create another object of type LinkedList and push_back elements into it, the elements get added to the first list. Curious to know if I am missing something. How can we fix this?
    Last edited by livin4th; 03-20-2018 at 09:30 PM.

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    Why do you think that's what would happen?
    Where's your test code?
    A little inaccuracy saves tons of explanation. - H.H. Munro

  3. #3
    Registered User
    Join Date
    Mar 2016
    Posts
    13
    Quote Originally Posted by john.c View Post
    Why do you think that's what would happen?
    Where's your test code?
    I added a few lines to the main to declare a new list2 and pushed 10,20 and 30 to it. I expected list to be still printing out 1, 2 and 3. But I am seeing the output for traverse as 1,2,3,10,20,30.

    below is the code added in main()

    Code:
    //Driver program
    int main()
    {
        LinkedList<int> list;
        LinkedList<int> list2;
    
        // Add few items to the end of LinkedList
        list.push_back(1);
        list.push_back(2);
        list.push_back(3);
    
        list2.push_back(10);
        list2.push_back(20);
        list2.push_back(30);
    
        cout << "Traversing LinkedList through method" << endl;
        list.Traverse();
        cout << endl;
    
        cout << "Traversing LinkedList through Iterator" << endl;
        for (LinkedList<int>::Iterator iterator = list.begin();
            iterator != list.end(); iterator++)
        {
            cout << *iterator << " ";
        }
    
        cout << endl;
    
        return 0;
    }

  4. #4
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    You're right. It's because they made m_spRoot static for some reason.
    Just remove the word static from in front of the declaration of m_spRoot,
    and remove the bit of code after the class that initializes m_spRoot to nullptr.
    Just initialize it to nullptr in the ctor (which is already being done anyway).
    There should probably be a destructor.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  5. #5
    Registered User
    Join Date
    Mar 2016
    Posts
    13
    Quote Originally Posted by john.c View Post
    You're right. It's because they made m_spRoot static for some reason.
    Just remove the word static from in front of the declaration of m_spRoot,
    and remove the bit of code after the class that initializes m_spRoot to nullptr.
    Just initialize it to nullptr in the ctor (which is already being done anyway).
    There should probably be a destructor.
    Thanks for the response. Follow up question:

    How does this work?

    Code:
    // Root of LinkedList wrapped in Iterator type Iterator begin() { return Iterator(m_spRoot); }
    the above call looks like a call to the parameterized constructor which is defined as below
    Code:
    Iterator(const Node* pNode) noexcept : m_pCurrentNode (pNode) { }
    Which means all it does is set m_pCurrentNode to a node*. But the Iterator begin() function expects a return value of type Iterator. We are just setting a private variable value to something and returning void. How is this working? Why isn't the compiler complaining when we are not returning an Iterator?

  6. #6
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    The Iterator constructor isn't returning void. Constructors don't have an explicit return type since they must obviously return themselves so the type is known. And they return themselves automatically when the function returns.
    A little inaccuracy saves tons of explanation. - H.H. Munro

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to get the sample code?
    By krishnampkkm in forum C++ Programming
    Replies: 2
    Last Post: 06-19-2009, 04:41 AM
  2. Linked List Tutorial sample code question
    By Kalleos in forum C Programming
    Replies: 2
    Last Post: 01-16-2009, 12:20 PM
  3. SMS sample code
    By hagenuk in forum C Programming
    Replies: 1
    Last Post: 05-30-2008, 11:47 AM
  4. Sample code please help!!!
    By aewing30 in forum C Programming
    Replies: 6
    Last Post: 05-29-2008, 10:51 AM
  5. is this C++ code sample?
    By chico1001 in forum C++ Programming
    Replies: 18
    Last Post: 03-03-2006, 03:05 PM

Tags for this Thread