A friend and I are trying to lean how to program in C. We are still beginners and don’t know too much about it and we are trying to make a stack. But we have a problem. We have included the code for our program in this message and if you can please help us to make this program work we will be very happy!
The problem with the program is when we run it and type ab it will say the contents of the stack is b. When we run it and type abcdefg it will say the contents of the stack is f d b. we want it to say ba the first time and gfedcba the second time. We have looked at the code a lot and we don’t know what is wrong so please help us. The code for the program is in this post below. Thankyou
Code:#include <stdio.h> #include <stdlib.h> #define STACK_SIZE 50 #define TRUE 1 #define FALSE 0 typedef int Bool; void make_empty(void); Bool is_empty(void); Bool is_full(void); void push(char c); char pop(void); void stack_overflow(void); void stack_underflow(void); char contents[STACK_SIZE]; int top = 0; int main() { char temp; printf("Enter some characters: "); while (getchar() != '\n') { temp = getchar(); push(temp); } printf("Contents of stack (should be reversed): "); while (is_empty() == FALSE) { printf("%c ", pop()); } printf("\n"); return 0; } void make_empty(void) { top = 0; } Bool is_empty(void) { return top == 0; } Bool is_full(void) { return top == STACK_SIZE; } void push(char c) { if (is_full()) stack_overflow(); else contents[top++] = c; } char pop(void) { if (is_empty()) stack_underflow(); else return contents[--top]; } void stack_overflow(void) { printf("Stack Overflow!\n"); exit(EXIT_FAILURE); } void stack_underflow(void) { printf("Stack Underflow!\n"); exit(EXIT_FAILURE); }