Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- typedef struct NODE{
- char data;
- struct NODE *next;
- }NODE;
- typedef struct STACK{
- NODE *top;
- int size;
- }STACK;
- STACK *creating_stack()
- {
- STACK *new_stack = (STACK*) malloc(sizeof(STACK));
- new_stack -> top = NULL;
- new_stack -> size = 0;
- return new_stack;
- }
- NODE *creating_node(int data)
- {
- NODE *new_node = (NODE*) malloc(sizeof(NODE));
- new_node -> data = data;
- new_node -> next = NULL;
- return new_node;
- }
- void ADD_end(NODE *head, int data)
- {
- NODE *new_node = creating_node(data);
- NODE *aux = head;
- while(aux -> next != NULL)
- {
- aux = aux -> next;
- }
- aux -> next = new_node;
- }
- void push(STACK *the_stack, char data)
- {
- NODE *new_node = creating_node(data);
- NODE *aux = the_stack -> top;
- the_stack -> top = new_node;
- new_node -> next = aux;
- the_stack -> size++;
- }
- void pop(STACK *the_stack)
- {
- NODE *aux = the_stack -> top;
- the_stack -> top = aux-> next;
- the_stack -> size--;
- free(aux);
- }
- int main()
- {
- char string[256];
- int num, i, flag = 1, j, size;
- STACK *the_stack = creating_stack();
- scanf("%d%*c", &num);
- // printf("%d\n",num);
- for(i = 0; i < num; i++)
- {
- // scanf("%s", string);
- fgets(string,256,stdin);
- // printf("%s\n",string);
- size = strlen(string);
- int d;
- // printf("%d\n", size);
- j = 0;
- while(j < size)
- {
- // if(string[j] == '(' || string[j] == ')' || string[j] == '[' || string[j] == ']')
- d = string[j];
- if(d != 32){
- // printf("j = %d entrando\n",j);
- if(string[j] == '('){
- puts("pushing");
- push(the_stack, string[j]);
- }
- if(string[j] == '['){
- puts("pushing");
- push(the_stack, string[j]);
- }
- if(string[j] == ')' || string[j] == ']'){
- if(the_stack -> top == NULL)
- {
- puts("No");
- flag = 2;
- break;
- }
- else
- {
- if((the_stack -> top -> data == '(' && string[j] == ')') || (the_stack -> top -> data == '[' && string[j] == ']')){
- puts("popoing");
- pop(the_stack);
- }
- else{
- flag = 0;
- break;
- }
- }
- }
- }
- j++;
- }
- if(the_stack -> top != NULL || flag == 0)
- {
- flag = 1;
- printf("No\n");
- }
- if(the_stack -> top == NULL && flag == 1)
- {
- printf("Yes\n");
- }
- the_stack -> top = NULL;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement