CSC235 Data Structures - Queue Homework


  1. Show what is contained in the queue qq and the value of ch1 and ch2, or a and b, after the completion of each of the following operations:
    
    a.   Queue qq;	                        b.   Queue qq;
         ok = qq.insert('a');                    ok = qq.insert(4);
         ok = qq.insert('b');                    ok = qq.insert(8);
         ok = qq.del(ch1);                       ok = qq.insert(2);
         ok = qq.insert('b');                    ok = qq.del(a);
         ok = qq.insert('f');                    ok = qq.del(b);
         ok = qq.insert('z');                    ok = qq.insert(a+b);
         ok = qq.del(ch2);                       ok = qq.insert(a*b);
         ok = qq.insert(ch2);                    ok = qq.del(a);
         ok = qq.del(ch2);                       ok = qq.insert(a+b);
    
    
  2. Show what is written by the following segments of code:
    
    a.   Queue qq;                          b.   Queue qq;
         ok = qq.insert(5);                      x = 2;
         ok = qq.insert(6);                      ok = qq.insert(4);
         ok = qq.insert(7);                      ok = qq.insert(x);
         ok = qq.insert(8);                      ok = qq.del(y);
         ok = qq.del(x);                         ok = qq.insert(y+3);
         ok = qq.del(y);                         ok = qq.insert(0);
         ok = qq.insert(x);                      ok = qq.del(z);
         ok = qq.insert(y+1);                    x = y + z;
         ok = qq.del(x);                         ok = qq.insert(x);
         ok = qq.insert(y);                      ok = qq.insert(x-1);
         while (!qq.isEmpty())                   ok = qq.insert(x);
         {                                       while (!qq.isEmpty())
            ok = qq.del(x);                      {
            cout << x << endl;                      ok = qq.del(x);
         }                                          cout << x << endl;
                                                 }
    
    
    c.   Queue qq;                          d.   Queue qq;
         x = 5;                                  ok = qq.insert(5);
         y = 7;                                  ok = qq.insert(6);
         ok = qq.insert(x);                      ok = qq.insert(10);
         ok = qq.insert(5);                      ok = qq.del(a);
         ok = qq.insert(y);                      ok = qq.del(b);
         ok = qq.del(y);                         ok = qq.insert(a+2);
         ok = qq.insert(2);                      ok = qq.insert(a-b);
         ok = qq.insert(x);                      ok = qq.insert(b+a);
         ok = qq.insert(y);                      ok = qq.insert(a*b);
         z = x - y;                              while (!qq.isEmpty()) 
         if (z == 0)                             {
            while (!qq.isEmpty())                   ok = qq.del(c);
            {                                       cout << a << b << c << endl;
               ok = qq.del(x);                   }
               cout << x << endl;
            }
         else
            cout << "the end\n";
    
    
  3. Show what is on the Queue qq initially, and then show the contents of Front, Back, Count, and the array after each of the following operations. Show the contents of value for any remove operations, and indicate if there is an attempt to add to a full queue or remove from an empty queue. For each problem, start with the queue shown (not the result of the previous problem.)
    
        [0] [1] [2] [3] [4]
    a.   A   B   C   D   E       Front = 1     Back = 4     Count = 4
                                 ok = qq.insert('F');
    
        [0] [1] [2] [3] [4]
    b.   A   B   C   D   E       Front = 2     Back = 0     Count = 4
                                 ok = qq.del(value);
    
        [0] [1] [2] [3] [4]
    c.   A   B   C   D   E       Front = 4     Back = 3     Count = 5
                                 ok = qq.insert('G');
    
        [0] [1] [2] [3] [4]
    d.   A   B   C   D   E       Front = 2     Back = 1     Count = 0
                                 ok = qq.del(value);
    
        [0] [1] [2] [3] [4]
    e.   A   B   C   D   E       Front = 3     Back = 0     Count = 3
                                 ok = qq.insert('H');
    
        [0] [1] [2] [3] [4]
    f.   A   B   C   D   E       Front = 1     Back = 3     Count = 3
                                 ok = qq.del(value);
    
    
  4. Write a function to copy qq1 to qq2 and leave qq1 unchanged. Use the queue member functions to access the queues.
  5. Write a function to count and return the number of integers in a queue which are less than zero. Leave the queue unchanged. Use the queue member functions to access the queue.
  6. Explain what the following code does to the Queue qq:
    
         Stack st;
         while (!qq.isEmpty()) 
         {
            ok = qq.del(value);
            ok = st.push(value);
         }
         while (!st.isEmpty())
         {
            ok = st.pop(value);
            ok = qq.insert(value);
         }
    
    
  7. Assume you have two queues qq1 and qq2. Write a function called match that will count how many elements in the same position of the two queues are equal. The queues may not be the same length. Use a reference parameter to pass the count back to the caller. Do not modify the queues. Use the queue member functions to access the queues.

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