Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Trying to make a dynamic queue using templates. need the names entered in a queue and then dequeued. Better if explained. thanks. #include stdafx.h #include

Trying to make a dynamic queue using templates. need the names entered in a queue and then dequeued. Better if explained. thanks.

#include "stdafx.h"

#include

using namespace std;

#pragma once

template

class QueueTemplate

{

private:

//Struct for nodes

struct qNode

{

T data; //Node's value

qNode *next; //Pointer

};

qNode *front; //Pointer, holds the front of the queue

qNode *back; //Pointer, holds the back of the queue

int queueNum; //Tracks number of items in queue

public:

//Constructor

QueueTemplate();

//Destructor

~QueueTemplate();

//Operations

void enqueue(T);

void dequeue(T &);

bool isEmpty() const;

void destroyQueue();

};

//QueueTemplate.cpp

//#include

#include

//#include "QueueTemplate.h"

using namespace std;

template

QueueTemplate::QueueTemplate()

{

front = NULL;

back = NULL;

queueNum = 0;

}

template

QueueTemplate::~QueueTemplate()

{

destroyQueue();

}

template

void QueueTemplate::enqueue(T val)

{

qNode *newNode;

//Create new node, have it hold val

newNode = new qNode;

newNode->data = val;

newNode->next = NULL;

//Change front and back pointers,

//depending if the queue is empty or already contains data

if (isEmpty())

{

front = newNode;

back = newNode;

}

else

{

back->next = newNode;

back = newNode;

}

//update number of items in queue

queueNum++;

}

template

void QueueTemplate::dequeue(T &val)

{

qNode *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 queueNum

queueNum--;

}

}

template

bool QueueTemplate::isEmpty() const

{

bool empty;

if (queueNum > 0)

empty = false;

else

empty = true;

return empty;

}

template

void QueueTemplate::destroyQueue()

{

T temp;

while (!isEmpty())

{

dequeue(data);

}

}

//Driver for QueueTemplate class

#include

#include

//#include "QueueTemplate.h"

using namespace std;

int main()

{

string word;

//Creating queues

QueueTemplate strque;

QueueTemplate intque;

cout << "We have a queue to hold names." << endl;

cout << "Our queue of names is " << (strque.isEmpty() ? "Empty" : "Not Empty");

cout << "We will put three names into our queue." << endl;

//Enqueueing names...

for (int n = 0; n < 3; n++)

{

cout << "Enter name: " << endl;

getline(cin, word);

strque.enqueue(word);

}

cout << "The program will read back the names in the order they were entered. ";

for (int n = 0; n < 3; n++)

{

strque.dequeue(word);

cout << word << endl;

}

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

The Database Management Systems

Authors: Patricia Ward, George A Dafoulas

1st Edition

1844804526, 978-1844804528

More Books

Students also viewed these Databases questions