What is the complexity of the following c code segment Show
Solution
2. Given: 2 3 6 5 - 4 + *:
read 2: it is operand . push into stack . Stack: 2
read 3: it is operand . push into stack . Stack: 2 3
read 6: it is operand . push into stack . Stack: 2 3 6
read 5: it is operand . push into stack . Stack: 2 3 6 5
read -: it is operator . Pop 2 elements from stack and perform -.
6 - 5=> 1 . Store this result in stack.
Stack: 2 3 1
read 4: it is operand . push into stack . Stack: 2 3 1 4
read +: it is operator. Pop 2 elements from stack and perform +.
4 + 1=> 5 . Store this result in stack.
Stack: 2 3 5
read *: it is operator. Pop 2 elements from stack and perform *.
3 * 5=> 15 . Store this result in stack.
Stack: 2 15
Given expression is processed but still stack contains elements. So it is an error.
3. Given: 2 3 4 6 5 + + *
read 2: it is operand . push into stack . Stack: 2
read 3: it is operand . push into stack . Stack: 2 3
read 4: it is operand . push into stack . Stack: 2 3 4
read 6: it is operand . push into stack . Stack: 2 3 4 6
read 5: it is operand . push into stack . Stack: 2 3 4 6 5
read +: it is operator. Pop 2 elements from stack and perform +.
6 + 5=> 11 . Store this result in stack.
Stack: 2 3 4 11
read +: it is operator. Pop 2 elements from stack and perform +.
4 + 11=> 15 . Store this result in stack.
Stack: 2 3 15
read *: it is operator. Pop 2 elements from stack and perform *.
3 * 15=> 45 . Store this result in stack.
Stack: 2 45
Given expression is processed but still stack contains elements. So it is an error.
4. New items enter a queue at its back side , and items leave a queue from its front side.
5. FRONT=-1; REAR=-1;
These are initialized like this to indicate the queue is empty.
6.
for(Node * cur=head;cur!=NULL;cur=cur->next)
cout<<cur->item<<endl;
