Question

Data Structures using C++ Consider the classes QueueADT and ArrayQueueType: QueueADT: #ifndef QUEUEADT_H #define QUEUEADT_H template...

Data Structures using C++

Consider the classes QueueADT and ArrayQueueType:

QueueADT:

#ifndef QUEUEADT_H

#define QUEUEADT_H

template <class ItemType>

class QueueADT

{

public:

       // Action responsibilities

       virtual void resetQueue() = 0;

          // Reset the queue to an empty queue.

          // Post: Queue is empty.

       virtual void add(const ItemType& newItem) = 0;

          // Function to add newItem to the queue.

          // Pre: The queue exists and is not full.

          // Post: The queue is changed and newItem is added to the

          //       back of the queue.

       virtual void remove() = 0;

          // Function to remove the first element of the queue.

          // Pre: The stack exists and is not empty.

          // Post: The queue is changed and the first (i.e., front) item is

          //       removed from the queue.

       // Knowledge responsibilities

       virtual bool isEmpty() const = 0;

          // Function to determine whether the queue is empty.

          // Post: Returns true if queue is empty; false otherwise.

       virtual bool isFull() const = 0;

          // Function to determin whether the queue is full.

          // Post: Returns true if there is room for another item;

          //       false otherwise.

       virtual ItemType front() const = 0;

          // Function to return the first element of the queue.

        // Pre: The queue exists and is not empty.

          // Post: If the queue is empty, the program terminates; otherwise,

          //       the first element of the queue is returned.

      virtual ItemType back() const = 0;

          // Function to return the last element of the queue.

        // Pre: The queue exists and is not empty.

          // Post: If the queue is empty, the program terminates; otherwise,

          //       the last element of the queue is returned.

};

ArrayQueueType:

#ifndef ARRAYQUEUETYPE_H

#define ARRAYQUEUETYPE_H

#include <iostream>

#include <new>        // for bad_alloc exception

#include <cstdlib>    // for the exit function

#include "QueueADT.h"

using namespace std;

const int MAX_QUEUE_SIZE = 100; // Maximum number of components

template <class ItemType>

class ArrayQueueType: public QueueADT<ItemType>

{

public:

       // Constructor

       ArrayQueueType();

          // Default constructor.

          // Post: An empty queue has been created. The variable list holds

          //       the base address of the array.

       ArrayQueueType(const ArrayQueueType<ItemType>& otherQueue);

          // Copy constructor for deep copy of the array-based queues.

       // Destructor

       ~ArrayQueueType();

          // Delete all the queue items.

          // Post: The array (list) holding the queue elements is deleted.

       // Action responsibilities

       void resetQueue();

          // Reset the queue to an empty queue.

          // Post: Queue is empty.

       void add(const ItemType& newItem);

          // Function to add newItem to the queue.

          // Pre: The queue exists and is not full.

          // Post: The queue is changed and newItem is added to the

          //       back of the queue.

       void remove();

          // Function to remove the first element of the queue.

          // Pre: The stack exists and is not empty.

          // Post: The queue is changed and the first (i.e., front) item is

          //       removed from the queue.

       // Knowledge responsibilities

       bool isEmpty() const;

          // Post: Returns true if queue is empty; false otherwise.

       bool isFull() const;

          // Post: Returns true if there is room for another item;

          //       false otherwise.

       ItemType front() const;

          // Function to return the first element of the queue.

        // Pre: The queue exists and is not empty.

          // Post: If the queue is empty, the program terminates; otherwise,

          //       the first element of the queue is returned.

       ItemType back() const;

          // Function to return the last element of the queue.

          // Pre: The queue exists and is not empty.

          // Post: If the queue is empty, the program terminates; otherwise,

          //       the last element of the queue is returned.

       // Operator overloading

       const ArrayQueueType<ItemType>& operator=

                     (const ArrayQueueType<ItemType>& otherQueue);

          // Overload the assignment operator.

protected:

       int length; // variable to store the number of items in the queue

       int queueFront; // variable to store the index of the first item

                       // of the queue

       int queueBack; // variable to store the index of the last item

                       // of the queue

       ItemType *list; // pointer to the array that holds the queue items

private:

       void copyQueue(const ArrayQueueType<ItemType>& otherQueue);

          // Function to make a copy of otherQueue.

          // A deep copy of otherQueue is created and assigned to this queue.

};

//**************************************************************

template <class ItemType>

ArrayQueueType<ItemType>::ArrayQueueType()

{

       try

       {

              queueFront = 0;

              queueBack = MAX_QUEUE_SIZE - 1;

              length = 0;

              list = new ItemType[MAX_QUEUE_SIZE]; // create the array to

                                                   // hold the queue items

       }// end try

       catch (bad_alloc)

       {

              cout << "ERROR: Cannot allocate memory!\n";

              exit(EXIT_FAILURE);

       }

}

