Advertisement
Guest User

Untitled

a guest
Dec 11th, 2019
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.76 KB | None | 0 0
  1.  
  2.  
  3. #include "pch.h"
  4. #include <iostream>
  5.  
  6. typedef struct Stack
  7. {
  8. int* values;
  9. unsigned size;
  10. int top_index;
  11.  
  12.  
  13. } stack;
  14.  
  15. void init_stack(stack* stack, unsigned size);
  16. void free_stack(stack* stack);
  17.  
  18. void push(stack* stack, int value);//Adds an item in the stack. If the stack is full, then it is said to be an Overflow condition.
  19. int peek(stack* stack);//Returns top element of stack.
  20. int pop(stack* stack);//Removes an item from the stack.
  21. int is_full(stack* stack);
  22. int isEmpty(stack* stack);
  23.  
  24. void print_stack(stack* stack);
  25. void print_info(stack* stack);
  26. char get_pair(char br);
  27.  
  28. int brackets(char* str);
  29.  
  30. int main()
  31. {
  32. stack stack;
  33.  
  34. init_stack(&stack, 5);
  35. //print_stack(&stack);
  36. push(&stack, 3);
  37. push(&stack, 4);
  38. push(&stack, 3);
  39. push(&stack, 2);
  40. push(&stack, 7);
  41.  
  42. print_stack(&stack);
  43. //printf("\n%d",peek(&stack));
  44. printf("%d", stack.top_index);
  45. pop(&stack);
  46. printf("\n");
  47. print_stack(&stack);
  48. printf("%d", stack.top_index);
  49. free_stack(&stack);
  50. char str[50] = "(a+b)( *c*c[d-e]";
  51. int res = brackets(str);
  52. if (res == -1)
  53. {
  54. printf("Correct\n");
  55. }
  56. else
  57. {
  58. printf("error in position %d", res);
  59. }
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73. return 0;
  74. }
  75.  
  76.  
  77. void init_stack(stack* stack, unsigned size)
  78. {
  79.  
  80. stack->values = (int*)malloc(sizeof(int)*size);
  81. stack->top_index = -1;
  82. stack->size = size;
  83. return;
  84. }
  85. int is_full(stack* stack)// Stack is full when top is equal to the last index
  86. {
  87. return stack->top_index == stack->size-1;
  88. }
  89. int isEmpty(stack* stack)
  90. {
  91. return stack->top_index == -1;
  92. }
  93. void free_stack(stack* stack)
  94. {
  95. free(stack->values);
  96.  
  97. return;
  98. }
  99.  
  100. void push(stack* stack, int value)
  101. {
  102. if (is_full(stack))
  103. return;
  104. stack->values[++stack->top_index] = value;
  105.  
  106. }
  107. int peek(stack* stack)
  108. {
  109. return stack->values[stack->top_index];
  110. }
  111. int pop(stack* stack)
  112. {
  113.  
  114. if (isEmpty(stack))
  115. return -1;
  116.  
  117. int item =stack->values[stack->top_index];
  118. (stack->top_index) -= 1; // top = top - 1
  119. return item;
  120.  
  121. }
  122.  
  123. void print_stack(stack* stack)
  124. {
  125. for (int i = 0; i < stack->top_index; i++)
  126. {
  127. printf("%3d", stack->values[i]);
  128. }
  129.  
  130. }
  131. void print_info(stack* stack)
  132. {
  133.  
  134.  
  135.  
  136.  
  137. }
  138.  
  139. char get_pair(char br)
  140. {
  141. switch (br)
  142. {
  143. case'(':return')';
  144. case'[':return']';
  145.  
  146. case'{':return'}';
  147.  
  148. case'<':return'>';
  149. }
  150. }
  151.  
  152. int brackets(char* str)
  153. {
  154. stack st;
  155. init_stack(&st, strlen(str));
  156. int i = 0;
  157. while (str[i])
  158. {
  159. switch (str[i])
  160. {
  161. case'(':
  162. case'[':
  163. case'{':
  164. case'<':
  165. push(&st, str[i]);
  166. break;
  167. case')':
  168. case']':
  169. case'}':
  170. case'>':
  171. if (isEmpty(&st))
  172. {
  173. return i;
  174. }
  175. if (str[i] != get_pair(pop(&st)))
  176. {
  177. return i;
  178. }
  179. default:break;
  180. }
  181. i++;
  182.  
  183. }
  184. if (!isEmpty(&st))
  185. {
  186. return i;
  187. }
  188. return -1;
  189. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement