Data Structures & AlgorithmsVery hardGATE CS
The infix expression a*b^c^d-(e+f*g)^h/i+j (^ is right-associative with highest precedence, then * and /, then + and -, all others left-associative) is converted to postfix with the standard operator-stack algorithm. What is the maximum number of symbols (operators and parentheses) on the stack at any instant, and what is the postfix result?
- A. 4 and abcd^^*efg*+h^i/-j+
- B. 5 and abcd^^*efg*+h^i/-j+
- C. 4 and abc^*efg*+h^i/-j+
- D. 3 and +j-/i^h+*gfe*^^dcba
Correct answer: A. 4 and abcd^^*efg*+h^i/-j+
Scanning with right-associative ^ (equal-precedence ^ is not popped) the stack peaks at 4 symbols and the postfix is abcd^^*efg*+h^i/-j+.
Practise more Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questionsArrays, Strings, Linked Lists, Stacks, Queues and Hashing: notes and key points
More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions
- For the pattern P = "abaabaabaabab", the KMP failure (prefix) function pi[i] is the length of the longest proper prefix of P[0..i] that is…
- The naive algorithm searches for P = "aaaab" in T = "aaabaaabaaaab": for each shift s = 0, 1, ..., it compares P[0], P[1], ... with T[s]…
- A queue is built from two stacks: an enqueue pushes onto the input stack; a dequeue pops from the output stack, and if the output stack is…
- From the digit string 1432219836475, exactly 5 digits are deleted (the remaining digits keep their order, and leading zeros are not an…
- An array next = [10, 6, 10, 1, 1, 9, 5, 5, 10, 3, 7] defines a linked structure in which the node after index i is next[i]. Floyd's…
- Two sorted lists A = [2, 4, 4, 7, 9, 12, 15, 18, 21] and B = [1, 4, 5, 6, 13, 14, 16, 17] are merged with the standard merge procedure…