1

I have to develop an algorithm using linked list and struct in C, but I'm having a problem nullifying elements on the array. To clarify:

On the left side is the inital setup of the problem. On the right side is a likely output.

What I've done so far:

struct cel {
    int pos;
    struct cel *prox;
};

typedef struct cel bloco;

...

bloco *blocos;
    scanf("%d",&numBlocos);

    blocos = malloc(sizeof(bloco) * numBlocos);

    for(i = 0; i < numBlocos; i++) {
        blocos[i].pos = i;
        blocos[i].prox = NULL;
    }

The situation: Let's say for instance the user wants to put the second 'bloco' after the fourth (like on the img). Obviously I'd do:

blocos[4].prox = &blocos[2];

But how should I proceed to nullify at blocos[2] ? I mean, something like blocos[2] = NULL

Hope it's clear enough ! Thanks

EDIT ---

I got two new functions now:

bloco* busca(bloco *blocos,int pos, int numblocos)
{
    int i;
    for(i = 0; i < numblocos; i++) {
        bloco *temp = &blocos[i];
        while(temp != NULL && temp->pos != -1) {
            if(temp->pos == pos) {
                return temp;
            }
            //se nao vai pro proximo item linkado
            temp = temp->prox;
        }
    }

    return NULL;
}

And then:

void moveOnto(bloco *blocos, int ori,int dest,int numblocos)
{
    if(ori == dest)
        return;

    bloco *origem = busca(blocos,ori,numblocos);
    retornaOrigem(blocos,origem);
    bloco *destino = busca(blocos,dest,numblocos);
    retornaOrigem(blocos,destino);

    bloco *temp = malloc(sizeof(bloco));
    *temp = *origem;
    destino->prox = temp;
    origem->pos = -1;
}

Does it still not possible to nullify the origem pointer, or in that matter the blocos[2] to null, not doing origem->pos = -1 ?? I mean, they are pointers, so, why if I set origem = null it doesn't nullify the bloco[2] (which is the same address, I guess) ??

6
  • Is there a problem with using NULL? Commented Nov 9, 2014 at 23:30
  • @Nit on something that is not a pointer, probably :) Commented Nov 9, 2014 at 23:31
  • To be clear I'd like to nullify at the blocos position, in the example above at the position 2, but the bloco[4]->prox must still point to a bloco[2], obviuosly not the nullified one. Commented Nov 9, 2014 at 23:33
  • I thought about that @indiv, but I was thinking about a better approach. Commented Nov 9, 2014 at 23:33
  • in the graph, 0 points to 4, who points to 2. But 4 should point to 2. Commented Nov 9, 2014 at 23:33

2 Answers 2

2

Your call, really. You have a struct type with an int and a pointer. You need to decide of a null state for it : maybe the int will be -1, maybe the pointer will be NULL, whatever you can use to recognize a "null" instance of this struct.

If all else fail, add a boolean (a char will do if you don't have a built-in boolean type) that flags whether the block is in a null state or not.

Sign up to request clarification or add additional context in comments.

2 Comments

Yeah, that sucks but I think it's the only way to do so. I thought that from the beggining but I hoped someone would come with a better solution... Anyway thanks for helping.
As stated in the other answer, the only universal null object in C is a NULL pointer. But that makes you use dynamic memory, which will cost you time for allocating your objects, space for the pointers, and time lost by the CPU hopping across pointers, busting your cache. Not really a good deal compared to simply reserving a null state.
1

From the pictures you showed to us, it appears to me you should use an array of pointers so that you can set NULL to any element in the array

blocos = malloc(sizeof(bloco *) * numBlocos);
for(i = 0; i < numBlocos; i++) {
    blocos[i] = malloc( sizeof(bloco ) );
    *(blocos[i]).pos  = i;
    *(blocos[i]).prox = NULL;
}

1 Comment

That would be pretty wasteful. Note : *(b[i]).p is incorrect, it should be (*b[i]).p, or better yet b[i]->p.

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.