尝试仅通过操作指针对链表进行排序
Trying to Sort a Linked List only by Manipulating Pointers
我正在尝试使用选择排序来对链表进行排序。我只能操作链表指针,而不能更改键。我认为我有功能逻辑,但是,我只是返回原始的未排序序列。
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;
}
相关文章:
- 对单向链表进行排序时出现运行时错误
- 为什么在排序链表上的这种合并实现总是将两个列表都设置为 NULL,而只有一个应该设置一个列表?
- 按升序对链表进行排序
- 我想在 c++ 中对单向链表进行冒泡排序,但我一直面临左值错误,无法解决它
- 合并链表上的排序
- 为什么链表的重新排序会失败?
- 合并两个排序链表时运行时出错
- 在 C++ 中合并两个排序链表
- 如何从几乎排序的链表中分离放错位置的元素?
- 使用C++中的链表对与原点的距离、x 和 y 坐标进行排序
- 使用运算符+将两个已排序的链表合并到位
- 合并 2 个排序的链表。必需的成员函数 bool Merge(List342 &list1):
- 双链表C++上的插入排序
- 在排序链表中插入结构并将其写入二进制文件
- C++:链表排序
- 链表排序不影响main(C++)中的列表项
- C++链表排序、拆分和打印问题
- 链表排序
- 自定义链表排序方法
- 链表排序方式的不同之处