I made it to the chapter in my book on program organization. It also introduced the concept of the stack data structure and gave this example program. Full code and question text is below
I have two questions
1). Is my interpretation of how the array gets filled out as a stack according to the code below correct? Does it go from left to right with this? Am I incremented the stack "top" pointer correctly. I had to draw this out because the book doesn't always give perfectly explicit explanations.
I put some sample input in the black box and outlined the steps that I believe the program executes
Full res picture link: http://i.cubeupload.com/NahJbi.jpg
2). I tried "stepping" through the program in Visual Studio and could not get the console to display the prompt I want to start the main loop. Did I position the break point correctly?
I've noticed that I'm not able to step through certain programs to see what all the variables and array elements are at a given time.
Is there a reason for this I'm overlooking? Attached screenshots of my attempt on this image host: http://i.cubeupload.com/NDhMz9.jpg
Code:#include <stdbool.h>#include <stdio.h> #include <stdlib.h> #define STACK_SIZE 100 char contents[STACK_SIZE]; int top = 0; void make_empty(void); bool is_empty(void); bool is_full(void); void push(char c); char pop(void); void failure(void); void stack_underflow(void); void stack_overflow(void); int main(void) { char c; printf("Enter parentheses and/or braces: "); while ((c = getchar()) != '\n') { if (c == '(' || c == '{') { push(c); } else if (c == ')') { if (pop() != '(') failure(); } else if (c == '}') { if (pop() != '{') failure(); } } if (is_empty()) printf("Parentheses/braces are nested properly.\n"); else failure(); 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 i) { if (is_full()) stack_overflow(); else contents[top++] = i; } char pop(void) { if (is_empty()) stack_underflow(); else return contents[--top]; } void failure(void) { printf("Parentheses/braces are not matched.\n"); exit(0); } void stack_underflow(void) { failure(); } void stack_overflow(void) { printf("Stack overflow\n"); exit(1); }
Exercise Question:
1). Modify the stack example so that it storescharacters instead of integers. Next, add a main function that asks the user toenter a series of parentheses and/or braces, then indicates whether or notthey're properly nested.
Enter parentheses and/or braces: (() {} { () } )
Parentheses/braces are nested properly
Hint: as the program reads characters, have it pusheach left parenthesis or left brace. When it reads a right parenthesis orbrace, have it pop the stack and check that the item popped is matching theparenthesis or brace. (If not, the parentheses/braces aren’t nested properly)
-When the program reads thenew-line character, have it check whether the stack is empty; if so, theparentheses are matched. If the stack isn't empty (or if stack_underflow is ever called), theparentheses/braces aren't matched.
-If stack_overflow is called, havethe program print the message Stack overflow and terminate immediately