Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
c循环双链表delete_node-在删除后的第一次遍历被删除的节点_C_Iterator_Doubly Linked List - Fatal编程技术网

c循环双链表delete_node-在删除后的第一次遍历被删除的节点

c循环双链表delete_node-在删除后的第一次遍历被删除的节点,c,iterator,doubly-linked-list,C,Iterator,Doubly Linked List,总之,在GNUC中,我有一个循环双链表,我正试图在其上实现一个delete_节点函数。它适用于除节点0以外的所有节点。它确实删除(free())节点0,但在删除节点0后第一次遍历列表时,它仍然存在于第一次遍历中,导致停止迭代的条件失败。实施的基础是: struct record { char *line; int lineno; int linetype; struct record *prev; struct record *next; }; type

总之,在GNUC中,我有一个循环双链表,我正试图在其上实现一个delete_节点函数。它适用于除节点0以外的所有节点。它确实删除(free())节点0,但在删除节点0后第一次遍历列表时,它仍然存在于第一次遍历中,导致停止迭代的条件失败。实施的基础是:

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

typedef struct record rec;

void
iterfwd (rec *list) {

    rec *iter = list;   // second copy to iterate list

    if (iter ==  NULL) {
        fprintf (stdout,"%s(), The list is empty\n",__func__);
    } else {
        do {
            printf ("%2d - prev: %p  cur: %p  next: %p\n", iter->lineno, iter->prev, iter, iter->next);
        iter = iter->next;
        } while (iter != list);
    }
}

void
delete_node (rec *list, int num) {

    rec *iter = list;   // second copy to iterate list
    int cnt = 0;
    int found = 0;

    if (iter ==  NULL) {
        fprintf (stdout,"%s(), The list is empty\n",__func__);
    } else {
        // traverse list forward (check cnt == num, else if end -> Out Of Range)
        do {
            if (cnt == num) {
                found=1;
                (iter->prev)->next = iter->next;
                (iter->next)->prev = iter->prev;
                free (iter);
                break;
            }
            iter = iter-> next;
            cnt++;
            } while (iter != list);

            if (found != 1) {
            fprintf (stderr, "%s(), Error: record to delete is out of range (%d)\n", __func__, num);
        }
    }
}

int main (int argc, char *argv[]) {
    struct record *textfile = NULL; // instance of record, pointer to list
    int node = 0;
    node = (argc >= 2) ? atoi (argv[1]) : 0;
    textfile = fillrecord ();  // fill textfile circular linked-list
    iterfwd (textfile);
    delete_node (textfile, node);
    iterfwd (textfile);
    return 0;
}
完整清单如下:

