Logo 
Search:

C++ Programming FAQ

Submit Interview FAQ
Home » Interview FAQ » C++ ProgrammingRSS Feeds

Write an algorithm for Unparenthesis infix to suffix expression in dfs.

  Shared By: Bailey Evans    Date: Dec 15    Category: C++ Programming    Views: 1111

Answer:

1. [Initialize Stack]

top <-- 1
s[top] <-- ‘#’.

2. [Initialize output string and rank count]

POLISH <-- ‘ ‘
RANK <-- 0.

3. [Get first input string]

NEXT <-- call NEXTCHAR(INFIX).

4. [Translate infix expression]

Repeat thru step 6 while NEXT != ‘#’.

5. [Remove symbols with greater or equal precedence from stack]

Repeat while
F(NEXT <= F (s[top])
X <-- call pop(s , top)
POLISH <-- POLISH O X
RANK <-- RANK +call R(X)
If ( RANK < 1)
then write (Invalid Expression”)
exit.

6. [Pushing current element to stack]

call push (s , top , NEXT)
NEXT <-- call NEXTCHAR(INFIX).

7. [Moving remaining stack elements to polish]

Repeat while s[top] != ‘#’
X <-- call pop(s , top)
POLISH <-- POLISH O X
RANK <-- RANK + call R(X)
If ( RANK < 1)
then write (Invalid Expression”)
exit.

8. [Checking validity of total expression]

If (RANK != 1)
then write (Invalid Expression”).

9. [FINISH]
return.

Share: 
 



Your Comment
  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].


Tagged: