#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#define MAX_NODE_SIZE 128
typedef struct node {
char data;
struct node * next;
} Node;
typedef struct {
Node * head;
Node * crnt;
} List;
typedef struct {
int max;
int num;
List stk;
} Stack;
#define head(stack) ((stack)->stk.head)
#define crnt(stack) ((stack)->stk.crnt)
#define num(stack) ((stack)->num)
static char pop( Stack * stack)
{
char data = crnt( stack) -> data;
Node * next = crnt( stack) -> next;
crnt( stack) = next;
num( stack) --;
return data;
}
static void final( Stack * stack)
{
while ( crnt( stack) != head( stack) ) {
Node * next = crnt( stack) -> next;
crnt( stack) = next;
}
}
static void push( Stack * stack, char data)
{
Node * node;
if ( stack-> num + 1 >= stack-> max) {
fprintf ( stderr
, "stack size is overflow, which must be smaller than %d\n " , stack
-> max
) ; final( stack) ;
}
node
= calloc ( 1 , sizeof ( * node
) ) ; if ( node == NULL) {
fprintf ( stderr
, "Cannot allocate memory (stack size = %d)\n " , stack
-> num
) ; final( stack) ;
}
node-> data = data;
node-> next = crnt( stack) ;
crnt( stack) = node;
num( stack) ++;
}
static void dbg( Stack * stack)
{
Node * node;
printf ( "max = %d\n " , stack
-> max
) ; printf ( "num = %d\n " , num
( stack
) ) ;
for ( node = crnt( stack) ; node != head( stack) ; node = node-> next)
}
static void init( Stack * stack, Node * head)
{
stack-> max = MAX_NODE_SIZE;
stack-> num = 0 ;
head( stack) = head;
crnt( stack) = head;
}
#define valid_or_return(stack, str, c, p) \
do { \
if (num(stack) == 0 || pop(stack) != c) { \
printf("%s is invalid. %c is invalid character.\n", str, *p); \
final(stack); \
return; \
} \
} while (0)
static void validate( char * str)
{
Stack stack;
Node head;
char * p;
init( & stack, & head) ;
for ( p = str; * p != '\0 ' ; p++ ) {
/** FIXME: not smart switch statement */
switch ( * p) {
case '(' :
case '{' :
case '[' :
push( & stack, * p) ;
break ;
case ')' :
valid_or_return( & stack, str, '(' , p) ;
break ;
case '}' :
valid_or_return( & stack, str, '{' , p) ;
break ;
case ']' :
valid_or_return( & stack, str, '[' , p) ;
break ;
default :
/** skip this character */
break ;
}
}
if ( num( & stack) == 0 )
printf ( "%s is valid.\n " , str
) ; else
printf ( "%s is invalid.\n " , str
) ;
final( & stack) ;
}
static void task1( )
{
Stack stack;
Node head;
init( & stack, & head) ;
push( & stack, 'A' ) ;
push( & stack, 'B' ) ;
push( & stack, 'C' ) ;
push( & stack, 'C' ) ;
pop( & stack) ;
pop( & stack) ;
dbg( & stack) ;
/**
do {
int i;
for (i = 0; i < stack.max; i++)
push(&stack, '0');
} while (0);
*/
final( & stack) ;
}
static void task2( )
{
validate( "(a)" ) ;
validate( "[a(b)c]" ) ;
validate( "{a(b)cd}" ) ;
validate( "{e}" ) ;
validate( "(a}" ) ;
validate( "[a(bc]" ) ;
validate( "{a(b)cd}[e}" ) ;
validate( "({}[()])" ) ;
validate( "({])" ) ;
validate( "({})])" ) ;
validate( "({}[()" ) ;
}
int main( void )
{
task1( ) ;
task2( ) ;
return 0 ;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KI2luY2x1ZGUgPHN0cmluZy5oPgojaW5jbHVkZSA8YXNzZXJ0Lmg+CgojZGVmaW5lIE1BWF9OT0RFX1NJWkUgMTI4Cgp0eXBlZGVmIHN0cnVjdCBub2RlIHsKICBjaGFyIGRhdGE7CiAgc3RydWN0IG5vZGUgKm5leHQ7Cn0gTm9kZTsKCnR5cGVkZWYgc3RydWN0IHsKICBOb2RlICpoZWFkOwogIE5vZGUgKmNybnQ7Cn0gTGlzdDsKCnR5cGVkZWYgc3RydWN0IHsKICBpbnQgbWF4OwogIGludCBudW07CiAgTGlzdCBzdGs7Cn0gU3RhY2s7CgojZGVmaW5lIGhlYWQoc3RhY2spICgoc3RhY2spLT5zdGsuaGVhZCkKI2RlZmluZSBjcm50KHN0YWNrKSAoKHN0YWNrKS0+c3RrLmNybnQpCiNkZWZpbmUgbnVtKHN0YWNrKSAoKHN0YWNrKS0+bnVtKQoKc3RhdGljIGNoYXIgcG9wKFN0YWNrICpzdGFjaykKewogIGNoYXIgZGF0YSA9IGNybnQoc3RhY2spLT5kYXRhOwogIE5vZGUgKm5leHQgPSBjcm50KHN0YWNrKS0+bmV4dDsKCiAgZnJlZShjcm50KHN0YWNrKSk7CiAgY3JudChzdGFjaykgPSBuZXh0OwogIG51bShzdGFjayktLTsKCiAgcmV0dXJuIGRhdGE7Cn0KCnN0YXRpYyB2b2lkIGZpbmFsKFN0YWNrICpzdGFjaykKewogIHdoaWxlIChjcm50KHN0YWNrKSAhPSBoZWFkKHN0YWNrKSkgewogICAgTm9kZSAqbmV4dCA9IGNybnQoc3RhY2spLT5uZXh0OwogICAgZnJlZShjcm50KHN0YWNrKSk7CiAgICBjcm50KHN0YWNrKSA9IG5leHQ7CiAgfQp9CgpzdGF0aWMgdm9pZCBwdXNoKFN0YWNrICpzdGFjaywgY2hhciBkYXRhKQp7CiAgTm9kZSAqbm9kZTsKICAKICBpZiAoc3RhY2stPm51bSArIDEgPj0gc3RhY2stPm1heCkgewogICAgZnByaW50ZihzdGRlcnIsICJzdGFjayBzaXplIGlzIG92ZXJmbG93LCB3aGljaCBtdXN0IGJlIHNtYWxsZXIgdGhhbiAlZFxuIiwgc3RhY2stPm1heCk7CiAgICBmaW5hbChzdGFjayk7CiAgICBleGl0KDEpOwogIH0KICAKICBub2RlID0gY2FsbG9jKDEsIHNpemVvZigqbm9kZSkpOwogIGlmIChub2RlID09IE5VTEwpIHsKICAgIGZwcmludGYoc3RkZXJyLCAiQ2Fubm90IGFsbG9jYXRlIG1lbW9yeSAoc3RhY2sgc2l6ZSA9ICVkKVxuIiwgc3RhY2stPm51bSk7CiAgICBmaW5hbChzdGFjayk7CiAgICBleGl0KDEpOwogIH0KICAKICBub2RlLT5kYXRhID0gZGF0YTsKICBub2RlLT5uZXh0ID0gY3JudChzdGFjayk7CiAgY3JudChzdGFjaykgPSBub2RlOwogIG51bShzdGFjaykrKzsKfQoKc3RhdGljIHZvaWQgZGJnKFN0YWNrICpzdGFjaykKewogIE5vZGUgKm5vZGU7CgogIHByaW50ZigibWF4ID0gJWRcbiIsIHN0YWNrLT5tYXgpOwogIHByaW50ZigibnVtID0gJWRcbiIsIG51bShzdGFjaykpOwoKICBmb3IgKG5vZGUgPSBjcm50KHN0YWNrKTsgbm9kZSAhPSBoZWFkKHN0YWNrKTsgbm9kZSA9IG5vZGUtPm5leHQpCiAgICBwcmludGYoIiVjXG4iLCBub2RlLT5kYXRhKTsKfQoKc3RhdGljIHZvaWQgaW5pdChTdGFjayAqc3RhY2ssIE5vZGUgKmhlYWQpCnsKICBzdGFjay0+bWF4ID0gTUFYX05PREVfU0laRTsKICBzdGFjay0+bnVtID0gMDsKICBoZWFkKHN0YWNrKSA9IGhlYWQ7CiAgY3JudChzdGFjaykgPSBoZWFkOwp9CgojZGVmaW5lIHZhbGlkX29yX3JldHVybihzdGFjaywgc3RyLCBjLCBwKSAgICAgICAgICAgICAgICAgICAgICAgICAgICBcCmRvIHsgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIFwKICBpZiAobnVtKHN0YWNrKSA9PSAwIHx8IHBvcChzdGFjaykgIT0gYykgeyAgICAgICAgICAgICAgICAgICAgICAgICAgXAogICAgcHJpbnRmKCIlcyBpcyBpbnZhbGlkLiAlYyBpcyBpbnZhbGlkIGNoYXJhY3Rlci5cbiIsIHN0ciwgKnApOyAgICBcCiAgICBmaW5hbChzdGFjayk7ICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIFwKICAgIHJldHVybjsgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgXAogIH0gICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBcCn0gd2hpbGUgKDApCgpzdGF0aWMgdm9pZCB2YWxpZGF0ZSh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