尝试仅通过操作指针对链接列表进行排序

时间:2013-10-24 21:48:05

标签: c++ sorting pointers linked-list selection

我正在尝试使用“选择排序”对“链接”列表进行排序。我只能操纵链表指针而不能更改键。我想我有功能逻辑但是,我只是返回原始的未排序序列。

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

3 个答案:

答案 0 :(得分:0)

尝试编写编译和/或询问特定问题的代码。

第3行:return head;

在一个应该返回布尔值

的函数中

答案 1 :(得分:0)

好像你正在按价值传递n。如果你需要在函数内修改n的值,你需要使它成为全局(argh)或传递n的地址:

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

答案 2 :(得分: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;
}

如果你想交换整个节点,那么你需要提供以前的指针,或者找到它们(下面假定一个双向链表,或者你看到'prev'你会发现它),

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

这是带有两个指针的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;
}