I have been trying to swap two adjacent nodes for my linked list sort. Its not meant to be optimal, just meant to work. My problem is I either lose nodes or get Access Violation errors, Can anyone help?
PHP Code:
void List::sortList()
{
Node *current = _head;
Node *rightNode = NULL;
if (_head->_data > _head->_next->_data)
{
current = _head;
rightNode = _head->_next;
current->_previous = rightNode;
current->_next = rightNode->_next;
rightNode->_previous = NULL;
rightNode->_next = current;
_head = rightNode;
}
bool swapped = true;
while (swapped)
{
current = _head;
swapped = false;
while (current->_next != NULL)
{
if (current->_next->_data < current->_data)
{
Node *left, *right;
left = current;
right = current->_next;
left->_next = right->_next;
left->_previous->_next = right;
right->_next->_previous = left;
right->_previous = left->_previous;
right->_next = left;
left->_previous = right;
swapped = true;
}
current = current->_next;
}
}
}