Question

in C++ creat a DynamicQueue class, add a new data member called count to trace the...

in C++ creat a DynamicQueue class, add a new data member called count to trace the total number of node you have in current queue (you need to modify some member functions for adding count). Add a member function called displayQueue() to display values stored in each node in the current queue, also the total number of nodes in the queue. You also need to have a driver program (refer to Tester) to test your modified new class and new member function.

// This program demonstrates the DynamicQueue template.
#include <iostream>
#include <string>
#include "DynamicQueue.h"
using namespace std;

const int QUEUE_SIZE = 5;

int main()
{
    string name;
    
    // Create a Queue.
    DynamicQueue<string> queue;
    
    // Enqueue some names.
    for (int count = 0; count < QUEUE_SIZE; count++)
    {
        cout << "Enter an name: ";
        getline(cin, name);
        queue.enqueue(name);
    }
    
    // Dequeue the names and display them.
    cout << "\nHere are the names you entered:\n";
    for (int count = 0; count < QUEUE_SIZE; count++)
    {
        queue.dequeue(name);
        cout << name << endl;
    }
    return 0;
}
0 0
Add a comment Improve this question Transcribed image text
Answer #1

CODE:

Please include the below files in the same folder where you have the file having main() function.

DynamicQueue.h

#ifndef DynamicQueue_H
#define DynamicQueue_H
#include <iostream>

using namespace std;
#pragma once

template <class T>
class DynamicQueue
{
private:
   //Struct for nodes
   struct Node
   {
       T data;   
       Node *next;   
   };

   Node *front; //front of the queue
   Node *back; //back of the queue

   int numberOfNodes; //Tracks number of nodes in queue

public:

   //Constructor
   DynamicQueue();

   void enqueue(T);
   void dequeue(T &);
   bool isEmpty() const;
   void displayQueue();
   void destroyQueue();
  
   //Destructor
   ~ DynamicQueue();
};
#endif

--------------------------------------------------------------------------------
DynamicQueue.cpp

#include <iostream>
#include <string>
#include "DynamicQueue.h"

using namespace std;

template <class T>
DynamicQueue<T>:: DynamicQueue()
{
   front = NULL;
   back = NULL;
   numberOfNodes = 0;
}

template <class T>
void DynamicQueue<T>::enqueue(T val)
{
   Node *newNode;

   //Create new node, have it hold val
   newNode = new Node;
   newNode->data = val;
   newNode->next = NULL;

   //Change front and back pointers, depending on if the queue is empty or already contains data

   if (isEmpty())
   {
       front = newNode;
       back = newNode;
   }
   else
   {
       back->next = newNode;
       back = newNode;
   }

   //update number of nodes in queue
   numberOfNodes++;
}

template <class T>
void DynamicQueue<T>::dequeue(T &val)
{
   Node *temp;

   if(isEmpty())
   {
       cout << "No data currently queued" << endl;
   }
   else
   {
       //Store data from front node in val
       val = front->data;
       //Remove front node, delete
       temp = front;
       front = front->next;
       delete temp;

       //Update numberOfNodes
       numberOfNodes--;
   }
}

template <class T>
bool DynamicQueue<T>::isEmpty() const
{
   bool empty;

   if(numberOfNodes > 0)
       empty = false;
   else
       empty = true;
  

   return empty;
}

template <class T>
void DynamicQueue<T>::destroyQueue()
{
   T temp;

   while(!isEmpty())
   {
       dequeue(data);
   }
}

template <class T>
DynamicQueue<T>::~ DynamicQueue()
{
   destroyQueue();
}

template <class T>
void displayQueue(){
  
   if(!isEmpty())
   {
       Node *temp;
       cout << "Number of nodes in Queue are: " << numberOfNodes << endl;
       cout << "Queue Elements are: " << endl;
       temp = front;
       while(temp->next != NULL){
           cout << temp->data << endl;
           temp = temp->next;
       }
      
   }
}

