Advertisement
LilChicha174

Untitled

May 23rd, 2022
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.42 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <math.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5.  
  6.  
  7. typedef struct Stack {
  8.     char elem;
  9.     struct Stack *next;
  10. } Stack;
  11.  
  12. Stack *initStack() {
  13.     Stack *kek = malloc(sizeof(Stack));
  14.     kek->next = NULL;
  15.     return kek;
  16. }
  17.  
  18. void pop(Stack *stack) {
  19.     stack->next = stack->next->next;
  20. }
  21.  
  22. void push(Stack *head, char val) {
  23.     Stack *kek = initStack();
  24.     kek->elem = val;
  25.     kek->next = head->next;
  26.     head->next = kek;
  27. }
  28.  
  29. char top(Stack *head) {
  30.     return head->next->elem;
  31. }
  32.  
  33. int isEmpty(Stack *head) {
  34.     return head->next == NULL;
  35. }
  36.  
  37. int count(Stack *head) {
  38.     Stack *cur = head;
  39.     int kl = 0;
  40.     while (cur != NULL) {
  41.         cur = cur->next;
  42.         kl++;
  43.     }
  44.     return kl - 1;
  45. }
  46.  
  47. int main() {
  48.     Stack *head = initStack();
  49.     char c;
  50.     int flag = 1;
  51.     do {
  52.         c = getchar();
  53.         if (strchr("([<{", c)) {
  54.             push(head, c);
  55.             continue;
  56.         } else {
  57.             if (strchr(")}]>", c)) {
  58.                 if (!(isEmpty(head))&&((c == ')' && top(head)=='(') || ((int)top(head)+2 == (int)
  59.                 c))){
  60.                     pop(head);
  61.                 }
  62.                 else{
  63.                     printf("wrong");
  64.                     flag = 0;
  65.                     break;
  66.                 }
  67.             }
  68.         }
  69.     } while (c != '\n');
  70.     if(flag)
  71.         printf("correct");
  72.     return 0;
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement