39998

Solution 1:

The Iterator types define for traversing the items in a sequence. Iterators have same advantages and power as the pointers. But same time, there are also risks and inconveniences. We can accidentally modify data that we are not supposed to. This can be done with the help of pointer. But we can partially solve that problem by using const modifier.

The const_iterator provides basically the same functionality as a regular iterator. But there is one exception that data cannot be modified because modifying the data “pointed to” by the const_iterator is disallowed.

 

Solution 2:

a) Insert a new element after the current iterator position: const_iterator

b) Replace the data stored in the currently selected item: iterator

c) Retrieve the data stored in the currently selected item: const_iterator

d) Insert a new element before the currently selected item: iterator

Solution 4:

Expression: 10 2 * 5 / 6 2 5 * + –

Expression Action Stack
10 2 * 5 / 6 2 5 * + –

­

Push 10

10

10 2 * 5 / 6 2 5 * + –

­

Push 2

2

10

10 2 * 5 / 6 2 5 * + –

­

Operator: *
Pop 2 and 10 and Evaluate 10 * 2.

Push 20

20

10 2 * 5 / 6 2 5 * + –

­

Push 5

5

20

10 2 * 5 / 6 2 5 * + –

­

Operator: /
Pop 5 and 20 and Evaluate 20 / 5.

Push 4

4

10 2 * 5 / 6 2 5 * + –

­

Push 6

 

6

4

10 2 * 5 / 6 2 5 * + –

­

Push 2

2

6

4

10 2 * 5 / 6 2 5 * + –

­

Push 5

5

2

6

4

10 2 * 5 / 6 2 5 * + –

­

Operator: ‘*’
Pop 5 and 2 and Evaluate 2*5.

Push 10

10

6

4

10 2 * 5 / 6 2 5 * + –

­

Operator: ‘+’
Pop 10 and 6 and Evaluate 6 + 10

Push 16

16

4

10 2 * 5 / 6 2 5 * + –

­

Operator:  ‘-‘
Pop 16 and 4

Evaluate 4 – 16

Push -12

-12

10 2 * 5 / 6 2 5 * + –

­

Pop -12

Stack is empty

Result is -12

 

Solution 5:

Expression: 10 * 5 / 6 2 5 * + –

Expression Action Stack
10 * 5 / 6 2 5 * + –

­

Push 10

10

10 * 5 / 6 2 5 * + –

­

Operator: *
Pop 10 and throw an exception

 

It will throws an exception, because to perform ‘*’, there should be one more operand along with 10.

 

Solution 6:

Expression:  y – 7 * 35 + 4 / 6 – 10

Token Action OpStack PostFix
  Add to the end of PostFix   y
Push –   y
7 Add to the end of PostFix y 7
* Prec * > –

Push *

*

y 7
35 Add to the end of PostFix *

y 7 35
+ Prec + < *

Pop * and add to the end of PostFix

Prec + = –

Pop – and add to the end of PostFix

Push +

+ y 7 35 * –
4 Add to the end of PostFix + y 7 35 * – 4
/ Prec / > +

Push /

/

+

y 7 35 * – 4
6 Add to the end of PostFix /

+

y 7 35 * – 4 6
Prec – < /

Pop / and add to the end of PostFix

Prec – = +

Pop + and add to the end of PostFix

Push –

y 7 35 * – 4 6 / +
10 Add to the end of PostFix y 7 35 * – 4 6 / + 10
  Pop – and add to the end of PostFix   y 7 35 * – 4 6 / + 10 –

 

Expression:  ( x + 15 ) * ( 3 * ( 4 – (5 + 7 / 2 ) ) )

Token Action OpStack PostFix
( Push ( (  
x Add to the end of PostFix ( x
+ Prec + > (

Push +

+

(

x
15 Add to the end of PostFix +

(

x 15
) Prec ) < +

Pop +  and add to the end of PostFix

Matching ) to (

Pop (

  x 15 +
* Push * * x 15 +
( Push ( (

*

x 15 +
3 Add to the end of PostFix (

*

x 15 + 3
* Prec * > (

Push *

*

(

*

x 15 + 3
( Push ( (

*

(

*

x 15 + 3
4 Add to the end of PostFix (

*

(

*

x 15 + 3 4
Prec – > (

Push –

(

*

(

*

x 15 + 3 4
( Push ( (

(

*

(

*

x 15 + 3 4
5 Add to the end of PostFix (

(

*

(

*

x 15 + 3 4 5
+ Prec + > (

Push +

+

(

(

*

(

*

x 15 + 3 4 5
7 Add to the end of PostFix +

(

(

*

(

*

x 15 + 3 4 5 7
/ Prec / > +

Push /

/

+

(

(

*

(

*

x 15 + 3 4 5 7
2 Add to the end of PostFix /

+

(

(

*

(

*

x 15 + 3 4 5 7 2
) Prec ) < /

Pop /  and add to the end of PostFix

Prec ) < +

Pop +  and add to the end of PostFix

Matching ) to (

Pop (

(

*

(

*

 

 

 

x 15 + 3 4 5 7 2 / +
) Prec ) < –

Pop –  and add to the end of PostFix

Matching ) to (

Pop (

*

(

*

x 15 + 3 4 5 7 2 / + –
) Prec ) < *

Pop *  and add to the end of PostFix

Matching ) to (

Pop (

* x 15 + 3 4 5 7 2 / + – *
blank Pop *  and add to the end of PostFix

 

  x 15 + 3 4 5 7 2 / + – **

 

 

Solution 8:

Algorithm to display all the elements in a queue:

Display() method
j = front
MAX = rear
while loop from j to MAX
print element at index ‘j’
j = j + 1
While loop end
Display() End