-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathValidParentheses
39 lines (35 loc) · 1.28 KB
/
ValidParentheses
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
struct Node{
char brackett;
struct Node* prev;
};
bool isValid(char* s) {
struct Node* tempNode;
struct Node* StackTop = NULL;
char tempChar;
char* sPtr = s;
while(*sPtr != NULL){
if(*sPtr == '(' || *sPtr == '{' || *sPtr == '[' ){ //push if next character is an open bracket
tempNode = malloc( sizeof(struct Node) );
(*tempNode).brackett = *(sPtr);
(*tempNode).prev = StackTop;
StackTop = tempNode;
}else{
//pop StackTop holds the opening brackett
if(StackTop == NULL){
return false;
}else{
if((*StackTop).brackett + 0x1 == (*sPtr) || (*StackTop).brackett + 0x2 == (*sPtr) ){
//compare StackTop char to string Pointer char
//closing brackett is one or two away in the ascii table
//If value of StackTop is not a closing brackett then return false
//free()
StackTop = (*StackTop).prev;
}else{
return false;
}
}
}
sPtr++;
}
return (StackTop == NULL); //stack should be empty, return true if empty and false elsewise
}