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!
printf("Removed: %d\n", pop(sA));not resulting in proper output.gcc -Wall -Wextra -g). Then learn how to use the debugger (e.g.gdb)