Linked List / Deleting any number of element while enumerating

K

Kevin

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 !
 
R

Richard Bos

Kevin said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top