列表中有50条测试数据记录,我插入了printf语句来确认指针操作。删除除节点0之外的任何节点都能按预期工作(以下是删除节点10时受影响行的iter->prev,iter,iter->next的指针地址[删除前和删除后]:

但是,如果删除节点0,则delete_节点将正确处理指针:

49 - prev: 0x604b10  cur: 0x604ba0  next: 0x603010
 0 - prev: 0x604ba0  cur: 0x603010  next: 0x6030a0  <-- delete_node
 1 - prev: 0x603010  cur: 0x6030a0  next: 0x603130

49 - prev: 0x604b10  cur: 0x604ba0  next: 0x6030a0
 0 - prev: 0x604ba0  cur: 0x603010  next: 0x6030a0  <-- (node deleted)
 1 - prev: 0x604ba0  cur: 0x6030a0  next: 0x603130
49-上一个:0x604b10当前:0x604ba0下一个:0x603010
0-上一个:0x604ba0 cur:0x603010下一个:0x6030a0上一个指向末尾。让delete_node()函数为节点0工作的诀窍是什么?这样,delete之后的第一次迭代从1开始,而不是旧的0然后消失?

当请求调用方指向的节点作为删除请求时,您没有修改调用方的指针。下面是一些代码的简化版本,演示了一种方法:

#include <stdio.h>
#include <stdlib.h>

typedef struct record rec;
struct record
{
    int data;
    rec *prev, *next;
};

void delete_node (rec ** pp, int num)
{
    if (!*pp)
        return;

    // find the num'th node
    while (num-- && *pp)
        pp = &(*pp)->next;

    // setup victim
    rec *victim = *pp;

    // non-self-reference node means just rewire
    if (victim && (victim != victim->next))
    {
        victim->prev->next = victim->next;
        victim->next->prev = victim->prev;
        *pp = victim->next;
    }
    else
    {   // deleted node was self-referenced. last node
        *pp = NULL;
    }
    free(victim);
}

void iterfwd(const rec* list)
{
    const rec *p = list;
    printf("list: %p\n", list);
    if (p)
    {
        for (; p; p = (p->next != list ? p->next : NULL))
            printf("prev: %p, self:%p, next:%p, data = %d\n", p->prev, p, p->next, p->data);
    }
    puts("");
}

void insert(rec **pp, int data)
{
    // setup new node
    rec *newp = malloc(sizeof(*newp));
    newp->data = data;

    if (!*pp)
    {
        newp->next = newp->prev = newp;
        *pp = newp;
    }
    else
    {   // insert between prev and head.
        newp->next = *pp;
        (*pp)->prev->next = newp;
        newp->prev = (*pp)->prev;
        (*pp)->prev = newp;
    }
}

int main()
{
    rec *list = NULL;
    int i;

    for (i=1; i<=5; ++i)
        insert(&list, i);
    iterfwd(list);

    // delete fourth node (0-based)
    delete_node(&list, 3);
    iterfwd(list);

    // delete first node (0-based)
    delete_node(&list, 0);
    iterfwd(list);

    // delete first node (0-based)
    delete_node(&list, 0);
    iterfwd(list);

    // delete first node (0-based)
    delete_node(&list, 0);
    iterfwd(list);

    // delete first node (0-based)
    delete_node(&list, 0);
    iterfwd(list);

    return 0;
}

您似乎没有考虑到这样一个事实,即您提供给此函数的
rec*list
指针本身指向某个对象。如果它是您应该删除的节点,那么调用方的指针也需要更改。事实上,所有内容都是通过引用传递的,这就是为什么在后续对迭代器的调用中,删除节点消失了——这只是关于节点0的删除我缺少的内容。不是。调用
delete\u节点时,您正在传递列表头的值。调用方的指针不受影响。您需要按地址传递指针(指针指向指针),或者利用函数的返回值作为新的列表头,否则列表头可能会更改(如果您删除第一个节点,它将更改)。@whoscrig,很抱歉,您完全正确。虽然我的脑袋还在为解压您优雅解决方案背后的逻辑而发狂,但我的错误正是您第一次建议的,最初的delete_节点未能修改调用方的指针。我仍然不明白,为什么在删除节点0之后的第二次迭代中,使用我的原始代码,列表正确地显示了删除(几乎就像最终从调用方的指针“刷新”了删除)。感谢您花时间来说明这个错误。@DavidC.Rankin“为什么”相当直截了当。若要修改指针,则需要按地址传递指针。与向函数传递
int*p
的方式相同,如果希望修改调用方
inta
,则传递
&a
,指针变量也一样。指针“值”是它们所持有的地址。如果您需要更改该地址,那么与C中的其他任何内容一样,您需要将参数声明为指向正式基础类型的指针(在本例中为指针类型,所以为指针),并传递被修改对象的地址。坚持下去,它最终会有意义的。
49 - prev: 0x604b10  cur: 0x604ba0  next: 0x603010
 0 - prev: 0x604ba0  cur: 0x603010  next: 0x6030a0  <-- delete_node
 1 - prev: 0x603010  cur: 0x6030a0  next: 0x603130

49 - prev: 0x604b10  cur: 0x604ba0  next: 0x6030a0
 0 - prev: 0x604ba0  cur: 0x603010  next: 0x6030a0  <-- (node deleted)
 1 - prev: 0x604ba0  cur: 0x6030a0  next: 0x603130
 0 - prev: 0x604ba0  cur: 0x603010  next: 0x6030a0
 1 - prev: 0x604ba0  cur: 0x6030a0  next: 0x603130
 2 - prev: 0x6030a0  cur: 0x603130  next: 0x6031c0
 3 - prev: 0x603130  cur: 0x6031c0  next: 0x603250
 4 - prev: 0x6031c0  cur: 0x603250  next: 0x6032e0
<snip>
47 - prev: 0x6049f0  cur: 0x604a80  next: 0x604b10
48 - prev: 0x604a80  cur: 0x604b10  next: 0x604ba0
49 - prev: 0x604b10  cur: 0x604ba0  next: 0x6030a0
 1 - prev: 0x604ba0  cur: 0x6030a0  next: 0x603130
 2 - prev: 0x6030a0  cur: 0x603130  next: 0x6031c0
 3 - prev: 0x603130  cur: 0x6031c0  next: 0x603250
#include <stdio.h>
#include <stdlib.h>

typedef struct record rec;
struct record
{
    int data;
    rec *prev, *next;
};

void delete_node (rec ** pp, int num)
{
    if (!*pp)
        return;

    // find the num'th node
    while (num-- && *pp)
        pp = &(*pp)->next;

    // setup victim
    rec *victim = *pp;

    // non-self-reference node means just rewire
    if (victim && (victim != victim->next))
    {
        victim->prev->next = victim->next;
        victim->next->prev = victim->prev;
        *pp = victim->next;
    }
    else
    {   // deleted node was self-referenced. last node
        *pp = NULL;
    }
    free(victim);
}

void iterfwd(const rec* list)
{
    const rec *p = list;
    printf("list: %p\n", list);
    if (p)
    {
        for (; p; p = (p->next != list ? p->next : NULL))
            printf("prev: %p, self:%p, next:%p, data = %d\n", p->prev, p, p->next, p->data);
    }
    puts("");
}

void insert(rec **pp, int data)
{
    // setup new node
    rec *newp = malloc(sizeof(*newp));
    newp->data = data;

    if (!*pp)
    {
        newp->next = newp->prev = newp;
        *pp = newp;
    }
    else
    {   // insert between prev and head.
        newp->next = *pp;
        (*pp)->prev->next = newp;
        newp->prev = (*pp)->prev;
        (*pp)->prev = newp;
    }
}

int main()
{
    rec *list = NULL;
    int i;

    for (i=1; i<=5; ++i)
        insert(&list, i);
    iterfwd(list);

    // delete fourth node (0-based)
    delete_node(&list, 3);
    iterfwd(list);

    // delete first node (0-based)
    delete_node(&list, 0);
    iterfwd(list);

    // delete first node (0-based)
    delete_node(&list, 0);
    iterfwd(list);

    // delete first node (0-based)
    delete_node(&list, 0);
    iterfwd(list);

    // delete first node (0-based)
    delete_node(&list, 0);
    iterfwd(list);

    return 0;
}
list: 0x100103af0
prev: 0x100103b70, self:0x100103af0, next:0x100103b10, data = 1
prev: 0x100103af0, self:0x100103b10, next:0x100103b30, data = 2
prev: 0x100103b10, self:0x100103b30, next:0x100103b50, data = 3
prev: 0x100103b30, self:0x100103b50, next:0x100103b70, data = 4
prev: 0x100103b50, self:0x100103b70, next:0x100103af0, data = 5

list: 0x100103af0
prev: 0x100103b70, self:0x100103af0, next:0x100103b10, data = 1
prev: 0x100103af0, self:0x100103b10, next:0x100103b30, data = 2
prev: 0x100103b10, self:0x100103b30, next:0x100103b70, data = 3
prev: 0x100103b30, self:0x100103b70, next:0x100103af0, data = 5

list: 0x100103b10
prev: 0x100103b70, self:0x100103b10, next:0x100103b30, data = 2
prev: 0x100103b10, self:0x100103b30, next:0x100103b70, data = 3
prev: 0x100103b30, self:0x100103b70, next:0x100103b10, data = 5

list: 0x100103b30
prev: 0x100103b70, self:0x100103b30, next:0x100103b70, data = 3
prev: 0x100103b30, self:0x100103b70, next:0x100103b30, data = 5

list: 0x100103b70
prev: 0x100103b70, self:0x100103b70, next:0x100103b70, data = 5

list: 0x0