- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
需要帮助,请需要帮助,请我的代码编译得很好,但是当我运行时它给出了一个段错误,我不知道为什么。我已经检查了代码很多次,但仍然不知道问题出在哪里。请需要帮助。
void insertionSort(listNode ** listPtr, listNodeCompareFcn compare)
{
listNode * sorted = *listPtr;
listNode * swapper;
listNode * prev;
int swapped;
while(sorted != NULL)
{
swapper = sorted -> next;
prev = findprev(*listPtr, swapper);
swapped = 0;
while(swapper != NULL && prev != NULL && swapper != *listPtr && compare(swapper,prev))
{
swapNodes(*listPtr, prev, swapper);
prev = findprev(*listPtr, swapper);
swapped = 1;
}
if (!swapped) sorted = sorted -> next;
}
}
static listNode * findprev(listNode * head, listNode * ptr){
listNode * current = head;
while (current -> next != NULL){
if ((current -> next) == ptr) return current;
current = current -> next;
}
return NULL;
}
void swapNodes(listNode * head, listNode * l1, listNode * l2){
listNode * prev = findprev(head, l1);
prev -> next = l2;
l1 -> next = l2 -> next;
l2 -> next = l1;
}
最佳答案
我评论过:
I've done enough work on an SSCCE to know that a major part of your problem is that you are not being careful enough with the head of your list. If you swap the head node and the next node, you need to update the head node pointer, as otherwise you lose the front of the list. I'm tolerably certain that's only a component of the trouble [it was in fact the whole trouble]. Given a valid list, the
findprev()
function works fine. NeitherswapNodes()
norinsertionSort()
can be given a clean bill of health (yet!), I think. For instance, iffindprev()
returns NULL,swapNodes()
happily dereferences the NULL pointer; crash!
这是您代码的 SSCCE 版本,已使用有关修复上述评论中的 swapNodes()
的反向 channel 信息进行了更新。 compare()
函数实际上是一个 C++ 风格的比较器,而不是 C 风格的比较器;也就是说,如果节点 1 位于节点 2 之前,则返回 true,否则返回 false(而 C 风格比较器返回 -1、0、+1 — 或严格来说,负数、零、正数 — 对于小于、等于、大于)。只需修复 swapNodes()
(接口(interface)和代码更改)以及正确的比较器语义,列表即可正确排序。这不是一个详尽的测试,但却是一个好的开始。
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct listNode listNode;
struct listNode
{
int datum;
listNode *next;
};
typedef int (*listNodeCompareFcn)(const listNode *n1, const listNode *n2);
//static void swapNodes(listNode *head, listNode *l1, listNode *l2);
static void swapNodes(listNode **head, listNode *l1, listNode *l2);
static listNode *findprev(listNode *head, listNode *ptr);
static int node_compare(const listNode *n1, const listNode *n2) // SSCCE
{
assert(n1 != 0 && n2 != 0);
printf("Compare: %2d and %2d\n", n1->datum, n2->datum);
if (n1->datum < n2->datum)
return 1;
// if (n1->datum < n2->datum)
// return -1;
// else if (n1->datum > n2->datum)
// return +1;
else
return 0;
}
static void insertionSort(listNode **listPtr, listNodeCompareFcn compare)
{
listNode *sorted = *listPtr;
while (sorted != NULL)
{
listNode *swapper = sorted->next;
listNode *prev = findprev(*listPtr, swapper);
int swapped = 0;
while (swapper != NULL && prev != NULL && swapper != *listPtr && compare(swapper, prev))
{
//swapNodes(*listPtr, prev, swapper);
swapNodes(listPtr, prev, swapper);
prev = findprev(*listPtr, swapper);
swapped = 1;
}
if (!swapped)
sorted = sorted->next;
}
}
static listNode *findprev(listNode *head, listNode *ptr)
{
listNode *current = head;
assert(current != 0);
while (current->next != NULL)
{
if (current->next == ptr)
return current;
current = current->next;
}
return NULL;
}
// Update via email
void swapNodes(listNode **listPtr, listNode *l1, listNode *l2)
{
listNode *prev = findprev(*listPtr, l1);
if (prev == NULL)
{
l1->next = l2->next;
*listPtr = l2;
l2->next = l1;
}
else
{
prev->next = l2;
l1->next = l2->next;
l2->next = l1;
}
}
/*
static void swapNodes(listNode *head, listNode *l1, listNode *l2)
{
listNode *prev = findprev(head, l1);
prev->next = l2;
l1->next = l2->next;
l2->next = l1;
}
*/
static listNode *node_insert(listNode *head, int datum) // SSCCE
{
listNode *node = malloc(sizeof(*node));
node->datum = datum;
node->next = head;
return node;
}
static void print_list(const char *tag, const listNode *list) // SSCCE
{
printf("%-8s", tag);
while (list != 0)
{
printf(" -> %2d", list->datum);
list = list->next;
}
putchar('\n');
}
int main(void) // SSCCE
{
static const int unsorted[] = { 29, 3, 14, 2, 91, 87, 13, 29, 1 };
enum { NUM_VALUES = sizeof(unsorted) / sizeof(unsorted[0]) };
listNode *head = 0;
print_list("Empty:", head);
for (int i = 0; i < NUM_VALUES; i++)
{
head = node_insert(head, unsorted[i]);
print_list("Added:", head);
}
for (listNode *curr = head; curr != 0; curr = curr->next)
{
listNode *prev = findprev(head, curr);
if (prev == 0)
printf("%2d - no prior node\n", curr->datum);
else
printf("%2d - prior node %2d\n", curr->datum, prev->datum);
}
print_list("Before:", head);
insertionSort(&head, node_compare);
print_list("After:", head);
return 0;
}
SSCCE 的输出(包括诊断):
Empty:
Added: -> 29
Added: -> 3 -> 29
Added: -> 14 -> 3 -> 29
Added: -> 2 -> 14 -> 3 -> 29
Added: -> 91 -> 2 -> 14 -> 3 -> 29
Added: -> 87 -> 91 -> 2 -> 14 -> 3 -> 29
Added: -> 13 -> 87 -> 91 -> 2 -> 14 -> 3 -> 29
Added: -> 29 -> 13 -> 87 -> 91 -> 2 -> 14 -> 3 -> 29
Added: -> 1 -> 29 -> 13 -> 87 -> 91 -> 2 -> 14 -> 3 -> 29
1 - no prior node
29 - prior node 1
13 - prior node 29
87 - prior node 13
91 - prior node 87
2 - prior node 91
14 - prior node 2
3 - prior node 14
29 - prior node 3
Before: -> 1 -> 29 -> 13 -> 87 -> 91 -> 2 -> 14 -> 3 -> 29
Compare: 29 and 1
Compare: 13 and 29
Compare: 13 and 1
Compare: 87 and 29
Compare: 91 and 87
Compare: 2 and 91
Compare: 2 and 87
Compare: 2 and 29
Compare: 2 and 13
Compare: 2 and 1
Compare: 14 and 91
Compare: 14 and 87
Compare: 14 and 29
Compare: 14 and 13
Compare: 3 and 91
Compare: 3 and 87
Compare: 3 and 29
Compare: 3 and 14
Compare: 3 and 13
Compare: 3 and 2
Compare: 29 and 91
Compare: 29 and 87
Compare: 29 and 29
After: -> 1 -> 2 -> 3 -> 13 -> 14 -> 29 -> 29 -> 87 -> 91
关于你能告诉我这有什么问题吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16072950/
为了遵循务实的编程原则,我试图根据“告诉,不要询问”原则来决定如何处理用户密码更改。 我有一个用户对象,其密码每 30 天过期一次。如果密码过期,我需要能够显示密码过期/更改密码 View 。询问对象
我试图在接收文件时绕过任何本地存储。根据documentation ,如果“合理”,Flask 会将文件保存在内存中,否则会将它们存储在临时位置。 我只找到了一种通常使用 MAX_CONTENT_LE
SAS 在 proc 中返回 sci-notation 意味着总和输出,我不能将其用于进一步的速率计算过程。如何抑制 SAS 产生 sci-notation,有什么想法吗?提前致谢。 “解决了一个类似
当使用 nohup 时,脚本的输出会被缓冲,只有在脚本执行完毕后才会转储到日志文件 (nohup.out) 中。以接近实时的方式查看脚本输出以了解其进展情况将非常有用。有没有办法让 nohup 在脚本
假设我们定义了以下路由: const routes: Routes = [ { path: '', component: WelcomeComponent }, {
我正在尝试以下操作: a a > 1 1 > 2 2 > 3 3 我想要的是: a b > 1 1 > 2 2 > 3 3 有没有办法告诉 R 使用存储在对象( "b" )中的字符串( a
我想在安装二进制文件之前使用 automake 处理/修改它们。例如,我想将二进制文件中的符号提取到单独的文件和位置(如 this )。另一个示例是收集关键 Assets 的 md5sum 以发布报告
我的应用程序有一个主要的 pro 文件,我想告诉 qmake 在与应用程序同时编译一个单独的库。该库的目录中还有一个 pro 文件。这可能吗? 最佳答案 将 lib 和应用程序放在单独的子目录中,并使
我的 vimrc 中有以下内容: nnoremap :!screen -S foo -p run -X stuff '!!^M' 但是,当单击 F1 时,出现错误:没有上一个命令。 我想要的
我正在使用 Swagger 和 Scala 来记录我的 REST API。我想为 POST、PUT 和 DELETE 启用批量操作,并希望相同的路由接受单个对象或对象集合作为正文内容。 有没有办法告诉
我有一个 SAS 代码,它为我的计算创建了很多中间表。事情是,我在工作完成后并不真正关心这张 table ,我只关心决赛的结果。 但是,每次我运行这段代码时,SAS 都会添加所有生成的表来做我的流程,
有没有办法告诉 UglifyJS 跳过特定的代码部分,也许使用这样的注释: // uglifyjs:skipStart filter = function(item){ /* some crazy f
在 macOS 上通过 homebrew 安装包时,如果我的网络不稳定并且一次下载失败,homebrew 将下载源并从源开始构建。这将需要很长时间和高 CPU 使用率,这是不需要的。如何在下载失败时告
有没有办法告诉 GORM 不要保留属性?我计划在我的 User 类上定义一个确认密码属性,用于验证,但不应保留。 最佳答案 使用 transient 关键字 GORM 可以指示不持久化特定属性。 以下
我正在为 jQuery 编写一个幻灯片放映应用程序(单击按钮,然后滑动浏览图像列表),但我遇到了一个小错误,它将响应 即使在动画发生时也会发出 click() 请求。我已经在使用 animate()
我可以告诉 Xcode 4 我不在项目中使用自动布局吗? 目前,每个新创建的 xib 都会启用自动布局,这意味着我必须在创建 xib 后手动将其关闭,而我不希望这样。 最佳答案 这是自动布局的问题。您
因此,我正在使用目前手动运行的 AzCopy,但我要通过我们的一台服务器上的任务计划程序来运行它。如果我手动执行批处理文件,这会将文件从一个容器复制到另一个容器,并且可以完美运行。然而,它问我: Ov
我正在 OSX 中编写一个基于文档的应用程序。我发现当我更改文档的内容时,应用程序不知道文档已更改。我可以在没有警告的情况下关闭文档,这会导致我未保存的内容丢失。 如何告诉 NSDocument 文档
根据NSWindow Class Reference ,您应该“很少需要调用”NSWindow 方法“display”或“setViewsNeedDisplay”。那么重新显示窗口内容的常用方法是什么
为了重写开源 iMedia 框架项目(目前有数十名开发人员正在使用),我们正在切换到 IKImageBrowserView,并且在缓存方面遇到了麻烦。 看来 IKImageBrowserView 喜欢
我是一名优秀的程序员,十分优秀!