Question
The following code takes in an integer (number of strings), and then allows the input of that many lines of parenthesis strings, and goes on
The following code takes in an integer (number of strings), and then allows the input of that many lines of parenthesis strings, and goes on to determine if they are balanced. I need it adjusted so that instead of taking the integer, and then each string line, one at a time, it takes in the integer number, then each string line BEFORE doing any calculating, BUT still calculating the balance for each line seperately.
For example, the code should alternatively be able to take in at text file with nothing but the following written:
3
()()
{}{}{{{{]][}{}
()()(){}}}{}{{{
The output should still be to print the integer, then string 1 and YES, then string 2 and NO, then string 3 and NO.
Let me know if you have any questions
#include
struct strNode { char data; struct strNode *next; };
void push(struct strNode** start, int _new); int pop(struct strNode** start);
int isMatchingPair(char character1, char character2) { if (character1 == '(' && character2 == ')') return 1; else if (character1 == '{' && character2 == '}') return 1; else if (character1 == '[' && character2 == ']') return 1; else return 0; }
int areParenthesisBalanced(char str[]) { int i = 0;
struct strNode *stack = NULL;
while (str[i]) {
if (str[i] == '{' || str[i] == '(' || str[i] == '[') push(&stack, str[i]);
if (str[i] == '}' || str[i] == ')' || str[i] == ']') {
if (stack == NULL) return 0;
else if ( !isMatchingPair(pop(&stack), str[i]) ) return 0; } i++; }
if (stack == NULL) return 1; else return 0; }
int main() { char str[100]; int i,j; scanf("%d",&j);
getchar();
for(i=0;i { fgets(str, sizeof str, stdin); if(str[0]==' ') {printf("Balanced "); exit(0);} if (areParenthesisBalanced(str)) printf("Yes "); else printf("No "); } return 0; } void push(struct strNode** start, int _new) { struct strNode* new_node = (struct strNode*) malloc(sizeof(struct strNode)); if (new_node == NULL) { printf("Stack overflow "); getchar(); exit(0); } new_node->data = _new; new_node->next = (*start); (*start) = new_node; } int pop(struct strNode** start) { char res; struct strNode *top; if (*start == NULL) { printf("Stack overflow "); getchar(); exit(0); } else { top = *start; res = top->data; *start = top->next; free(top); return res; } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started