Advertisement
Guest User

Untitled

a guest
Mar 21st, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.98 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>
  4. #define new(x) (x*)malloc(sizeof(x))
  5.  
  6. typedef struct node
  7. {
  8. char value;
  9. struct node* next;
  10. } Node;
  11.  
  12. typedef struct stack{
  13. Node* head; // Posisi teratas dari Stack
  14. int size; // Banyak elemen di Stack
  15. } Stack;
  16.  
  17. void push(Stack* st, char value)
  18. {
  19. Node* temp = new(Node);
  20. temp->value = value;
  21. temp->next = NULL;
  22. if(!st->size)
  23. { // Kasus apabila Stack kosong
  24. st->head = temp;
  25. }
  26. else
  27. { // Kasus Stack sudah terisi
  28. temp->next = st->head;
  29. st->head = temp;
  30. }
  31. st->size++; // Jangan lupa increment size
  32. }
  33.  
  34. char pop(Stack* st)
  35. {
  36. if(st->size <= 0) return -1;
  37. Node* temp = st->head;
  38. int value = temp->value;
  39. st->head = temp->next;
  40. st->size--;
  41. free(temp);
  42.  
  43. return value;
  44. }
  45. void printAll(Stack* lk)
  46. {
  47. Node* tmp=lk->head;
  48. while(tmp!=NULL)
  49. {
  50. printf("%c",tmp->value);
  51. tmp=tmp->next;
  52. }
  53. printf("\n");
  54. return;
  55. }
  56.  
  57.  
  58.  
  59. int
  60. main()
  61. {
  62. Stack st,z;
  63. st.head=NULL;
  64. st.size=0;
  65. z.head=NULL;
  66. z.size=0;
  67. char f;
  68.  
  69. // printf("%d-%d\t%d-%d\t%d-%d\n",'(',')','[',']','{','}');
  70. while( scanf("%c",&f)==1 )
  71. {
  72. if(f=='('|| f=='['|| f=='{')
  73. {
  74. push(&st,f);
  75. }
  76. else
  77. {
  78. if(f==')')
  79. {
  80. if(st.head==NULL)
  81. {
  82. printf(":(");
  83. return 0;
  84. }
  85. else if(st.head->value=='(') pop(&st);
  86. else
  87. {
  88. printf(":(");
  89. return 0;
  90. }
  91. }
  92. else if(f==']')
  93. {
  94. if(st.head==NULL)
  95. {
  96. printf(":(");
  97. return 0;
  98. }
  99. else if(st.head->value=='[') pop(&st);
  100. else
  101. {
  102. printf(":(");
  103. return 0;
  104. }
  105. }
  106. else if(f=='}')
  107. {
  108. if(st.head==NULL)
  109. {
  110. printf(":(");
  111. return 0;
  112. }
  113. else if(st.head->value=='{') pop(&st);
  114. else
  115. {
  116. printf(":(");
  117. return 0;
  118. }
  119. }
  120. }
  121. }
  122. if(st.head==NULL)
  123. {
  124. printf(":)");
  125. // printf("%d",st.size);
  126. return 0;
  127. }
  128. else
  129. {
  130. printf(":(");
  131. // printf("%d",st.size);
  132. return 0;
  133. }
  134. // printAll(&st);
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement