Linked List / Deleting any number of element while enumerating

Discussion in 'C Programming' started by Kevin, Feb 24, 2004.

  1. Kevin

    Kevin Guest

    Hi,

    How can I enumerate a linked list while being hable to delete any number of
    elements while enumerating ?

    I was using:

    struct st {
    ...
    struct st *prev;
    struct st *next;
    } ;

    struct st *fst; // First element of the linked list

    for(struct st *cst = fst, *nst; cst != NULL; cst = nst)
    {
    nst = cst->next;
    ....
    delete any number of element anywhere...
    ....
    }

    This works fine in every case but one, when nst (the next element) is
    deleted ...

    That's why I'm deseperately looking for a safe way to enumerate the linked
    list while being able to remove any element while doing it...

    I'm stuck on this for while, Thanks for any help !
    Kevin, Feb 24, 2004
    #1
    1. Advertising

  2. Kevin

    Richard Bos Guest

    "Kevin" <> wrote:

    > for(struct st *cst = fst, *nst; cst != NULL; cst = nst)
    > {
    > nst = cst->next;
    > ....
    > delete any number of element anywhere...
    > ....
    > }
    >
    > This works fine in every case but one, when nst (the next element) is
    > deleted ...


    Well, then, add code to check whether you're doing that and adjust nst
    accordingly...

    Richard
    Richard Bos, Feb 24, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Harald Hein
    Replies:
    2
    Views:
    2,756
    Harald Hein
    Aug 31, 2003
  2. fool
    Replies:
    14
    Views:
    491
    Barry Schwarz
    Jul 3, 2006
  3. Daniel Vukadinovic

    Deleting linked list

    Daniel Vukadinovic, Apr 12, 2006, in forum: C++
    Replies:
    10
    Views:
    548
    Jaspreet
    Apr 13, 2006
  4. joshd
    Replies:
    12
    Views:
    652
    John Carson
    Oct 2, 2006
  5. sara
    Replies:
    4
    Views:
    1,252
    Michael Doubez
    Feb 15, 2011
Loading...

Share This Page