我正在查看来自http://publications.gbdirect.co.uk/c_book/chapter6/structures.html的示例 6.7
(为了方便贴在这里)
struct list_ele *
sortfun( struct list_ele *list )
{
int exchange;
struct list_ele *nextp, *thisp, dummy;
/*
* Algorithm is this:
* Repeatedly scan list.
* If two list items are out of order,
* link them in the other way round.
* Stop if a full pass is made and no
* exchanges are required.
* The whole business is confused by
* working one element behind the
* first one of interest.
* This is because of the simple mechanics of
* linking and unlinking elements.
*/
dummy.pointer = list;
do{
exchange = 0;
thisp = &dummy;
while( (nextp = thisp->pointer)
&& nextp->pointer){
if(nextp->data < nextp->pointer->data){
/* exchange */
exchange = 1;
thisp->pointer = nextp->pointer;
nextp->pointer =
thisp->pointer->pointer;
thisp->pointer->pointer = nextp;
}
thisp = thisp->pointer;
}
}while(exchange);
return(dummy.pointer);
}
我明白了基本的想法,但我无法真正解释那里发生了什么。有人可以更深入但以简单的方式解释该排序功能中发生了什么吗?
一般的一些问题:
- 为什么
dummy.pointer = list;
需要?dummy
然后在函数末尾返回,为什么列表仍然排序? - 评论
The whole business is confused by working one element behind the first one of interest.
是什么意思?