Hello I was assigned to write a c program usuing a queue dat
Hello, I was assigned to write a c++ program usuing a queue data structure, but was getting errors in my int main () section. Can you help me out? The program listed below.
// header file : myQueue.h
#ifndef H_QueueType
#define H_QueueType
#include <iostream>
#include <cassert>
using namespace std;
// Definition of the template class queueType
template <class Type>
class queueType
{
public:
// data methods of the class
const queueType<Type>& operator= (const queueType<Type> &);
queueType(const queueType<Type>& otherQueue);
bool isEmptyQueue() const;
bool isFullQueue() const;
void initializeQueue();
Type front() const;
Type back() const;
void addQueue(const Type&);
void deleteQueue();
queueType<Type>(int queueSize = 100);
~queueType();
void print();
private :
// data members of the class
int maxQueueSize;
int count;
int queueFront;
int queuerear;
Type *list;
//member function
void copyQueue(const queueType<Type>&);
}; // end template class queueType
template <class Type>
const queueType<Type>& queueType<Type>::operator= (const queueType<Type> &otherQueue)
{
if (this != &otherQueue)
copyQueue(otherQueue);
return *this;
} //end function operator=
template <class Type>
queueType<Type>::queueType
(const queueType<Type>& otherQueue)
{
list = NULL;
copyQueue(otherQueue);
} // end copy constructor
template <class Type>
void queueType<Type>::copyQueue
(const queueType<Type> &otherQueue)
{
// delete the present queue elements
delete[] list;
// assign the target values to the present queue
maxQueueSize = otherQueue.maxQueueSize;
count = otherQueue.count;
queueFront = otherQueue.queueFront;
queueRear = otherQueue.queuerear;
// allocate memory (of target queue size)
list = new Type[maxQueueSize];
//copy the elements of the queue
for (int i = 0; i < otherQueue.maxQueueSize; i++)
list[i] = otherQueue.list[i];
} // end function copyQueue
template <class Type>
bool queueType<Type>::isEmptyQueue() const
{
return (count == 0);
} // end function isEmptyQueue
template <class Type>
bool queueType<Type>::isFullQueue() const
{
return (count == maxQueueSize);
} // end function isFullQueue
template <class Type>
void queueType<Type>::initializeQueue()
{
queueFront = 0;
queueRear = maxQueueSize - 1;
count = 0;
} // end function initializeQueue
template <class Type>
Type queueType<Type>::front() const
{
assert(!isEmptyQueue());
return list[queueFront];
} //end function front
template <class Type>
Type queueType<Type>::back() const
{
assert(!isEmptyQueue());
return list[queueRear];
} // end function back
template <class Type>
void queueType<Type>::addQueue(const Type& newElement)
{
if (!isFullQueue())
{
queuerear = (queueRear + 1) % maxQueueSize;
count++;
list[queueRear] = newElement;
} // end if
else
cout << \"\ \\tCan not add to a full queue.\";
} // end function addQueue
template <class Type>
void queueType<Type>:: delete Queue()
{
if (!isEmptyQueue())
{
count--;
queeuFront = (queueFront = 1) % maxQueueSize;
} // end if
else
cout << \"\ \\tCan not remove from an empty queue.\";
} // end function deleteQueue
template <class Type>
queueType<Type>::queueType<Type>(int queueSize)
{
if (queueSize <= 0)
{
cout << \"Creating an arrray of size 100\";
maxQueueSize = 100;
} // end if
else
maxQueueSize = queueSize;
queueFront = 0;
queueRear = maxQueuesize - 1;
count = 0;
list = new Type[maxQueueSize];
} // end constructor queueType
template <class Type>
queueType < Type >::~queueType()
{
for (int i = queueFront; i < queueFront + count; i++)
cout << list[i] << \" \";
}// end function print
#endif
//---------------------------------
int main()
{
// let the user know about the program
cout << \"\ \\t Program that describe teh operations of \" << \"\ \\toverloading of operator = and copy constructor.\";
// create two objects of type queueType
queueType<int> myQueue1(100);
queueType<int> myQueue2(100);
//add ten elements into the first queue
for (int i = 0; i < 10; i++)
myQueue1.addQueue(i);
// print the contents of teh first queue
cout << \"\ \\t The elements in the first queue were : \";
myQueue1.print();
// using overloaded =, copy first queue into second
myQueue2 = myQueue1;
// print the elements of the second queue
cout << \"\ \\t The elements in the second queue were : \";
myQueue2.print();
// add two more elements to second queue
cout << \"\ \ \\t Adding elemtns 11 99 to the second queue.\";
myQueue2.addQueue(11);
myQueue2.addQueue(99);
//using copy constructor, copy second queue into third
queueType<int> myQueue3(myQueue2);
//print the elements of the third queue
cout << \"\ \\t The elements in the third queue were : \";
myQueue3.print();
// let the screen be paused to see the output by the user.
cout << \"\ \ \\t\" << endl;
system(\"pause\");
return 0;
}
Solution
/* errors: spleeing mistakes of private variable, wrong function and constructor implementation,
All errors resolved
*/
// header file : myQueue.h
#ifndef H_QueueType
#define H_QueueType
#include <iostream>
#include <cassert>
using namespace std;
// Definition of the template class queueType
template <class Type>
class queueType
{
public:
// data methods of the class
const queueType<Type>& operator= (const queueType<Type> &);
queueType(const queueType<Type>& otherQueue);
bool isEmptyQueue() const;
bool isFullQueue() const;
void initializeQueue();
Type front() const;
Type back() const;
void addQueue(const Type&);
void deleteQueue();
queueType<Type>(int queueSize = 100);
void print();
private :
// data members of the class
int maxQueueSize;
int count;
int queueFront;
int queuerear;
Type *list;
//member function
void copyQueue(const queueType<Type>&);
}; // end template class queueType
template <class Type>
const queueType<Type>& queueType<Type>::operator= (const queueType<Type> &otherQueue)
{
if (this != &otherQueue)
copyQueue(otherQueue);
return *this;
} //end function operator=
template <class Type>
queueType<Type>::queueType
(const queueType<Type>& otherQueue)
{
list = NULL;
copyQueue(otherQueue);
} // end copy constructor
template <class Type>
void queueType<Type>::copyQueue
(const queueType<Type> &otherQueue)
{
// delete the present queue elements
delete[] list;
// assign the target values to the present queue
maxQueueSize = otherQueue.maxQueueSize;
count = otherQueue.count;
queueFront = otherQueue.queueFront;
queuerear = otherQueue.queuerear;
// allocate memory (of target queue size)
list = new Type[maxQueueSize];
//copy the elements of the queue
for (int i = 0; i < otherQueue.maxQueueSize; i++)
list[i] = otherQueue.list[i];
} // end function copyQueue
template <class Type>
bool queueType<Type>::isEmptyQueue() const
{
return (count == 0);
} // end function isEmptyQueue
template <class Type>
bool queueType<Type>::isFullQueue() const
{
return (count == maxQueueSize);
} // end function isFullQueue
template <class Type>
void queueType<Type>::initializeQueue()
{
queueFront = 0;
queuerear = maxQueueSize - 1;
count = 0;
} // end function initializeQueue
template <class Type>
Type queueType<Type>::front() const
{
assert(!isEmptyQueue());
return list[queueFront];
} //end function front
template <class Type>
Type queueType<Type>::back() const
{
assert(!isEmptyQueue());
return list[queuerear];
} // end function back
template <class Type>
void queueType<Type>::addQueue(const Type& newElement)
{
if (!isFullQueue())
{
queuerear = (queuerear + 1) % maxQueueSize;
count++;
list[queuerear] = newElement;
} // end if
else
cout << \"\ \\tCan not add to a full queue.\";
} // end function addQueue
template <class Type>
void queueType<Type>::deleteQueue()
{
if (!isEmptyQueue())
{
count--;
queueFront = (queueFront = 1) % maxQueueSize;
} // end if
else
cout << \"\ \\tCan not remove from an empty queue.\";
} // end function deleteQueue
template <class Type>
queueType< Type >::queueType(int queueSize)
{
if (queueSize <= 0)
{
cout << \"Creating an arrray of size 100\";
maxQueueSize = 100;
} // end if
else
maxQueueSize = queueSize;
queueFront = 0;
queuerear = maxQueueSize - 1;
count = 0;
list = new Type[maxQueueSize];
} // end constructor queueType
template <class Type>
void queueType<Type>::print()
{
for (int i = queueFront; i < queueFront + count; i++)
cout << list[i] << \" \";
}// end function print
#endif
// test c++ file main.cpp
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <algorithm>
#include <fstream>
#include <vector>
#include <math.h>
#include <complex.h>
#include \"myQueue.h\"
using namespace std;
int main()
{
// let the user know about the program
cout << \"\ \\t Program that describe teh operations of \" << \"\ \\toverloading of operator = and copy constructor.\";
// create two objects of type queueType
queueType<int> myQueue1(100);
queueType<int> myQueue2(100);
//add ten elements into the first queue
for (int i = 0; i < 10; i++)
myQueue1.addQueue(i);
// print the contents of teh first queue
cout << \"\ \\t The elements in the first queue were : \";
myQueue1.print();
// using overloaded =, copy first queue into second
myQueue2 = myQueue1;
// print the elements of the second queue
cout << \"\ \\t The elements in the second queue were : \";
myQueue2.print();
// add two more elements to second queue
cout << \"\ \ \\t Adding elemtns 11 99 to the second queue.\";
myQueue2.addQueue(11);
myQueue2.addQueue(99);
//using copy constructor, copy second queue into third
queueType<int> myQueue3(myQueue2);
//print the elements of the third queue
cout << \"\ \\t The elements in the third queue were : \";
myQueue3.print();
// let the screen be paused to see the output by the user.
cout << \"\ \ \\t\" << endl;
//system(\"pause\");
return 0;
}
/*
output:
Program that describe teh operations of
overloading of operator = and copy constructor.
The elements in the first queue were : 0 1 2 3 4 5 6 7 8 9
The elements in the second queue were : 0 1 2 3 4 5 6 7 8 9
Adding elemtns 11 99 to the second queue.
The elements in the third queue were : 0 1 2 3 4 5 6 7 8 9 11 99
*/






