السلام عليكم ورحمة الله وبركاته
كتبت دالة ال Quick Sort ل Singly Linked List واشتغلت تمام 100% ، ال List تأخذ int ( قائمة ارقام ) ودالة الترتيب ترتبها من الاصغر الى الأكبر تصاعدياً :
Node* LinkedList::partition( Node* low, Node* high )
{
Node* left;
Node* right;
Node* pivot;
int pivotValue;
pivotValue = low -> data;
left = low;
pivot = left;
right = high;
while ( getIndex( left ) < getIndex( right ) )
{
while ( left -> data <= pivotValue && getIndex( left ) <= getIndex( high ) )
left = left -> next;
while ( right -> data > pivotValue && getIndex( right ) >= getIndex( low ) )
right = getPrev( right );
if ( getIndex( left ) < getIndex( right ) )
swap( left, right );
}
low -> data = right -> data;
right -> data = pivotValue;
return right;
}
void LinkedList::quickSort( Node* first, Node* last )
{
Node* pivot;
if ( getIndex( last ) > getIndex( first ) )
{
pivot = partition( first, last );
quickSort( first, getPrev( pivot ) );
quickSort( pivot -> next, last );
}
}وهذي هي الدوال المساعدة وهي getPrev و swap و getIndex
void LinkedList::swap( Node* n1, Node* n2 )// swap data only
{
int tempValue;
tempValue = n1 -> data;
n1 -> updateData( n2 -> data );
n2 -> updateData( tempValue );
}
Node* LinkedList::getPrev( Node* currentPtr )
{
Node* current = firstPtr;
while ( current != NULL )
{
if ( current -> next == currentPtr )
break;
current = current -> next;
}
return current;
}
int LinkedList::getIndex( Node* sd )
{
Node* current = firstPtr;
int count = 0;
while ( current != NULL )
{
if ( ( current -> data ) == ( sd -> data ) )
break;
count++;
current = current -> next;
}
return count;
}السؤال او الطلب هو أني أريد ان اجعل دالة الترتيب تقوم بالترتيب من الأكبر الى الأصغر تنازلياً ، قمت ببعض التغييرات في الكود لكن ظهرت لي اخطاء في المؤشرات Segmentation fault ، والذي قمت بتغييره فقط إشارة الأكبر والأصغر وتحديداً هذا الكود :
while ( getIndex( left ) < getIndex( right ) )
{
while ( left -> data > pivotValue && getIndex( left ) <= getIndex( last ) )
left = left -> next;
while ( right -> data <= pivotValue && getIndex( right ) >= getIndex( first ) )
right = getPrev( right );
if ( getIndex( left ) < getIndex( right ) )
swap( left, right );
}وهو موجود داخل دالة partition .
ارجو ان يساعدني أحد في حل هذه المشكلة وشكراً :)