2

I'm having trouble handling stacks in C. I want to create two, and then do various things with them. The current code works in the sense that it compiles and runs but the outputs aren't all correct yet.

This is the code:

#include <stdio.h>
#include <stdlib.h>

#define STACK_SIZE 5

typedef struct stackADT {
    int elements[STACK_SIZE];
    int count;
} stack;

void initialize(stack *s) {
    s->count = 0;
}

int push(stack *s, int value) {
    if (s->count < STACK_SIZE) {
        s->elements[s->count++] = value;
        return s->count - 1;
    }
    else return -1;
}

int pop(stack *s) {
    if (s->count == 0) return -1;
    else s->count--;
}

int compareStack(stack *sA, stack *sB) {
    int i = 0;

    if (sA->count != sB->count) return 0;

    else {
        for (i; i < sA->count + 1; i++) {
            if (sA->elements[i] != sB->elements[i]) {
                return 0;
                break;
            }
            else return 1;
        }
    }
}

int printStack(stack *s) {
    if (s->count == 0) printf("ERROR: Stack is empty!\n");
    else {
        int i;
        printf("The contents of the stack are:\n");
        for (i = s->count; i != 0; i--) printf("%d: %d\n", i - 1, s->elements[i - 1]);
    }
}

void test() {

    // 1.
    stack *sA = malloc(sizeof(stack));

    stack *sB = malloc(sizeof(stack));

    initialize(sA);
    initialize(sB);

    // 2.
    push(sA, 3);
    push(sA, 4);
    push(sA, 5);
    push(sA, 6);
    push(sA, 7);
    push(sA, 8);

    printf("Removed: %d\n", pop(sA));
    printf("Removed: %d\n", pop(sA));

    // 3.
    push(sB, 12);
    push(sB, 13);
    push(sB, 6);
    push(sB, 7);

    // 4.
    printStack(sB);

    // 5.
    printf("Removed: %d\n", pop(sB));

    // 6.
    push(sB, 8);
    push(sB, 9);

    // 7.
    printf("1 if stacks are equal: %d\n", compareStack(sA, sA));

    // 8.
    printf("1 if stacks are equal: %d\n", compareStack(sA, sB));

    // 9.
    printf("Removed: %d\n", pop(sA));
    printf("Removed: %d\n", pop(sA));
    printf("Removed: %d\n", pop(sA));
    printf("Removed: %d\n", pop(sA));

    // 10.
    free(sA);
    free(sB);

}

int main() {
    test();
}

Here is the output:

Removed: 8523848
Removed: 8523848
The contents of the stack are:
3: 7
2: 6
1: 13
0: 12
Removed: 8523880
1 if stacks are equal: 1
1 if stacks are equal: 0
Removed: 8523848
Removed: 8523848
Removed: 8523848
Removed: -1

Process returned 1 (0x1)   execution time : 0.251 s
Press any key to continue.

Regarding push, it is supposed to return the position where it was added in the elements-array. I'm currently unsure of whether it returns the correct value but that is probably mostly due to my headache right now and not because it's particularly difficult to figure out. Still, if someone could give me input on that it would be appreciated.

Regarding compareStack, I feel like I'm still kind of confused between how linked lists and stacks work. From the output it looks like it could be correct since it recognizes sA and sB as not being equal. But that might just be due to the first little bit where I compare their sizes to each other and not because the following code works correctly.

Clearly something is quite wrong with the stacks as the removed-messages and the highest element of sB cannot be correct.

The test cases are kind of weird but it's what I was given to work with.

Thanks in advance for any help with my code and questions!

7
  • 1
    Have you tried valgrind? Commented Apr 11, 2015 at 22:03
  • Hm, I just looked up valgrind and it seems to be an analysis tool? I'm not sure I'm at the level where the output of such a tool would be useful for me yet to be honest. I'm still very inexperienced. It also seems a bit overkill for this little bit of code from what I can tell. Commented Apr 11, 2015 at 23:33
  • That's what I thought when I was first told about it, but when dealing with the stack, it can shine a bright light on problems, if any exist. The basics are: compile your code with the '-g' flag. Run 'valgrind --tool=memcheck <your program name> <your program arguments>' and just look at the errors. They usually reference a line in your code. Commented Apr 11, 2015 at 23:36
  • Sounds interesting. I'll look into it for future coding, thanks! But I think the remaining problem with this code comes down to something wrong with printf("Removed: %d\n", pop(sA)); not resulting in proper output. Commented Apr 11, 2015 at 23:49
  • Compile with all warnings and debug info (e.g. gcc -Wall -Wextra -g). Then learn how to use the debugger (e.g. gdb) Commented May 3, 2015 at 17:05

1 Answer 1

1

Try the following changes

int push(stack *s, int value) {
    if (s->count < STACK_SIZE) {
        s->elements[s->count++] = value;
        return s->count - 1; // not sure whether this is right
    }
    else return -1;
}


int pop(stack *s) {
    if (s->count == 0) return -1;
    else return s->elements[--s->count];
}

int compareStack(stack *sA, stack *sB) {
    int i = 0;

    if (sA->count != sB->count) return 0;

    while ( i < sA->count && sA->elements[i] == sB->elements[i] ) ++i;

    return i == sA->count;
}

void printStack(stack *s) {
    if (s->count == 0) printf("ERROR: Stack is empty!\n");
    else {
        int i;
        printf("The contents of the stack are:\n");
        for (i = s->count; i != 0; i--) printf("%d: %d\n", i - 1, s->elements[i-1]);
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Are those changes meant for debugging? Because what I actually forgot to mention is that the returns are predefined: compareStack should return either 1 or 0 for equal or nonequal and pop is only supposed to return -1 when it fails and nothing otherwise. As such, I have now tested it without the changes to the returns (implemented the -1 to pop and the changes to printStack which fixed the error when displaying that. What I'm left with is may only be an error in this: printf("Removed: %d\n", pop(sA)); since those create bogus output. I'll update OP with new code and output. Thanks!
Ok, I may have made a mental error here. Trying to figure this out. Will report back with findings.
Ok, I found my problem. Thank you so much! You actually fixed a problem that came to be because I misread the instructions. The fix at pop() was what did it: I forgot that it needed to return the value of the element that was removed, which tripped up the printf() in printStack() since they didn't get any return. I will update the OP with working code and output.

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.