Question

I'm stuck in some trouble, unfortunately I'm still learning the details of this language.

I have the following code

#define SIZE 5
typedef struct 
{
    int start;
    int end;
    int nElements;
    int vetor[SIZE];
} TSTACK;

and I try to inser values in its array using the following function:

void Push(TSTACK *s, int elementInsert)
{
    if(IsFull(s))
    {
        printf("%s\n", "# ERROR: full.");
    }
    else
    {
        s->end++;
        if(s->end == SIZE)
        {
            s->end = 0;
        }

        s->vetor[s->end] = elementInsert;
        s->nElements++;
    }
}

and I use the following function to show elements:

void Display(TSTACK *s)
{
    int i;
    if (isEmpty(s))
    {
        printf("%s\n", "# ERROR: empty");
    }
    else
    {
        for (i = 0; i < s->nElements; i++) 
        {
            printf ("value: %d\n", s->vetor[i]);
        }
    }
}

My question is that I don't get the same result when I add different amount of elements, when I insert 5 elements, the Display function display all the elements correctly, but when I insert less than 5, the first item appears to be its pointer:

The following main results in 4201166, 3, 5, 7

int main(void)
{
    TSTACK test;

    test.start = 1;
    test.end = 0;
    test.nElements = 0;

    Push(&test, 3);
    Push(&test, 5);
    Push(&test, 7);
    Push(&test, 3);

    Display(&test);
}

The following main results in 5, 3, 5, 7, 3

int main(void)
{
    TSTACK test;

    test.start = 1;
    test.end = 0;
    test.nElements = 0;

    Push(&test, 3);
    Push(&test, 5);
    Push(&test, 7);
    Push(&test, 3);
    Push(&test, 5);

    Display(&test);
}

Can someone show me where I am wrong?

Was it helpful?

Solution

When you "insert" an element, you do it at

s->vetor[s->end]

But before you do that you increase s->end, so the first element will be at s->vetor[1]. Since the first element (s->vetor[0]) is not initialized, you will have a random value there. That is the reason you have a strange value first in the four-value insert.

When you insert a fifth value, you have the check for s->end == SIZE where you set s->end to zero, so the last value in that case will end up in the first place in the array.

The solution, that will solve both of the above problems, is to increase s->end after you assigned the value to the array.

OTHER TIPS

When you print the stack:

for (i = 0; i < s->nElements; i++) 
{
    printf ("value: %d\n", s->vetor[i]);
}

You are starting at index 0, but your stack implementation starts at index 1, so when you have less than 5 elements the first item is not initialized. Perhaps you can fix this by changing start value to 0.

First problem: Local variables aren't initialized, they contain garbage unless you initialize them.

Second problem: You start assigning at array index 1 instead of 0. Only when you insert five elements will it work, because you check if end == SIZE and if so set end = 0, making the fift insert set the first element vetor[0].

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top