I have a quick question about my homework for c++. My code is shown below. Im re
ID: 3732181 • Letter: I
Question
I have a quick question about my homework for c++. My code is shown below. Im really not sure what I am supposed to be implimenting for the second function. Any help would be appricated. (I need help with QueueLinked)
#include "QueueLinked.h"
template
QueueLinked::QueueNode::QueueNode(const DataType& nodeData, QueueNode* nextPtr)
{
dataItem = nodeData;
next = nextPtr;
}
template
QueueLinked::QueueLinked(int maxNumber = Queue::MAX_QUEUE_SIZE)
{
}
template
QueueLinked::QueueLinked(const QueueLinked& other)
{
front = NULL;
back = NULL;
this = other;
}
template
QueueLinked& QueueLinked::operator=(const QueueLinked& other)
{
QueueNode * temp = other.front;
while (temp != NULL)
{
insert(temp->dataItem);
temp = temp->next;
}
return *this;
}
template
QueueLinked::~QueueLinked()
{
clear();
}
template
void QueueLinked::enqueue(const DataType& newDataItem) throw (logic_error)
{
if (!isFull())
if (!isEmpty())
back = back->next = new QueueNode(newDataItem, NULL);
else
front = back = new QueueNode(newDataItem, NULL);
else
throw logic_error("Linked list is full");
}
template
DataType QueueLinked::dequeue() throw (logic_error)
{
if (!isEmpty())
{
QueueNode *temp = front->next;
DataType tempData = front->dataItem;
delete front;
front = temp;
return tempData;
}
else
throw logic_error("Linked list is empty");
return(DataType)NULL;
}
template
void QueueLinked::clear()
{
while (front != NULL)
dequeue();
front = NULL;
back = NULL;
}
template
bool QueueLinked::isEmpty() const
{
if (front == NULL)
return true;
else
return false;
}
template
bool QueueLinked::isFull() const
{
return false;
}
template
void QueueLinked::putFront(const DataType& newDataItem) throw (logic_error)
{
if (!isFull())
{
front = new QueueNode(newDataItem, front);
if (front->next == NULL)
back = front;
}
else
throw logic_error("Linked list is full");
}
template
DataType QueueLinked::getRear() throw (logic_error)
{
if (!isEmpty())
{
if (back != front)
{
QueueNode* temp = back;
DataType tempData = back->dataItem;
back = front;
while (back->next != temp)
back = back->next;
delete back->next;
back->next = NULL;
return tempData;
}
else
{
DataType tempData = front->dataItem;
delete front;
front = back = NULL;
return tempData;
}
}
else
throw logic_error("Linked list is empty");
return (DataType)NULL;
}
template
int QueueLinked::getLength() const
{
int i = 0;
QueueNode* temp = front;
while (temp != NULL)
{
temp = temp->next;
i++;
}
return i;
}
Here is the .h file.
// QueueLinked.h
#include <stdexcept>
#include <iostream>
using namespace std;
#include "Queue.h"
template <typename DataType>
class QueueLinked : public Queue<DataType> {
public:
QueueLinked(int maxNumber = Queue<DataType>::MAX_QUEUE_SIZE);
QueueLinked(const QueueLinked& other);
QueueLinked& operator=(const QueueLinked& other);
~QueueLinked();
void enqueue(const DataType& newDataItem) throw (logic_error);
DataType dequeue() throw (logic_error);
void clear();
bool isEmpty() const;
bool isFull() const;
// Programming Exercise 2
void putFront(const DataType& newDataItem) throw (logic_error);
DataType getRear() throw (logic_error);
// Programming Exercise 3
int getLength() const;
void showStructure() const;
private:
class QueueNode {
public:
QueueNode(const DataType& nodeData, QueueNode* nextPtr);
DataType dataItem;
QueueNode* next;
};
QueueNode* front;
QueueNode* back;
};
Explanation / Answer
You can ignore the parameter maxNumber for linked list based queue. The QueueLinked extends Queue interface. The Queue interface is written in general so that if the implementation uses arrays, then the maximum size can be specified through the constructor. So if you are implementing QueueArray using Queue interface, then the value of the parameter can be used to set the array size. Since QueueLinked uses linked list nodes, the size of queue is not required in advance. So you can simply ignore the parameter value and just initialize the member variables as shown below.
template <typename DataType>
QueueLinked::QueueLinked(int maxNumber = Queue::MAX_QUEUE_SIZE)
{
front = NULL;
back = NULL;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.