Skip to main content
added 33 characters in body
Source Link
coderodde
  • 32.3k
  • 15
  • 79
  • 205

Advice I - unsigned int memb_size

This is a counter. Therefore, I suggest using a data type that is tailored for counting: size_t memb_size;.

Advice II - More verbose zero comparisons

In stack_is_empty, you write: !s->size;. I suggest you write instead s->size == 0;.

Advice III - stack_create

In stack_create, you write:

if (!cap || !memb_size) {
    return NULL;
}

Why not:

if (cap == 0 || memb_size == 0) {
    return NULL;
}

Advice IV - Contract the stack when too small

I suggest this: when the load factor of your stack drops below 25%, halve its capacity. This will optimize the memory usage of your stack while keeping the running time complexity of stack_pop as amortized \$\Theta(1)\$. This is a basic stuff from CLRS.

Advice I - unsigned int memb_size

This is a counter. Therefore, I suggest using a data type that is tailored for counting: size_t memb_size;.

Advice II - More verbose zero comparisons

In stack_is_empty, you write: !s->size;. I suggest you write instead s->size == 0;.

Advice III - stack_create

In stack_create, you write:

if (!cap || !memb_size) {
    return NULL;
}

Why not:

if (cap == 0 || memb_size == 0) {
    return NULL;
}

Advice IV - Contract the stack when too small

I suggest this: when the load factor of your stack drops below 25%, halve its capacity. This will optimize the memory usage of your stack while keeping the running time complexity of stack_pop as amortized \$\Theta(1)\$.

Advice I - unsigned int memb_size

This is a counter. Therefore, I suggest using a data type that is tailored for counting: size_t memb_size;.

Advice II - More verbose zero comparisons

In stack_is_empty, you write: !s->size;. I suggest you write instead s->size == 0;.

Advice III - stack_create

In stack_create, you write:

if (!cap || !memb_size) {
    return NULL;
}

Why not:

if (cap == 0 || memb_size == 0) {
    return NULL;
}

Advice IV - Contract the stack when too small

I suggest this: when the load factor of your stack drops below 25%, halve its capacity. This will optimize the memory usage of your stack while keeping the running time complexity of stack_pop as amortized \$\Theta(1)\$. This is a basic stuff from CLRS.

Source Link
coderodde
  • 32.3k
  • 15
  • 79
  • 205

Advice I - unsigned int memb_size

This is a counter. Therefore, I suggest using a data type that is tailored for counting: size_t memb_size;.

Advice II - More verbose zero comparisons

In stack_is_empty, you write: !s->size;. I suggest you write instead s->size == 0;.

Advice III - stack_create

In stack_create, you write:

if (!cap || !memb_size) {
    return NULL;
}

Why not:

if (cap == 0 || memb_size == 0) {
    return NULL;
}

Advice IV - Contract the stack when too small

I suggest this: when the load factor of your stack drops below 25%, halve its capacity. This will optimize the memory usage of your stack while keeping the running time complexity of stack_pop as amortized \$\Theta(1)\$.