c circular double linked-list:遍历末尾节点的fwd / rev给出不同的指针地址

时间:2014-03-25 00:21:19

标签: c iterator doubly-linked-list

相关帖子:c circular double linked-list delete_node - iterate traverses deleted node on first pass after delete

全部,实施搜索节点连词行号' x'在删除该节点之前,我遇到了一个问题,即前向搜索和反向搜索都识别出正确的节点,但是反向搜索报告的呼叫者节点地址的指针与前向节点的报告不同?这仅适用于最后一个节点(最高行号)。如果仅使用转发搜索(pba_fwd_iter_test),则会正确删除最后一个节点。但是,如果使用反向搜索(pba_rev_iter_test),那么由"(victim-> next) - > prev = victim-> prev;"设置的地址是不正确的,它设置"(受害者 - >下一个) - > prev =(victim-> next) - > prev"。例如,使用反向搜索到达终端节点然后执行delete_node会导致以下结果:

49: 7 - (line to delete) This is a line of text that is somewhere around 50 to 80 characters in length

48 - prev: 0x604a80  cur: 0x604b10  next: 0x604ba0
49 - prev: 0x604b10  cur: 0x604ba0  next: 0x603010  <-- delete_node
 0 - prev: 0x604ba0  cur: 0x603010  next: 0x6030a0

48 - prev: 0x604a80  cur: 0x604b10  next: 0x603010
49 - prev: 0x604b10  cur: 0x604ba0  next: 0x603010  <-- (node deleted)
 0 - prev: 0x603010  cur: 0x603010  next: 0x6030a0
              \_______________\______ Error (should be prev: 0x604b10)

@WhosCraig慷慨地帮助了delete_node函数,它运行正常,但是我无法弄清楚为什么在delete_node中使用反向搜索结果定位同一节点时无法设置&#34;(victim-&gt; next) - &gt ; prev = victim-&gt; prev;&#34;正常。作为反向搜索的测试,我只是将一个额外的节点向前推进,然后将一个节点转发回相关节点,然后delete_node工作正常。 (只是一个额外的:list =&amp;(* list) - &gt; prev; list =&amp;(* list) - &gt; next;所以这个问题与到达终端节点时的指针状态有关反向搜索而不是前向搜索 - 这就是我需要帮助搞清楚。这是正向和反向搜索后指针地址的输出,以及快速 - &gt; prev - &gt;下一步:

=========== pba_fwd_iter_test() ===========
passing list = &(*list)->next to tstpptr (0x605b28)
tstpptr(): list     : 0x605b28
tstpptr(): &list    : 0x7ffff14633a8
tstpptr(): *list    : 0x605ba0
tstpptr(): &(*list) : 0x605b28  <- caller's address reported
tstpptr(): &(**list): 0x605ba0     with forward search

tstpptr(): &(*list)->next : 0x605bb8

=========== pba_rev_iter_test() ===========
passing list = &(*list)->next to tstpptr (0x604020)
tstpptr(): list     : 0x604020
tstpptr(): &list    : 0x7ffff14633a8
tstpptr(): *list    : 0x605ba0
tstpptr(): &(*list) : 0x604020  <- caller's address reported
tstpptr(): &(**list): 0x605ba0     with reverse search

tstpptr(): &(*list)->next : 0x605bb8

passing list = &(*list)->next to tstpptr (0x605b28)
tstpptr(): list     : 0x605b28
tstpptr(): &list    : 0x7ffff14633a8
tstpptr(): *list    : 0x605ba0
tstpptr(): &(*list) : 0x605b28  <- caller's address reported after
tstpptr(): &(**list): 0x605ba0     &(*list)->prev; &(*list)->next

tstpptr(): &(*list)->next : 0x605bb8

以下是相关代码片段,其中包含指向完整源代码的链接。感谢您提供的任何帮助:

/*
full source: http://www.3111skyline.com/dl/dev/prg/src/ll-double-cir-1.c.txt
*/