//**************************************************************

template <class ItemType>

ArrayQueueType<ItemType>::ArrayQueueType

                (const ArrayQueueType<ItemType>& otherQueue)

{

       try

       {

              list = new ItemType[MAX_QUEUE_SIZE];

       }// end try

       catch (bad_alloc)

       {

              cout << "ERROR: Cannot allocate memory!\n";

              exit(EXIT_FAILURE);

       }

       copyQueue(otherQueue);

}

//**************************************************************

template <class ItemType>

ArrayQueueType<ItemType>::~ArrayQueueType()

{

       delete [] list; // Deallocate the memory occupied by the array.

}

//**************************************************************

template <class ItemType>

void ArrayQueueType<ItemType>::resetQueue()

{

       queueFront = 0;

       queueBack = MAX_QUEUE_SIZE - 1;

       length = 0;

}

//**************************************************************

template <class ItemType>

void ArrayQueueType<ItemType>::add(const ItemType& newItem)

{

       if ( !isFull() )

       {

              queueBack = (queueBack + 1) % MAX_QUEUE_SIZE; // use the mod

                 // operator to advance queueBack because the array is circular

              list[queueBack] = newItem; // add newItem to the back

              length++;

       }

       else

              cout << "Cannot add to a full queue." << endl;

}

//**************************************************************

template <class ItemType>

void ArrayQueueType<ItemType>::remove()

{

       if ( !isEmpty() )

       {

              length--;

          queueFront = (queueFront + 1) % MAX_QUEUE_SIZE; // use the mod

                 // operator to advance queueFront because the array is circular

       }

       else

              cout << "Cannot remove from an empty queue." << endl;

}

//**************************************************************

template <class ItemType>

bool ArrayQueueType<ItemType>::isEmpty() const

{

       return ( length == 0 );

}

//**************************************************************

template <class ItemType>

bool ArrayQueueType<ItemType>::isFull() const

{

       return ( length == MAX_QUEUE_SIZE );

}

//**************************************************************

template <class ItemType>

ItemType ArrayQueueType<ItemType>::front() const

{

       if ( !isEmpty() )

              return list[queueFront]; // the first item is indexed

                                   // by queueFront

       else // if queue is empty, terminate the program

       {

              cout << "ERROR: Cannot return first item from an empty queue!\n";

              exit(EXIT_FAILURE);

       }

}

//**************************************************************

template <class ItemType>

ItemType ArrayQueueType<ItemType>::back() const

{

       if ( !isEmpty() )

              return list[queueBack]; // the last item is indexed

                                   // by queueBack

       else // if queue is empty, terminate the program

       {

              cout << "ERROR: Cannot return last item from an empty queue!\n";

              exit(EXIT_FAILURE);

       }

}

//**************************************************************

template <class ItemType>

const ArrayQueueType<ItemType>& ArrayQueueType<ItemType>::operator=

                    (const ArrayQueueType<ItemType>& otherQueue)

{

       if ( this != &otherQueue ) // avoid self-copying

              copyQueue(otherQueue);

       return *this;

}

//**************************************************************

template <class ItemType>

void ArrayQueueType<ItemType>::copyQueue

                    (const ArrayQueueType<ItemType>& otherQueue)

{

       length = otherQueue.length;

       queueFront = otherQueue.queueFront;

       queueBack = otherQueue.queueBack;

       for (int i = 0; i < length; i++)

              list[ (queueFront + i) % MAX_QUEUE_SIZE ]

              = otherQueue.list[ (queueFront + i) % MAX_QUEUE_SIZE ];

}

#endif

What is the effect of the following statements? If a statement is invalid, explain why it is invalid.

  (a)    QueueADT<int> newQueue;

(b) ArrayQueueType<double> sales;

(c) ArrayQueueType<string> names;

Homework Answers

Answer #1

(a) QueueADT<int> newQueue;
QueueADT is an abstract class. And an abstract class is to provide appropriate base
to the inherited classes. And it is not allowed to have any instances of type Abstract class.

(b) ArrayQueueType<double> sales;
Creates an object sales of type ArrayQueueType which takes a variables of type double.

