Like the title says I'm experiencing an infinite loop in my doubly linked list. Using gdb I've found the infinite loop to be in my size() function and walking through the main i've seen that the first time insert_after is called is when the loop begins.
I have been working on it for sometime but I can't seem to find out where the error lies in my insert_after function or why it would be causing the size function to loop infinitely.
#include <iostream>
#include <fstream>
#include "dlist.h"
#include "swatch.h"
#include "node_iterator.h"
usingnamespace std;
int main()
{
dlist<Swatch> swatches;
dlist<Swatch>::iterator it;
ifstream fin;
fin.open("swatches.txt");
if (fin.fail())
{
cout << "Could not open input file." << endl;
return 1;
}
Swatch tmp;
while (fin >> tmp)
{
int red = tmp.get_red();
int green = tmp.get_green();
int blue = tmp.get_blue();
if ( (red>=green) && (red>=blue) ) // red is dominant
{
swatches.front_insert(tmp);
}
elseif ( (green >= red) && (green >= blue) )
// green is dominant
{
swatches.rear_insert(tmp);
}
else // blue is dominant
{
it = swatches.begin();
for(int i = 0;i < swatches.size()/2;i++)
++it; // loop moves iterator to the middle
if(swatches.size()%2 == 1){
if(swatches.size()>2){
it++;
}
swatches.insert_before(it,tmp);
}
else{
swatches.insert_after(it,tmp);
}
}
}
fin.close();
dlist<Swatch> copy(swatches); // make a copy
// remove the front, back, and centermost swatch from the copy
copy.front_remove();
copy.rear_remove();
it = copy.begin();
for(int i =0; i < copy.size()/2; ++i)
++it;
if(copy.size()%2 ==1) ++it; // if list has a true middle
// step up into it
copy.remove(it);
// output the original list frontwards
for (dlist<Swatch>::iterator i=swatches.begin(); i != swatches.end(); ++i)
{
cout << *i << endl;
}
cout << endl << endl; // some space
// output the copy frontwards
for (dlist<Swatch>::iterator i=copy.begin(); i != copy.end(); ++i)
{
cout << *i << endl;
}
cout << endl << endl; // some space
// output the original backwards
for (dlist<Swatch>::iterator i=swatches.r_begin(); i != swatches.r_end(); --i)
{
cout << *i << endl;
}
cout << endl << endl; // some space
// destroy the original list by alternating between removal of first and
// last items. Print each item as it is removed
int counter=0;
while (swatches.size() > 1)//0
{
if (swatches.begin() != NULL)
cout<<*swatches.begin()<<endl;
swatches.front_remove();
if(swatches.r_begin() != NULL)//if(swatches.size() > 0){
cout<<*swatches.r_begin()<<endl;
swatches.rear_remove();
}
cout << endl << endl; // some space
// output the copy backwards
for (dlist<Swatch>::iterator i=copy.r_begin(); i != copy.r_end(); --i)
{
cout << *i << endl;
}
return 0;
}
I am probably out of my depth here, but who knows these ideas might be useful :+)
So it is Swatch::size() that causes the infinite loop, not dlist::size() ? If you are using the debugger, you should be able to see why it is not meeting the end condition?
Are the tail pointers always guaranteed to to be null? Should use C++11's nullptr instead of NULL
When doing an elseif chain, always put an else with some kind of warning - maybe a static_assert or even a std::cout if that is feasible. So this will catch anything not matching the other else if's . Edit: The insert_before and insert_after functions don't have this
Are you allowed to use smart pointers? They are better than using new - which should probably be avoided like the plague :+)