VioletaBabel

9012번: 괄호 본문

백준/백준-C
9012번: 괄호
Beabletoet 2017. 4. 20. 12:39

스택을 안써도 그냥 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");

}

}

'백준 > 백준-C' 카테고리의 다른 글

2750번: 수 정렬하기  (0) 2017.05.02
11866번: 조세퍼스 문제  (0) 2017.04.25
10845번: 큐  (0) 2017.04.21
1874번:스택 수열  (0) 2017.04.19
10828번: 스택  (0) 2017.04.19
Comments