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-------------------------

/*
I, <Your Full Name>, am submitting the assignment for
an individual project.
I declare that the assignment here submitted is original except for
source material explicitly acknowledged, the piece of work, or a part
of the piece of work has not been submitted for more than one purpose
(i.e. to satisfy the requirements in two different courses) without
declaration. I also acknowledge that I am aware of University policy
and regulations on honesty in academic work, and of the disciplinary
guidelines and procedures applicable to breaches of such policy and
regulations, as contained in the University website
http://www.cuhk.edu.hk/policy/academichonesty/.
It is also understood that assignments without a properly signed
declaration by the student concerned will not be graded by the
teacher(s).
*/

#include <stdio.h>
#include <stdlib.h>
#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\n"); 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:\n");
    scanf("%d", &size_n);
    printf("Input these chars:\n");
	scanf("%s", buf);
	if (test(buf,size_n))
		printf("Valid \n");
	else
		printf("Invalid \n");

    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. 

Submit