So I'm having a little trouble with trying to make a swap function that can deal with all of the possible circumstances in a doubly linked list (=head&&=tail, =head&& !=tail, etc). I think I have it complete but I wanted to ask if anyone saw problems with it. It compiles fine but I don't have a way to check it at the moment.
void List<T>::reverse( ListNode * & startPoint, ListNode * & endPoint )
{
ListNode * s = startPoint;
ListNode * e = endPoint;
if(startPoint == endPoint){return;}
if(startPoint == NULL || endPoint == NULL){return;}
ListNode * r;
ListNode * x;
r = s->next;
x = e->prev;
if(startPoint == endPoint){return;}
if(startPoint == NULL || endPoint == NULL){return;}
while(s!=e)
{
swap(s,e);
s=r;
e=x;
r = s->next;
x = e->prev;
}
//find the head
while(head->prev != NULL)
{
head = head->prev;
}
//find the tail
while(tail->next != NULL)
{
tail = tail->next;
}
}
//Swap class does the grunt work for reverse. It first checks to see if the nodes
//are usable. Then it checks the conditions of the nodes (head or tail) and swaps
//them according to their condition. This lets reverse only worry about whether
//snode == enode
template <class T>
void List<T>::swap( ListNode * & snode, ListNode * & enode )
{
ListNode * s = snode;
ListNode * e = enode;
ListNode * r;
ListNode * x;
ListNode * y;
ListNode * t;
//Are they the same?
if(s == e) return;
//if given null nodes
if (s == NULL || e == NULL) return;
//if they are head and tail nodes but not next to each other
if(s==head && e==tail && s->next != e->prev)
{
r = s->next;
x = e->prev;
s->next = NULL;
s->prev = x;
e->prev = NULL;
e->next = r;
}
else
//if nodes are next to each other
if(s->next == e->prev)
{
if(s!=head && e!=tail)
{
t = s->prev;
y = e->next;
s->next = y;
s->prev = e;
e->prev = t;
e->next = s;
}
elseif(s==head && e!=tail)
{
y = e->next;
s->next = y;
s->prev = e;
e->prev = NULL;
e->next = s;
}
elseif(s!=head && e==tail)
{
t = s->prev;
s->next = NULL;
s->prev = e;
e->prev = t;
e->next = s;
}
elseif(s==head && e==tail)
{
s->next = NULL;
e->prev= NULL;
e->next=s;
s->prev=e;
}
}
else
//if not heads or tails and not next to eachother. middle of list.
if(s !=head && e != tail && s->next != e->prev)
{
t = s->prev;
r = s->next;
x = e->prev;
y = e->next;
t->next = e;
r->prev = e;
e->prev = t;
e->next = r;
x->next = s;
y->prev = s;
s->next = y;
s->prev = x;
}
else
//if head but not tails and not next to each other
if(s==head && e!=tail && s->next != e->prev)
{
r = s->next;
x = e->prev;
y = e->next;
s->prev = x;
s->next = y;
e->prev = NULL;
e->next = r;
}
else
//if not head but is tail and not next to each other
if(s!=head && e==tail && s->next != e->prev)
{
r = s->next;
x = e->prev;
t = s->prev;
s->prev = x;
s->next = NULL;
e->prev = t;
e->next = r;
}
}
I understand it isn't built for speed or efficiency. I just want to make sure I have the general idea. Perhaps I can go back and try to use few pointers when I have time.
I don't even know what to say. It never occurred to me to even think about the data. I was just thinking of the nodes. All this time I've wasted trying to think of ways to do this..