JMU
Sample Questions for Exam 2


  1. Answer the sample questions for exam 1.
  2. Carefully define each of the following:
    Abstract Data Type


    Queue


    Class


  3. Indicate whether each of the following statements is true or false:
    (1) _____ Contiguous implementations are always preferred to linked implementations.
    (2) _____ C++ templates are used to implement "generic" ADTs.
    (3) _____ A contiguous implementation of a List is an array.
  4. Given the following specification of the Queue class:
    class Queue
    {   
        public:
    	Queue();
    
    	// Returns and removes the front item from the Queue 
    	int dequeue();
    
            // Add the item x onto the rear of the Queue
    	void enqueue(int x);	
    
    
        private: 
    
            int space[10];  
            int front, rear, size, count;              
    };
        

    and the following partial implementation of the Queue class:

    Queue::Queue()
    {
        size  = 10;
    
        front = size - 1; 
        rear  = size - 1; 
        count = 0; 
    }
    
    
    void Queue::enqueue(int x)
    { 
        if (count == size) 
        {
            cerr << "\n Full \n";
        }
        else
        {
            space[rear] = x;
            count++;
            rear = (rear+1) % size;
        }
    }
    

    complete the implementation of the Queue class (i.e., write the dequeue() method).

  5. Given the following specification of a Node:
    class Node
    {
      public:
          int     value;
          Node    *next;
    }
      

    and the following specification/implementation of a Pew:

    class Pew
    {
      public:
        Pew();
        void pish(int anInt);
    
      private:
        Node   *last;
    }
    
    
    
    Pew::Pew()
    {
        last  = NULL;
    }
    
    
    void Pew::pish(int anInt)
    {
        Node *temp;
    
        temp = new Node();
    
        temp->value = anInt;
        temp->next  = last;
    
        last = temp;
    }
      

    draw a model of memory that illustrates what happens when the following statements are executed:

      Pew   *p;
    
      p = new Pew();
    
      p->pish(22);
    
      p->pish(33);
    
      p->pish(44);
      
  6. A Virginia Tech student implemented an endogenous, singly-linked, circular linked list using the following Node class:
      class Node
      {
        public:
    
          Node  *next;
          int   data;
      };
      

    His LinkedList class included, among other things, two Node pointers, one named head that points at the first element in the list and one named tail that points at the last element in the list.

    He was then asked to implement a method for finding the minimum data point in the linked list. He implemented this method as follows:

      int findMinimum(Node *head)
      {
        int  min;
        Node *current;
    
        min = MAX_INT; // MAX_INT is the largest possible int
        current = head;
        while (current != NULL)
        {
          if (current->data < min) min = current->data;
          current = current->next;
        }
    
        return min;
      }
      

    Not surprisingly, his implementation does not work properly. Explain the problem(s) with his implementation and correct it.

  7. Write a function that will merge two linked lists of integers, assuming that they are sorted in ascending order. The merged list should, itself, be sorted in ascending order.
  8. Consider the following MultiStack class:
    const int NUM_STACKS = 5;
    const int CAPACITY   = 32;
    
    class MultiStack {
       private:
          int store[NUM_STACKS][CAPACITY];
          int topIndex[NUM_STACKS];
       public:
          MultiStack( void );
          void push( int whichStack, int value );
          int pop( int whichStack );
          bool isEmpty( int whichStack );
    
    }; // class MultiStack
    
    
    MultiStack::MultiStack( void )
    { 
      for ( int i = 0; i < NUM_STACKS; i++ ) 
        topIndex[i] = 0; 
    } 
    
    
    void MultiStack::push( int whichStack, int value ) 
    {
       if ( topIndex[whichStack] == CAPACITY ) return;
    
       store[whichStack][topIndex[whichStack]] = value;
       topIndex[whichStack]++;
    } // push
    
    
    
    int MultiStack::pop( int whichStack ) 
    {
       if ( topIndex[whichStack] == 0 ) return 0;
    
       topIndex[whichStack]--;
       return store[whichStack][topIndex[whichStack]];
    } // pop
    
    
    
    bool MultiStack::isEmpty( int whichStack )
    { 
      return topIndex[whichStack] == 0; 
    }
    
    1. In a sentence, explain what this data structure holds.
    2. What PRECISELY do the values stored in the topIndex array represent?
    3. If a client tries to push data on a full stack, or remove data from an empty stack, this code will detect but ignore the error. What error is not only ignored but not detected at all (in other words, there is a systematic bug in this code, what is it)?
    4. Write the isFull() method for this class.
  9. A priority queue is an abstract data type in which each value entered has an associated priority. The highest priority values move to the front of the priority queue. The next value to leave the priority queue is always the value with the highest priority that has been in the priority queue the longest. One way to implement a priority queue is with an array of pointers to (regular) queues. Each queue holds values of equal priority. When a value enters the priority queue, it is placed in the regular queue that matches its priority; values leave the priority queue starting with the regular queues of highest priority. Consider the following PriorityQueue class for Jobs with 10 possible priority levels that uses Queues of (pointers to) Jobs:
      #include "Underflow.h"    // underflow exception class
      #include "Job.h"          // jobs with associated priorities
      #include "Queue.h"        // queue of (pointers to) jobs
    
      class PriorityQueue {
         public:
            PriorityQueue( void );
            void enter( Job *aJob );
            Job *leave( void ) throw ( Underflow );
            Job *front( void ) throw ( Underflow );
      
         private: 
            Queue *jobQueue[10]
      };
      
    1. Write the PriorityQueue constructor.
    2. Write the enter() function. Assume that the Job class has a getPriority() operation that returns an integer between 0 (the lowest priority) and 9 (the highest priority).
    3. Write the leave() function.

Copyright 2010