c - Balanced Parantheses Using Stack -


I am trying to construct a balanced bracket algorithm using the pile in the CI that way the parentheses are the same (like "( ) "Or" {} "or" [] ") but when they get mixed then I am getting a wrong answer

What is the error in my code?

   int count = 0; Typedef configuration node {struct node * next; Four data; } Node; Node * top; Node * Local; Node * temp; Create zero () {top = NULL; } Zero push (four data) {if (top == NULL) {top = malloc (sizeof (node)); Top & gt; Next = zero; Top & gt; Data = data; } Else {temp = malloc (sizeof (node)); Temp- & gt; Data = data; Temp- & gt; Next = top; Top = temporary; } Calculate ++; } Four pop () {local = top; If (local == faucet) {printf ("empty stack \ n"); Return; } And {local = local-> next; } // printf ("% d \ n", top-> data); Return to top- & gt; Data; Free (Up); Top = local; Count--; } Et blank () {return top == tap; } Int match (four A, four B) {if (a == '[' & amp; amp; amp; b == ']') Return 1; If (a == '{' & amp; amp; amp;; b == '}') return 1; If (a == '(' and amp; b == ')') 1 return; Return 0; } / * Match end () * / int main () {int no, ch, e; Four str [51]; Scanf ("% s", str); Int z; Int i; Four floating; Int balanced = 1; Z = strlen (STR); (I = 0; i & lt; z; i ++) {if (str [i] == '(' str [i] == '{'} str [i] == '[' ] Push (str [I]); And if (str [i] == ')' || str [i] == '}' || str [i] == ']') {temp = pop () ; If (empty) balanced = 0; if (match! (Str [i], floating) balanced = 0; balanced = 1;}} printf ("% d \ n", balanced); return 0;}  

`

ignoring the problem in your pop () I & lt; strlen (str); ++ i for the function and your remaining pile code, I like balance algorithm:

  int balanced = 0; ) {If (str [i] == '(' || str [i] == '{' || str [i] == '['] [push (str [i]); ++ satisfied ;) And if (str [i] == ')' || str [i] == '}' || str [i] == ']') {Temp = pop (); if (match (str [ I], temporarily) - balanced;}} Printf ("% d \ n", balanced);  

If balanced is finally 0, then you know If you are positive then you are missing or unbalanced closing brackets.


Comments

Popular posts from this blog

apache - 504 Gateway Time-out The server didn't respond in time. How to fix it? -

c# - .net WebSocket: CloseOutputAsync vs CloseAsync -

c++ - How to properly scale qgroupbox title with stylesheet for high resolution display? -