1

これが私の小さなバブルソート機能で抱えている問題です。値を並べ替えることはできますが、3ノードの後で常にカットオフが発生します。例は次のとおりです。

以下を並べ替えます。

3 2 54 5 4

私はいつも(最小のものが頭であるから)得ます:

2 3 4

そしてそれだけです、他には何もありません。

 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);
    }
}

}

Line 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);
  }

}

4

1 に答える 1

0

問題はスワップ機能にあります。ノードを削除すると、以前に計算されたノードが無効になる可能性があります

 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 if (n1==prevn2)
  {
       evictNode (head, n1);
       insertNode(head, n2, n1);  
  }
  else if (n2==prevn1)
   {
       evictNode (head, n2);
       insertNode(head, n1, n2);  
   } 
  else {
   evictNode(head, n1);
   evictNode(head, n2);   
   insertNode(head, prevn1, n2);
   insertNode(head, prevn2, n1);
  }
}
于 2013-02-26T10:34:54.543 に答える