struct record
{
char *line;
int lineno;
int linetype;
struct record *prev;
struct record *next;
};
typedef struct record rec;

void  // traverse in fwd direction to find hightest line no.
pba_fwd_iter_test (rec **list, int num);
void  // traverse in rev direction to find hightest line no.
pba_rev_iter_test (rec **list, int num);
void  // dump the pointers for examination
tstpptr (rec **list);

int main (int argc, char *argv[]) {
// <snip> fill struct with 50 records for testing (lineno '0' based 0-49)
pba_fwd_iter_test (&textfile, 49);
pba_rev_iter_test (&textfile, 49);
return 0;
}

void
pba_fwd_iter_test (rec **list, int num) {
printf ("=========== %s() ===========\n",__func__);
int linemax = getmaxline (*list);
int iterno = 0;
while (((*list)->lineno != num) && (iterno <= linemax)) {
    iterno++;
    list = &(*list)->next;
}
printf ("passing list = &(*list)->next to tstpptr (%p)\n", list);
tstpptr (list);
}

void
pba_rev_iter_test (rec **list, int num) {
printf ("=========== %s() ===========\n",__func__);
int linemax = getmaxline (*list);
int iterno = 0;
while (((*list)->lineno != num) && (iterno <= linemax)) {
    iterno++;
    list = &(*list)->prev;
}
printf ("passing list = &(*list)->next to tstpptr (%p)\n", list);
tstpptr (list);
// increment prev then next and check ptr values again
list = &(*list)->prev;
list = &(*list)->next;
printf ("passing list = &(*list)->next to tstpptr (%p)\n", list);
tstpptr (list);
}

void
tstpptr (rec **list) {
fprintf (stdout, "%s(): list      : %p\n", __func__, list);
fprintf (stdout, "%s(): &list     : %p\n", __func__, &list);
fprintf (stdout, "%s(): *list     : %p\n", __func__, *list);
fprintf (stdout, "%s(): &(*list)  : %p\n", __func__, &(*list));
fprintf (stdout, "%s(): &(**list) : %p\n\n", __func__, &(**list));
fprintf (stdout, "%s(): &(*list)->next : %p\n\n", __func__, &(*list)->next);
}

2 个答案:

答案 0 :(得分:0)

我想我看到了问题 - 我认为没有问题。重要的值是*list,在所有情况下都是相同的。我认为打印列表和&amp; list等只会让问题蒙上阴影。

在前向迭代器中,list指向项目#48的next变量的位置。

在你的后向迭代器中,list指向项目#0的prev变量的位置。

在这两种情况下,* list都指向正确的项目#49。

如果他们只使用rec *而不是rec **,那么这两个函数会简单得多,那么获取list变量的地址会更加明显你想要什么。

答案 1 :(得分:0)

我从3111skyline.com网站上获取了您的代码,但它根本没有调用删除功能(或许多其他功能)。我添加了一个函数validate_list(),它确认您的列表构造正确。我创建了一个循环来删除列表中的行,如下所示:

static void validate_list(rec *list)
{
    assert(list != 0);
    assert(list->next != 0);
    assert(list->prev != 0);
    rec *iter = list;
    do
    {
        assert(iter != 0);
        assert(iter->next != 0);
        assert(iter->prev != 0);
        assert(iter == iter->next->prev);
        assert(iter == iter->prev->next);
        printf("Node: %p (n = %p; p = %p) %2d [%s]\n",
               (void *)iter, (void *)iter->next,
               (void *)iter->prev, iter->lineno, iter->line);
        iter = iter->next;
    } while (iter != list);
}

int main(void)
{
    rec *textfile = fillrecord();
    validate_list(textfile);

    for (int i = 0; i < 49; i++)
    {
        delete_node(&textfile, (i * 13 + 31) % (50 - i));
        validate_list(textfile);
    }
    return 0;
}

这对我来说似乎都行得通。下一步是用随机数替换表达式生成的确定性(但不是特别明显)序列,以检查它是否也能正常工作,但代码对我来说似乎很干净。