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

Trying to Sort a Linked List only by Manipulating Pointers

本文关键字:链表 排序 指针 操作      更新时间:2023-10-16

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

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 行:return head;

在一个应该返回布尔值的函数中

似乎您正在按值传递 n。 如果需要修改函数中 n 的值,则需要将其设为全局 (argh) 或传递 n 的地址:

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

单链表,还是双链表? 你提到只交换数据,但你没有提供指针定义(仅键,还是键和数据指针?),

如果要交换两个节点的内容,则需要在 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;
}

这是带有两个指针的节点交换,

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