CSC235 Data Structures - Stack Homework


  1. Show what is written by the following segments of code. Suppose st is a stack of ints and ok, a, b, c, x, y, and z are int variables.
    
    a.   x = 3;                          b.   y = 1;
         y = 5;                               Stack st;
         z = 2;                               ok = st.push(5);
         Stack st;                            ok = st.push(7);
         ok = st.push(x);                     ok = st.pop(x);
         ok = st.push(4);                     x = x + y;
         ok = st.pop(z);                      ok = st.pop(x);
         ok = st.pop(y);                      ok = st.push(x);
         ok = st.push(y);                     ok = st.push(y);
         ok = st.push(3);                     ok = st.push(2);
         ok = st.push(2);                     ok = st.pop(y);
         ok = st.pop(x);                      ok = st.pop(x);
         ok = st.push(2);                     while (!st.isEmpty())
         ok = st.push(x);                     {
         while (!st.isEmpty())                   ok = st.pop(y);
         {                                       cout << y << endl;
            ok = st.pop(x);                   }
            cout << x << endl;                cout << "x = " << x << endl;
         }                                    cout << "y = " << y << endl;
    
    
    c.   x = 0;                          d.   a = 0;
         y = 5;                               b = 5;
         z = y / 2;                           c = 4;
         ok = st.push(x);                     ok = st.push(a);
         ok = st.push(y);                     ok = st.push(b);
         ok = st.pop(z);                      ok = st.pop(c);
         ok = st.push(x+1);	                  a = a + 1;
         ok = st.push(y);                     ok = st.push(a);
         ok = st.push(3);                     ok = st.push(c);
         while (!st.isEmpty())                ok = st.push(b);
         {                                    b = a + b;
            ok = st.pop(z);	                  ok = st.push(b);
            cout << z << endl;                ok = st.push(7);
         }                                    while (!st.isEmpty())
         cout << "x = ",x;                    {			   		
         cout << "y = ",y;                       ok = st.pop(a);
         cout << "z = ",z;                       cout << a << "  ";
                                              }
    
    
  2. Suppose we use the array implementation for a stack st. Show what is on the stack st initially, and then show the contents of top, ch, and the array after each of the following operations. Indicate if there is an attempt to push to a full stack or pop from an empty stack. For each problem, start with the stack shown (not the result of the previous problem.)
    
       [0] [1] [2] [3] [4]
    a.  B   C   D   F   O  	    Top = 1    ch = 'X'
                                ok = st.pop(ch);
    
        [0] [1] [2] [3] [4]
    b.   X   Y   Z   A   B      Top = 4    ch = 'C'
                                ok = st.push('A');
    
        [0] [1] [2] [3] [4]
    c.   D   J   K   P   Q      Top = 3    ch = 'X'
                                ok = st.push(ch);
    
        [0] [1] [2] [3] [4]
    d.   R   V   M   H   A      Top = -1   ch = 'C'
                                ok = st.pop(ch);
    
    
  3. Write a function to copy one stack to another stack and leave the first stack unchanged. Use the stack member functions.

  4. Write a function that will sum up all the negative values on a stack of integers. Leave the stack unchanged and return the sum in a var parameter. Use the stack member functions.

  5. Write a function that will delete all the elements of a character stack that contain the value 'A' while preserving the order of the other elements. Use the stack member functions.

  6. Write a function called REPLACES which takes as arguments a character stack and two character variables OLDCH and NEWCH. If the value OLDCH is found anywhere in the stack, replace it with the second value NEWCH, while preserving the order of the values on the stack. If OLDCH is not found on the stack, return the stack unchanged. Use the stack member functions.

  7. Evaluate the following postfix expressions (suppose A = 7.0, B = 4.0, C = 3.0, and D = -2.0):
            A.  A B + C / D *
            B.  A B + C - D +
            C.  A B C + / D *
            D.  A B * C - D /
    
  8. Convert the following infix expressions to postfix:
            A.  A * B + C - D
            B.  (A + B) / C - D
            C.  (A - B) * (C - (D + E))
            D.  (((A - B) * C) + D) / E)
    

  9. Email Me | Office Hours | My Home Page | Department Home | MCC Home Page