I'm trying to implement a bubble sort on a linked list. However, I get access errors:
Unhandled exception at 0x001A8C7B in program.exe: 0xC0000005: Access violation reading location 0xCCCCCCCC.
This error occur in my bubble sort method:
if (current->data > nextElement->data)
call in main
list1.SortList();
struct
struct IntNode
{
int data;
IntNode * next;
};
bubble sort
void NodeSLList::SortList()
{
if (head == NULL || head->next == NULL)
return;
IntNode * current = head;
IntNode * nextElement = current->next;
IntNode * temp = NULL;
int changed = 1;
while (changed)
{
changed = 0;
for (current; current != NULL; current = current->next)
{
if (current->data > nextElement->data) //ACCESS ERROR
{
temp = current;
current = nextElement;
nextElement = temp;
changed = 1;
}
nextElement = nextElement->next;
}
}
}
I changed the inside of the loop to:
for (current; (current != NULL) && (nextElement = NULL); )
{
if (current->data > nextElement->data)
{
temp = current->next;
current->next = nextElement->next;
nextElement->next = temp;
changed = 1;
}
current = current->next;
nextElement = nextElement->next;
}
However, my list continues to output the same list.