正在加载图片...
3. Applications Balancing Symbols Check if parenthesis(), brackets[], and braces() are balanced Algorithm i Make an empty stack S TONDO(N) while(read in a character c)t where n is the length if(c is an opening symbol) of the expression Push(, S) This is an else if (c is a closing symbol) if(s is empty)I ERROR; exit; y on-line algorithm else/stack is okay if(Top(s)doesnt match c)I ERROR, exit; 1 else Pop(s); 1 / end else-stack is okay * 3/end else-if-closing symbol * 3/end while-loop*/ if(s is not empty) ERROR3. Applications  Balancing Symbols Check if parenthesis ( ), brackets [ ], and braces { } are balanced. Algorithm { Make an empty stack S; while (read in a character c) { if (c is an opening symbol) Push(c, S); else if (c is a closing symbol) { if (S is empty) { ERROR; exit; } else { /* stack is okay */ if (Top(S) doesn’t match c) { ERROR, exit; } else Pop(S); } /* end else-stack is okay */ } /* end else-if-closing symbol */ } /* end while-loop */ if (S is not empty) ERROR; } T( N ) = O ( N ) where N is the length of the expression. This is an on-line algorithm
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有