Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

checking sequence Time limit: 5000ms Memory limit: 256mb Description: In this exercise, we use a stack to check a given sequnce of '(',')','[',']' is valid

checking sequence

Time limit: 5000ms Memory limit: 256mb Description: In this exercise, we use a stack to check a given sequnce of '(',')','[',']' is valid or not. Definition: If there is a ( followed by a ), or a [ followed by a ], we can delete them together from the sequence. Repeat this procedure until there is nothing to delete. Finally, if the remaining sequence is empty, we say the original sequence is valid; otherwise, the original sequence is invalid. -------------------------Copy the following code, complete it and submit------------------------- #include  #include  #define MAX 100000 typedef enum{FALSE =0, TRUE = 1} Boolean; typedef struct{ int size; int top; int *stacklist; }stack; stack *createS(int size){ stack *s; s = (stack*)malloc(sizeof(stack)); s->size =size; s->stacklist = (int*)malloc(size * sizeof(int)); s->top = -1; return s; } Boolean IsFull(stack *s){ if (s->top == s->size -1) return TRUE; else return FALSE; } Boolean IsEmpty(stack *s){ if (s->top == -1) return TRUE; else return FALSE; } void push(stack *s, int e){ if (!IsFull(s)){ s->top++; s->stacklist[s->top] = e; } } char pop(stack *s){ int i; if (!IsEmpty(s)){ i = s-> stacklist[s->top]; s->top--; return i; }else{printf("error "); return -1;} } char top(stack *s){ return s->stacklist[s->top];} Boolean isMatching(char character1, char character2) { //return TRUE if two characters match, otherwise FALSE. } Boolean test(char exp[], int size_n) { int i = 0; stack *stack_holder = createS(20000); //use stack_holder to help checking exp is valid or not, return TRUE if exp is valid, otherwise FALSE. } int main() { char value; char buf[MAX]; int size_n; printf("Input the number of chars in buf: "); scanf("%d", &size_n); printf("Input these chars: "); scanf("%s", buf); if (test(buf,size_n)) printf("Valid "); else printf("Invalid "); return 0; } -------------------------------------------End of Code------------------------------------------- You can refer page 3-6 in Stacks slides for the implementation of stack, which is already there in the template. To make this work, you need to revise the stack a little bit. Hint: Data type. Boolean isMatching(char character1, char character2); Given two character1 and character2, e.g., '(' and ')', if they matchs, return TRUE, otherwise return FALSE. Boolean test(char exp[]); Given a sequence of character exp[], initial a stack_holder to help check if this sequence is valid or not, you may use isMatching(char, char) to check if two characters is matched; Sample Input 1: 12 (()[])[()]() Sample Output 1: Valid Sample Input 2: 13 ((((([]]))))) Sample Output 2: Invalid Hint: Be careful of the efficiency issue. 

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

Recommended Textbook for

Wiley CIA Essentials Of Internal Auditing Part 1 Exam Review 2023

Authors: S. Rao Vallabhaneni

1st Edition

1119987148, 978-1119987147

More Books

Students also viewed these Accounting questions

Question

Briefly describe the seven new product development stages.

Answered: 1 week ago

Question

Write formal proposal requests.

Answered: 1 week ago

Question

Write an effective news release.

Answered: 1 week ago

Question

Identify the different types of proposals.

Answered: 1 week ago