Add a comment
Know the answer?
Add Answer to:
in C++ creat a DynamicQueue class, add a new data member called count to trace the...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Array-based Queue Lecture 6 Two Class Exercises | Class Exercise #1 - Create an array-based queue that holds value...

    Array-based Queue Lecture 6 Two Class Exercises | Class Exercise #1 - Create an array-based queue that holds values of double data type. 1.) Create a program that produces the following output OUTPUT: Q Quit Enter your choice: e Enter an item: 1.1 E Enqueue D Dequeue s-show queue ← showMenuO function called in main) OQuit // screen clears-.. continue enqueuing.screen clearing with each iteration Enter your choice: e Queue is full. E Enqueue D Dequeue s Show queue 0...

  • Help me solve this in C++ Rewrite the code to use array instead of linked lists....

    Help me solve this in C++ Rewrite the code to use array instead of linked lists. Please do not change anything besides the data structure. Instead of linked list use an array. The functionality should remain exactly the same. You can prepare a class if you wish but it is not required. /** * Queue implementation using linked list C style implementation ( no OOP). */ #include <cstdio> #include <cstdlib> #include <climits> #include <iostream> #define CAPACITY 100 // Queue max...

  • HI USING C++ CAN YOU PLEASE PROGRAM THIS ASSIGNMENT AND ADD COMMENTS: stackARRAY: #include<iostream> #define SIZE...

    HI USING C++ CAN YOU PLEASE PROGRAM THIS ASSIGNMENT AND ADD COMMENTS: stackARRAY: #include<iostream> #define SIZE 100 #define NO_ELEMENT -999999 using namespace std; class Stack { int arr[SIZE]; // array to store Stack elements int top; public: Stack() { top = -1; } void push(int); // push an element into Stack int pop(); // pop the top element from Stack int topElement(); // get the top element void display(); // display Stack elements from top to bottom }; void Stack...

  • PART A - STACKS To complete this assignment: Please study the code posted below. Please rewrite...

    PART A - STACKS To complete this assignment: Please study the code posted below. Please rewrite the code implementing a template class using a linked list instead of an array. Note: The functionality should remain the same *************************************************************************************************************** /** * Stack implementation using array in C/procedural language. */ #include <iostream> #include <cstdio> #include <cstdlib> //#include <climits> // For INT_MIN #define SIZE 100 using namespace std; /// Create a stack with capacity of 100 elements int stack[SIZE]; /// Initially stack is...

  • Subject: C++ I have created a class called QueueOfIntegers in a file called QueueOfIntegers.h, which is...

    Subject: C++ I have created a class called QueueOfIntegers in a file called QueueOfIntegers.h, which is here: #ifndef QueueOfIntegers_H #define QueueOfIntegers_H class QueueOfIntegers { public:    int elements[100]; //Array to store integers in the queue    int size; // Number of integers in the queue    QueueOfIntegers(); // Construct an empty queue    bool IsEmpty(); // Returns true if the queue is empty    int Peek(); // Returns the integer at the beginning of the Queue without removing it   ...

  • public class PQueue<E extends Comparable<E>> { private E[] elements; private int size; private int head; private...

    public class PQueue<E extends Comparable<E>> { private E[] elements; private int size; private int head; private int tail; Private int count;   } public void enqueue(E item) { if(isFull()){ return; } count++; elements[tail] = item; tail = (tail + 1) % size; } public E dequeue() { if(isEmpty()) return null; int ct = count-1; E cur = elements[head]; int index = 0; for(i=1;ct-->0;i++) { if(cur.compareTo(elements[head+i)%size])<0) cur = elements[(head+i)%size]; index = i; } } return remove((head+index%size); public E remove(int index) { E...

  • Using the below files, Write a program that reads a document containing endnotes indicated in this...

    Using the below files, Write a program that reads a document containing endnotes indicated in this manner, collects them in a queue, and prints them on the screen. For this lab, you will create a text file called sample.txt and put the following paragraph in it. This part is the beginning. {This part is the footnote.} This part is the end. /* Queue.h contains the declaration of class Queue. Basic operations: Constructor: Constructs an empty queue empty: Checks if a...

  • Introduction In this lab, you are supposed to implement a graph class with the data structure...

    Introduction In this lab, you are supposed to implement a graph class with the data structure implemented before like linked list and queue. graph The class graph contains three member variables: linkedList *adjacentVertices; //an array of linked list. For a vertice i, adjacentVertices[i] stores the linked list that contains all other vertices connected to vertice i. int numVertices; //The number of vertices in the graph. int maxNumVertices; //The maximum number of vertices the graph can hold. Following public methods are...

  • using: class MyQueue<T> { private java.util.LinkedList<T> list; public MyQueue() { list = new java.util.LinkedList<T>(); } public...

    using: class MyQueue<T> { private java.util.LinkedList<T> list; public MyQueue() { list = new java.util.LinkedList<T>(); } public void enqueue(T data) { list.add(data); } public T dequeue() { return list.remove(0); } public T peek() { return list.get(0); } public int size() { return list.size(); } public boolean isEmpty() { return list.isEmpty(); } } class MyQueueTest { public static void main(String[] args) { MyQueue<Integer> queue = new MyQueue<Integer>(); queue.enqueue(3); queue.enqueue(2); queue.enqueue(7); queue.enqueue(1); while (!queue.isEmpty()) { System.out.println(queue.dequeue()); } } } please solve the following:...

  • Language C++ (Please include a short description & Screenshot of output) Implement a Priority...

    Language C++ (Please include a short description & Screenshot of output) Implement a Priority queue using a SORTED list. Use Quick sort after adding a new node. Example of quick sort below. Adopt to your program the code below. #include <iostream> void quickSort(int a[ ], int first, int last); int pivot(int a[], int first, int last); void swap(int& a, int& b); void swapNoTemp(int& a, int& b); void print(int array[], const int& N); using namespace std; int main() { int test[]...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT