Check if the list iterators for upper/lower have reached the end. Then change your logic accordingly such that it can work for the cases when lower or upper are not present.
list<char>::const_iterator i, u, l
u = upper_row.begin();
l = lower_row.begin();
for (i = middle_row.begin(); i != middle_row.end(); ++i)
{
bool has_upper = (u != upper_row.end());
bool has_lower = (l != lower_row.end());
// code to do something with upper, middle and lower lists
// >> only do something with upper/lower if has_upper/has_lower is true
if(has_upper) ++u;
if(has_lower) ++l;
}
Additional note: always use ++i
for iterators, not i++
!