Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Given the following code as a whole, can someone please help me split it up into the following files correctly, without changing the functionality at

Given the following code as a whole, can someone please help me split it up into the following files correctly, without changing the functionality at all and making sure everything still works when compiled? I am trying but I can't get the order/functions/declarations right when I split them into different files.

Split into:

main.c (containing the main function)

stack.c (containing the pop and push functions)

stack.h (don't even know what to put here)

balance.c (containing checkBalance and checkMatching functions)

-----------------------------------------------------------------------------------------------------------

#include #include

struct node { char data; struct node *next; };

void push(struct node **start, int newData); int pop(struct node **start);

int checkMatching(char char1, char char2) { if (char1 == '(' && char2 == ')') return 1; else if (char1 == '{' && char2 == '}') return 1; else if (char1 == '[' && char2 == ']') return 1; else return 0; }

int checkBalance(char str[]) { int i = 0; struct node *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 (!checkMatching(pop(&stack), str[i])) return 0; } i++; } if (stack == NULL) return 1; else return 0; } int main() { char str[300]; int i; int j; scanf("%d",&j); getchar(); for(i=0;i { fgets(str, sizeof str, stdin); if(str[0]==' ') { printf("Yes "); } else if (checkBalance(str)) printf("Yes "); else printf("No "); } return 0; }

void push(struct node **start, int newData) { struct node *temp = (struct node*) malloc(sizeof(struct node)); if (temp == NULL) { printf("Stack overflow "); getchar(); exit(0); } temp->data = newData; temp->next = *start; *start = temp; }

int pop(struct node **start) { char res; struct node *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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

LO2 Distinguish among three types of performance information.

Answered: 1 week ago