9012번: 괄호
스택을 안써도 그냥 count 매기는걸로 풀 수 있을 것 같지만, 분류가 스택이길래 스택 함수까지 해보았다.
#include <stdio.h>
#include <stdlib.h>//malloc
#include <string.h>
#define mal (node *)malloc(sizeof(node))
typedef struct _node
{
int data;
struct _node *next;
}node;
node *head, *end, *indexnode;
int count = 0;
void push(int num);
void pop();
void push(int num)
{
indexnode = (node *)malloc(sizeof(node));
indexnode->data = num;
indexnode->next = head->next;
head->next = indexnode;
++count;
}
void pop()
{
indexnode = head->next;
head->next = indexnode->next;
if(count>0)
free(indexnode);
--count;
}
int main()
{
head = mal;
end = mal;
int T;
char a[101];
head->next = end;
end->next = end;
scanf("%d", &T);
for (int i = 0; i < T; ++i, count = 0)
{
memset(a, 0, sizeof(a));
scanf("%s", &a);
for (int j = 0; a[j] != '\0'; ++j)
{
if (a[j] == '(')
push(1);
else if (a[j] == ')')
pop();
if (count < 0)
break;
}
if (count == 0)
printf("YES\n");
else
printf("NO\n");
}
}