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