CS210
Tutorial 4

 

 

1.      Assume that Stack is the class described in section 7.2 of your text with StackType set to char and STACK_CAPACITY or myCapacity set to 5. Give the value of myTop and the contents of the array myArray in the stack s after the code segment is executed or indicate why an error has occurred:

     

      Stack s;

      char x, y,z ;

 

      x = ‘D’;

      y = ‘B’;

      z = ‘C’;

      s.push(x);

      s.push(y);

      s.push(‘y’);

      x=s.top();

      s.pop();

      y=s.top();

      s.push (z);

      s. push(x);

      cout << x << endl <<  y << endl<<  z  << endl;

      while (! s.empty( )){

                  y = s.top();

                  cout << y << endl;

                  }

s.push(y);

s.push(z);

 

2.      Write documentation, a prototype, and a definition for a member function bottom() for a Stack class that returns the bottom element of the stack. Assume static-array implementation.

3.      Repeat exercise 3 above, assuming a linked list implementation of the stack as described in section 7.3 of your text.

4.      Repeat exercise 2, but for a function bottom() that is neither a member function nor a friend function of the class Stack.

5.      Assume that a=7.0, b=4.0, c=3.0 and d=-2.0. Assume also that ~ represents the unary minus operation. Evaluate the postfix expressions:

a .     a b c d + / *

b .    a b c + / d *

c .     a  ~ b  c  + -

6.Repeat exercise 5 tracing the contents of the operator stack used for evaluation.

7.Convert the postfix expressions in exercise 5 to infix form.

8.Convert the infix to postfix:

a .     a * b + c – d

b .    (a + b) / c + d

c .     ((a > 3) && ( a < 9)) || ! (a > 0)

d .    a – (b –(c –(d –e)))

Hosted by www.Geocities.ws

1