0

I'm working with a linked list and am trying to initalize two pointers equal to the "first"/"head" pointer. I'm trying to do this cleanly in a for loop. The point of all this being so that I can run two pointers through the linked list, one right behind the other (so that I can modify as needed)...

Something like:

//listHead = main pointer to the linked list
for (blockT *front, *back = listHead; front != NULL; front = front->next)
//...//
back = back->next;

The idea being I can increment front early so that it's one ahead, doing the work, and not incrementing "back" until the bottom of the code block in case I need to backup in order to modify the linked list...

Regardless as to the "why" of this, in addition to the above I've tried:

for (blockT *front = *back = listHead; /.../

for (blockT *front = listHead, blockT *back = listHead; /.../

I would like to avoid pointer to a pointer. Do I just need to initialize these before the loop?

4 Answers 4

3

You were close to success

for (blockT *front = listHead, *back = listHead; front != NULL; front = front->next)

Note, that it syntactic sugar. It is actually simply as

int x = 0, y = 1;
Sign up to request clarification or add additional context in comments.

Comments

2

The syntax is:

for ( BlockT* front = listHead, *previous = NULL;
        front != NULL;
        front = front->next ) {
    //  ...
    previous = front;
}

You'll need the special case for previous in the loop, in order to test and avoid backing up before the front of the list. Alternatively, you may be able to use &listHead, instead of NULL, but you'll need to play type games with it (and be sure you don't try and modify any members). If all you need is to be able to insert intermediate members (in front of front), or delete front, then you should consider a pointer to a pointer:

BlockT** previousNext = &listHead;
BlockT* current = listHead;
while ( current != NULL ) {
    //  ...
    previousNext = &current->next;
    current = current->next;
}

In the loop, you remove current from the list by *previousNext = &current->next (making sure to advance current before deleting the old element); to insert a new element, *previousNext = new BlockT; previousNext->next = current;.

1 Comment

I like the way you've set up that top block, setting previous to front at bottom which should always keep it one behind and it looks much cleaner. Thanks!
1

I'd put the declarations of the pointers outside the loop:

//listHead = main pointer to the linked list
blockT *front, *back;
for (front = back = listHead; front != NULL; front = front->next) {
    //...//
    back = back->next;
}

This way you can also perform the back = back->next; line as you have written it (outside of the loop) if you for some reason need to, although I suppose you want it in there. Declaring variables (including pointers, of course) in the loop makes them unavailable outside it.

Update: If you, however need the pointers only for the array, I would go for:

for (blockT *front = listHead, *back = listHead; front != NULL; front = front->next) {
    //...//
    back = front;
}

Comments

1

You can take the variable declarations out of the for:

blockT *front, *back;
for (front = back = listHead; front != NULL; front = front->next)

But for your purpose this would be more logical:

for (blockT *front = listHead, *back = 0; 
    front != NULL;  
    back = front, front = front->next) {

    /* if (back != 0) backup */
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.