2013-10-24 44 views
1

我試圖使用選擇排序來排序鏈接列表。我只能操作鏈接列表指針,而不能更改鍵值。我認爲我有功能邏輯,但是,我只是返回原始的未排序序列。嘗試僅通過操作指針對鏈接列表進行排序

bool nodeSwap(Node* head){ 

    Node* next = head->next; 
    if(next == NULL){ return head;} 
    head->next = next->next; 
    next->next = head; 
    head = next; 
    return next; 
} 
Node* sort_list(Node* head){ 

for(Node* n = head; n->next != NULL; n = n->next){ 
    for(Node* n1 = head->next; n1 != NULL; n1 = n1->next){ 
     if(n-> key > n1->key){ 
      nodeSwap(n); 


      } 
    } 
} 
return head; 
} 

編輯

好了,所以我經歷了和增加了更多的和一些邏輯,實際上有一定道理這段時間,我有我的功能差不多的工作...唯一的問題是它總是跳過列表中的前兩個元素進行排序,排序後不返回。有關爲什麼可能發生的任何想法?

Node* sort_list(Node* head){ 

Node* curr; 
Node* prev; 

    for(curr = head; curr->next != NULL; curr = curr->next){ 
     if(curr == head){ 
      head = curr->next; 
      curr->next = head->next; 
      head->next = curr; 
      prev = head; 
     } 
    else if(curr->key > curr->next->key){ 
        head = curr->next; 
        curr->next = head->next; 
        head->next = curr; 
        prev = head; 
       } else if(curr -> next -> next != NULL){ 

        prev->next = curr->next; 
        curr->next = prev->next->next; 
        prev->next->next = curr; 

        }else if(head != curr){ 
         prev = prev->next; 
        }else{} 
    } 


return head; 
} 
+0

你不能像這樣交換兩個節點。那麼'next'指針指向'head'的節點會傳遞給'nodeSwap'嗎? – paddy

+0

所以你試圖對鏈表進行冒泡排序,對嗎?如果你認爲這是直截了當的,你不妨多想一想。它可以*製作*非常簡單,但你必須深入思考。你真的在改變什麼? – WhozCraig

回答

0

嘗試放置編譯和/或提出具體問題的代碼。

線3:return head;

在功能

這應該返回一個布爾值

0

好像你傳遞N乘值。如果您需要修改一個函數內部n的值,你需要或者使其全球(哎呀)或通過正地址:

bool nodeSwap(Node** head){ 
    [...] 
} 
0

單鏈表,或雙鏈表?你提到只交換數據,但是你不提供指針定義(只有密鑰,或者密鑰&數據指針?),

如果你想交換兩個節點的內容,你需要提供指向兩者的指針在nodeSwap功能節點,

bool nodeSwap(Node* a, node* b) 
{ 
    if(!a) return false; 
    if(!b) return false; 
    int temp = a->key; 
    a->key = b->key 
    b->key = temp; 
    void* dtemp = a->data; 
    a->data = b->data; 
    b->data = dtemp; 
    return true; 
} 

如果你希望交換的整個節點,那麼你需要提供以前的指針,或去找他們(以下假定一個雙向鏈表,或者你看到「下一頁」你去找吧),

bool nodeSwap(Node* a, node* b, Node* head) 
{ 
    if(!a) return false; 
    if(!b) return false; 
    Node* ap=NULL, *bp=NULL; 
    //double linked list 
    ap = a->prev; 
    bp = b->prev; 
    //single linked list, get ap (a.previous), 
    if(a!=head) 
     for(ap=head; ap!=a->next;) 
      ap=np->next; 
    //single linked list, get bp (b.previous) 
    if(b!=head) 
     for(bp=head; bp!=b->next;) 
      bp=bp->next; 
    Node* temp; 
    temp = a; 
    //fix a->prev->next, b->prev->next 
    ap->next = b; //was a 
    bp->next = a; //was b 
    //swap a->next, b->next 
    temp = a->next; 
    a->next = b->next; 
    b->next = temp; 
    //swap a->prev, b->prev for double-linked list 
    temp = a->prev; //double linked list 
    a->prev = b->prev; //double linked list 
    b->prev = temp; //double linked list 
    //swap keys, not needed, you moved the Node* 
    return true; 
} 

d這裏是有兩個指針的nodeSwap,

Node* sort_list(Node* head){ 
    for(Node* n = head; n->next != NULL; n = n->next){ 
     for(Node* n1 = head->next; n1 != NULL; n1 = n1->next){ 
      if(n-> key > n1->key){ 
       nodeSwap(n,n1); 
       //nodeSwap(n,n1,head); 
      } 
     } 
    } 
    return head; 
}