1
所以这里是我用我的小泡沫排序功能的问题。我能够对这个值进行排序,但是我总是在3个节点之后得到一个临界值。一个例子是:泡沫排序C - 链接列表
排序如下:
总是得到(从最小的作为头):
就是这样,没有别的。
void sortByLine (struct lnode** head) {
int count = 1;
while(count){
struct lnode *temp =*head;
count = 0;
while(temp != NULL){
struct lnode *next = nodeGetNext(temp);
if(next != NULL){
if((lineCmp(temp,next)) > 0){
swap(head, next,temp);
count = 1;
}
}
temp = nodeGetNext(temp);
}
}
}
行CMP功能:
int lineCmp (struct lnode* n1, struct lnode* n2) {
int node1 = nodeGetLine(n1);
int node2 = nodeGetLine(n2);
if(node1 == node2){
return 0;
}
else if(node1 > node2){
return 1;
}
else
return -1;
}
交换功能:
void swap (struct lnode** head, struct lnode* n1, struct lnode* n2) {
struct lnode *prevn1 = nodeGetPrev(*head, n1);
struct lnode *prevn2 = nodeGetPrev(*head, n2);
struct lnode *nextn1 = nodeGetNext(n1);
struct lnode *nextn2 = nodeGetNext(n2);
if(prevn2 == n1 && prevn1 == NULL){
evictNode(head, n2);
pushNode(head, n2);
}
else if(prevn1 == n2 && prevn2 == NULL){
evictNode(head, n1);
pushNode(head, n1);
}
else if(prevn1 == n2 && nextn1 == NULL){
evictNode(head, n1);
insertNode(head, prevn2 , n1);
}
else if(prevn2 == n1 && nextn2 == NULL){
evictNode(head, n2);
insertNode(head, prevn1, n2);
}
else{
evictNode(head, n1);
evictNode(head, n2);
insertNode(head, prevn2 , n1);
insertNode(head, prevn1 , n2);
}
}
,我们需要看到的交换和linecmp的定义,它看起来像你的arent处理下一个PTR正确 – 2013-02-26 01:29:37
添加的新功能。所以指针不是正确指向下一个节点的指针? – 2013-02-26 01:36:21