Abstract Data Type |
Queue |
Class |
(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. |
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).
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);
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.
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; }
topIndex
array represent?
isFull()
method for this class.
PriorityQueue
class for Job
s with 10
possible priority levels that
uses Queue
s of (pointers to) Job
s:
#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] };
PriorityQueue
constructor.
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).
leave()
function.
Copyright 2010