2 Replies - 323 Views - Last Post: 03 November 2011 - 01:13 PM Rate Topic: -----

#1 neato0z  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 47
  • Joined: 29-August 11

for loop task question

Posted 03 November 2011 - 02:32 AM

For starters im use to simple for / while loops and I encountered one Tuesday that needs a little clarity. I emailed my instructor yesterday morning and have yet to get a reply. And in all fairness to her she has a 48hr window for responses, yes the dreaded online learning. But I needing a little help with the listDifference function section of this code.


template <class Type>
void listDifference(linkedListType<Type>& diff,
                    linkedListType<Type>& list1,
                    linkedListType<Type>& list2);

template <class Type>
void mergeLists(linkedListType<Type>& list1,
                linkedListType<Type>& list2);

int main(int argc, char **argv)
{
   // Declare list variables
   linkedListType<int> list1;
   linkedListType<int> list2;
   linkedListType<int> diff;

   // Add some data to list 1
   list1.insertLast(2);
   list1.insertLast(3);
   list1.insertLast(4);
   list1.insertLast(5);
   list1.insertLast(6);
   list1.insertLast(7);

   // Add some data to list 2
   list2.insertLast(8);
   list2.insertLast(5);
   list2.insertLast(7);
   list2.insertLast(2);
   list2.insertLast(13);
   list2.insertLast(34);

   cout << "\nList 1:\n   ";
   list1.print();

   cout << "\nList 2:\n   ";
   list2.print();

   listDifference(diff, list1, list2);
   cout << "\nList 1 and List 2 difference:\n   ";
   diff.print();

   mergeLists(list1, list2);
   cout << "\nMerged List 1 with List 2:\n   ";
   list1.print();

   cout << "\n\n** Press any key to continue **\n";
   getchar();

   return 0;
}

template <class Type>
void listDifference(linkedListType<Type>& diff,
                    linkedListType<Type>& list1,
                    linkedListType<Type>& list2)
{
    linkedListIterator<int> itr1;                 
    linkedListIterator<int> itr2;                 

    for (itr1 = list1.begin(); itr1 != list1.end(); ++itr1)   
    {
       // Add the each element in list 1 to the difference list
       diff.insertLast(*itr1);

       for (itr2 = list2.begin(); itr2 != list2.end(); ++itr2)
       {
          if (*itr1 == *itr2)
          {
              // If the node is in both lists delete the node from the
              // difference list.
              diff.deleteNode(*itr1);
          }
       }
    }

    return;


I omitted the merglist function because i dont need any help with that. But if someone could help me understand the pointers a little more (*itr and *itr2) it would be greatly appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: for loop task question

#2 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 687
  • View blog
  • Posts: 2,376
  • Joined: 31-December 10

Re: for loop task question

Posted 03 November 2011 - 07:13 AM

They're not pointers, they are iterators, but they act somewhat like pointers. Say you have an iterator "it", when you say "*it", you're just asking for the value of that iterator. The '*' in this context is the dereference operator. The prefix increment operator(++it) just moves the iterator to the next element. The class member function end() returns an iterator to one-past-the-end of the list, so when you increment past the last element then the iterator will equal what end() returns and exit the loop.
Was This Post Helpful? 0
  • +
  • -

#3 neato0z  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 47
  • Joined: 29-August 11

Re: for loop task question

Posted 03 November 2011 - 01:13 PM

Thanks vividexstance. I was actually liking c++ until this data structures and algorithms class. Its alot that im not grasping it seems.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1