Logo 
Search:

C++ Programming FAQ

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

Write an algorithm for Parenthesis infix to suffix expression in dfs (data file structure).

  Shared By: Molly Brown    Date: Nov 24    Category: C++ Programming    Views: 1341

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 7 while NEXT != ‘’.

5. [Remove symbols with greater precedence from stack]

If top < 1
then write (‘Invalid’)
exit

Repeat while
F(NEXT <= G (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. [Are there matching parenthesis]

If F(NEXT != G (s[top])
then call push (s,top,NEXT)
else
pop (s,top).

7. [Get next input symbol]

NEXT <-- call NEXTCHAR (INFIX).

8. [Checking validity of total expression]

If (RANK != 1 or top != 0)
then write (‘Invalid Expression’)
else
write (‘Valid Expression’).

9. [FINISH]
return.

Share: 
 



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


Tagged: