问题描述
- 这段链表的快速排序的代码哪里出了问题,vs2010上调试不过
-
void myquicksort( node *phead, node *pback )
{if( phead == pback )
{
return;
}
else
{
int key = phead->data ;
node *p1 = NULL;
for( node *p2 = phead->pNext; p2 != pback; p2 = p2->pNext )
{
if( p2->data < key )
{
p1 = p1->pNext;
int temp;
temp = p1->data;
p1->data = p2->data;
p2->data = temp;
}
}
int temp;
temp = p1->data;
p1->data = phead->data;
phead->data = temp;myquicksort( phead, p1 ); myquicksort( p1->pNext, pback );
}
}
解决方案
p1空指针,没指向就使用了。
解决方案二:
void myquicksort(node* &head, node* &end){
node *head1, *head2, *end1, *end2;
head1 = head2 = end1 = end2 = NULL;
if( head == NULL ) return;
node *p, *pre1, *pre2;
p = pre1 = pre2 = NULL;
int key = head->data;
p = head->pNext; head->pNext = NULL;
while( p != NULL ) {
if ( p->data < key ){
if( !head1 ) { head1 = p; pre1 = p; }
else{ pre1->pNext = p; pre1 = p; }
p = p->pNext;
pre1->pNext = NULL;
}
else{
if( !head2 ) { head2 = p; pre2 = p; }
else { pre2->pNext = p; pre2 = p; }
p = p->pNext;
pre2->pNext = NULL;
}
}
end1 = pre1; end2 = pre2;
myquicksort(head1, end1);
myquicksort(head2, end2);
if( end1 && head2){
end1->pNext = head; head->pNext = head2;
head = head1; end = end2;}
else if(end1) {
end1->pNext = head;
end = head; head = head1; }
else if(head2){
head->pNext = head2; end = end2;}
}
解决方案三:
我知道了,漏了一句p1 = phead,p1要指向头指针!
解决方案四:
快速排序(VS2010)的代码
时间: 2024-11-03 04:39:58