(c) ArrayQueueType<string> names;
Creates an object names of type ArrayQueueType which takes a variables of type string.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Data Structures using C++ Consider the following class #ifndef LINKEDQUEUETYPE_H #define LINKEDQUEUETYPE_H #include <iostream> #include <new>...
Data Structures using C++ Consider the following class #ifndef LINKEDQUEUETYPE_H #define LINKEDQUEUETYPE_H #include <iostream> #include <new>    #include <cstdlib> #include "QueueADT.h" using namespace std; // Definition of the node template <class ItemType> struct NodeType {        ItemType info;        NodeType<ItemType> *next; }; template <class ItemType> class LinkedQueueType: public QueueADT<ItemType> { public:        // Constructor        LinkedQueueType();           // Default constructor.           // Post: An empty queue has been created. queueFront = NULL;           //       queueBack = NULL;...
- implement the Stack ADT using the linked list approach. Use C++ program language #include "StackLinked.h"...
- implement the Stack ADT using the linked list approach. Use C++ program language #include "StackLinked.h" template StackLinked::StackLinked (int maxNumber) { } template StackLinked::StackLinked(const StackLinked& other) { } template StackLinked& StackLinked::operator=(const StackLinked& other) { } template StackLinked::~StackLinked() {    clear(); } template void StackLinked::push(const DataType& newDataItem) throw (logic_error) {    } template DataType StackLinked::pop() throw (logic_error) { } template void StackLinked::clear() {    StackNode* t;    while ( top != NULL)    {        t = top;       ...
C++ Class involving difference. The goal is to overload the function: void Bag::operator-=(const Bag& a_bag) //...
C++ Class involving difference. The goal is to overload the function: void Bag::operator-=(const Bag& a_bag) // The Set Difference between two sets A and B is the set that consists of the elements of A which are not elements of B. Bag bag1 = (1,2,3) and Bag bag2 = (2,4,5) then bag1-=bag2 should return 1,3,4,5. //parameter a_bag to be subtracted from this (the calling) bag //post removes all data from items_ that is also found in a_bag //Since type is...
Used the next seudoucode pseudocode and implement in the next code below run in c ++...
Used the next seudoucode pseudocode and implement in the next code below run in c ++ this code What is the output of the following pseudocode, where num1 , num2 , and num3 are integer variables? num1 = 5 num2 = 1 num3 = 4 aQueue.enqueue(num2) aQueue.enqueue(num3) aQueue.dequeue() aQueue.enqueue(num1 - num2) num1 = aQueue.peek() aQueue.dequeue() num2 = aQueue.peek() aQueue.dequeue() cout << num2 << " " << num1 << " " << num3 << endl ____________________________________________________________________________________ a// Created by Frank M....
My assignment: Triplet Template Class Directions: Define a template class for a generic triplet. The private...
My assignment: Triplet Template Class Directions: Define a template class for a generic triplet. The private data member for the triplet is a generic array with three elements. The triplet ADT has the following functions:  default constructor  explicit constructor: initialize the data member using parameters  three accessors (three get functions) which will return the value of each individual element of the array data member  one mutator (set function) which will assign values to the data member...
The output only produces the values from the first linkedlist and not the second. Please fix...
The output only produces the values from the first linkedlist and not the second. Please fix the code to produce the desired objective. Thanks for your help! Objective: Interleave Example: Define the calling list as a set of ordered nodes, $L1 = {4, 2, 8 ,5, 8}$, and define the list that is passed as a parameter as a set of ordered nodes, $L2 = {5, 1, 8, 4, 5, 9}$. L1.interleave(L2) yields the set ${4, 5, 2, 1, 8,...
Create a class StacktwoPopArr using Arrays, Change the pop function so that two elements get popped...
Create a class StacktwoPopArr using Arrays, Change the pop function so that two elements get popped everytime instead of the one. If there is only one element in the stack, just pop the one element and report that the stack is now empty. If stack is empty, it should just report that stack is empty. All other functions for StackArray remain the same. using java StackArray.java: public class StackArray {       private final int size = 20; //Size of...
Given the following specifications for an array-based unsorted list, implement all of the functions (declared below)...
Given the following specifications for an array-based unsorted list, implement all of the functions (declared below) and a write a driver code to test all of your implementations. // Define a structure to use as the list item struct ListItem { int key; int Data; }; #define MAX_SIZE 50 // Define maximum length of the list class UnsortedArray { private: int head; // Index to head of the list ListItem theList[MAX_SIZE]; // The list public: UnsortedArray(); // Class constructor ~...
IntNode class I am providing the IntNode class you are required to use. Place this class...
IntNode class I am providing the IntNode class you are required to use. Place this class definition within the IntList.h file exactly as is. Make sure you place it above the definition of your IntList class. Notice that you will not code an implementation file for the IntNode class. The IntNode constructor has been defined inline (within the class declaration). Do not write any other functions for the IntNode class. Use as is. struct IntNode { int data; IntNode *next;...
You must alter the Queue class you created in L5 to make it a CIRCULAR Queue...
You must alter the Queue class you created in L5 to make it a CIRCULAR Queue class . Call your class Queue. it must be a template class. public class Queue { } I have put a driver program in the module . It is called CircularQueue.java This driver program should then run with your Queue class (no modifications allowed to the driver program). Your Queue class should have at least the following methods: one or more constructors, enqueue, dequeue,...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT