Advertisement
Guest User

Untitled

a guest
Feb 15th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.35 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. typedef struct NODE{
  6. char data;
  7. struct NODE *next;
  8. }NODE;
  9.  
  10. typedef struct STACK{
  11. NODE *top;
  12. int size;
  13. }STACK;
  14.  
  15. STACK *creating_stack()
  16. {
  17. STACK *new_stack = (STACK*) malloc(sizeof(STACK));
  18. new_stack -> top = NULL;
  19. new_stack -> size = 0;
  20. return new_stack;
  21. }
  22.  
  23. NODE *creating_node(int data)
  24. {
  25. NODE *new_node = (NODE*) malloc(sizeof(NODE));
  26. new_node -> data = data;
  27. new_node -> next = NULL;
  28. return new_node;
  29. }
  30.  
  31. void ADD_end(NODE *head, int data)
  32. {
  33. NODE *new_node = creating_node(data);
  34. NODE *aux = head;
  35. while(aux -> next != NULL)
  36. {
  37. aux = aux -> next;
  38. }
  39. aux -> next = new_node;
  40. }
  41.  
  42.  
  43. void push(STACK *the_stack, char data)
  44. {
  45. NODE *new_node = creating_node(data);
  46. NODE *aux = the_stack -> top;
  47. the_stack -> top = new_node;
  48. new_node -> next = aux;
  49. the_stack -> size++;
  50. }
  51.  
  52. void pop(STACK *the_stack)
  53. {
  54. NODE *aux = the_stack -> top;
  55. the_stack -> top = aux-> next;
  56. the_stack -> size--;
  57. free(aux);
  58. }
  59.  
  60. int main()
  61. {
  62. char string[256];
  63. int num, i, flag = 1, j, size;
  64. STACK *the_stack = creating_stack();
  65. scanf("%d%*c", &num);
  66. // printf("%d\n",num);
  67. for(i = 0; i < num; i++)
  68. {
  69.  
  70. // scanf("%s", string);
  71. fgets(string,256,stdin);
  72. // printf("%s\n",string);
  73. size = strlen(string);
  74. int d;
  75. // printf("%d\n", size);
  76. j = 0;
  77. while(j < size)
  78. {
  79. // if(string[j] == '(' || string[j] == ')' || string[j] == '[' || string[j] == ']')
  80. d = string[j];
  81. if(d != 32){
  82. // printf("j = %d entrando\n",j);
  83. if(string[j] == '('){
  84. puts("pushing");
  85. push(the_stack, string[j]);
  86. }
  87. if(string[j] == '['){
  88. puts("pushing");
  89. push(the_stack, string[j]);
  90. }
  91. if(string[j] == ')' || string[j] == ']'){
  92. if(the_stack -> top == NULL)
  93. {
  94. puts("No");
  95. flag = 2;
  96. break;
  97. }
  98. else
  99. {
  100. if((the_stack -> top -> data == '(' && string[j] == ')') || (the_stack -> top -> data == '[' && string[j] == ']')){
  101. puts("popoing");
  102. pop(the_stack);
  103. }
  104. else{
  105. flag = 0;
  106. break;
  107. }
  108. }
  109. }
  110. }
  111. j++;
  112. }
  113.  
  114. if(the_stack -> top != NULL || flag == 0)
  115. {
  116. flag = 1;
  117. printf("No\n");
  118. }
  119. if(the_stack -> top == NULL && flag == 1)
  120. {
  121. printf("Yes\n");
  122. }
  123.  
  124. the_stack -> top = NULL;
  125. }
  126. return 0;
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement