Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C++ HW Question 1 EASY QUESTION. Add to your improved queue class with the following requirements: copy constructor, queue(const queue& q) , performs a deep-copy.

C++ HW Question 1 EASY QUESTION.

Add to your improved queue class with the following requirements:

copy constructor, queue(const queue& q), performs a deep-copy.

Include deconstructor, ~queue(), that deallocates dynamic allocated elements array.

Include static int numberOfObjects and static int getNumberofObjects to keep track the number of queue objects created and deleted (see page 386-388).

Include contains(T element) as an instance function to check whether the element is in the queue (see homework Exercise 12.7).

Include print(const char*s) as an instance functions to display the title (s), the size of the queue, the front and back index of the queue, and the stored elements in the queue.

Here is the queue class without the implementation codes:

template

class Queue

{

public:

Queue();

Queue(const Queue& q);

~Queue();

bool enQueue(const T value);

T deQueue();

bool contains(const T value) const;

bool isEmpty() const;

bool isFull() const;

int getSize() const;

static int getNumberOfObjects();

void print(const char*) const;

private:

T* elements;

int size;

int front;

int back;

int capacity;

void ensureCapacity();

static int numberOfObjects;

};

Here is the code I have so far: #include #include using namespace std; // define default capacity of the queue #define CAPACITY 10 // Class for queue template class queue { X *arr; // array to store queue elements int capacity; // maximum capacity of the queue int front; // front points to front element in the queue (if any) int rear; // rear points to last element in the queue int count; // current size of the queue void ensureCapacity(); public: queue(int capacity = CAPACITY); // constructor void dequeue(); void enqueue(X x); X peek(); int size(); bool isEmpty(); bool isFull(); ~queue(); }; // Constructor to initialize queue template queue::queue(int cap) { arr = new X[cap]; capacity = cap; front = 0; rear = -1; count = 0; } // Utility function to remove front element from the queue template void queue::dequeue() { // check for queue underflow if (isEmpty()) { cout << "UnderFlow Program Terminated "; exit(EXIT_FAILURE); } cout << "Removing " << arr[front] << ' '; front = (front + 1) % capacity; count--; } // Utility function to add an item to the queue template void queue::enqueue(X item) { // check for queue overflow if (isFull()) { ensureCapacity(); } cout << "Inserting " << item << ' '; rear = (rear + 1) % capacity; arr[rear] = item; count++; } // Utility function to return front element in the queue template X queue::peek() { if (isEmpty()) { cout << "UnderFlow Program Terminated "; exit(EXIT_FAILURE); } return arr[front]; } // Utility function to return the size of the queue template int queue::size() { return count; } // Utility function to check if the queue is empty or not template bool queue::isEmpty() { return (size() == 0); } // Utility function to check if the queue is full or not template bool queue::isFull() { return (size() == capacity); }

//function to expand the array when it is full template void queue::ensureCapacity() { int newCapacity = capacity * 2; X *temp = new X[newCapacity]; int index = front; for(int i = 0; i < count; i++) { temp[i] = arr[index]; index = (index + 1) % capacity; } delete []arr; arr = temp; front = 0; rear = size()-1; capacity = newCapacity; }

//destructor template queue::~queue() { delete []arr; }

// main function int main() { // create a queue of capacity 4 queue q(4); q.enqueue("a"); q.enqueue("b"); q.enqueue("c"); cout << "Front element is: " << q.peek() << endl; q.dequeue(); q.enqueue("d"); cout << "Queue size is " << q.size() << endl; q.dequeue(); q.dequeue(); q.dequeue(); queue q1(4); q1.enqueue(2); q1.enqueue(5); q1.enqueue(8); q1.enqueue(20); cout << "Front element is: " << q1.peek() << endl; q1.dequeue(); q1.enqueue(50); cout << "Queue size is " << q1.size() << endl; if (q.isEmpty()) cout << "Queue Is Empty "; else cout << "Queue Is Not Empty "; cout << "Creating a double queue with capacity of 5" << endl; queue q2(5); cout << "Adding 10 doubles so the array should expand" << endl; for(int i = 1; i <= 10; i++) q2.enqueue(i+0.5); cout << "Dequeuing whole queue" << endl; while(!q2.isEmpty()) { cout << q2.peek() << " "; q2.dequeue(); } cout << endl; return 0; }

Once the above is done please also implement the following:

Use the following main() and implement a template printNumberOfObjects() function that displays the current number of allocated queue objects, and code the section that write a queue object to and read a queue object from the queue.dat file.

template

void printNumberOfObjects()

{

// displays the current number of queue object(s)

}

int main()

{

printNumberOfObjects();

Queue* pQ1 = new Queue;

printNumberOfObjects();

pQ1->print("1");

pQ1->enQueue(string("hello"));

pQ1->enQueue(string("C++"));

pQ1->enQueue(string("world"));

pQ1->print("1");

Queue Q2(*pQ1);

delete pQ1;

Q2.print("2");

//Write Q2 into a binary file called "queue.dat"

Queue Q3;

//Read Q3 from a binary file called "queue.dat"

Q3.print("3");

printNumberOfObjects();

return 0;

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Design Application Development And Administration

Authors: Mannino Michael

5th Edition

0983332401, 978-0983332404

More Books

Students also viewed these Databases questions

Question

What are Decision Trees?

Answered: 1 week ago

Question

What is meant by the Term Glass Ceiling?

Answered: 1 week ago