Did I do this right? #include \"StackLinked.h\" template <typename DataType> Sta
ID: 3723173 • Letter: D
Question
Did I do this right?
#include "StackLinked.h"
template <typename DataType>
StackLinked<DataType>::StackLinked (int maxNumber)
{
top = NULL;
}
template <typename DataType>
StackLinked<DataType>::StackLinked(const StackLinked& other)
{
operator=(other);
}
template <typename DataType>
StackLinked<DataType>& StackLinked<DataType>::operator=(const StackLinked& other)
{
clear();
if (!other.isEmpty()) {
top = new StackNode(other.top->dataItem, 0);
StackNode *otherTemp = other.top->next;
StackNode *thisTemp = 0, *thisPrevious = top;
while (otherTemp != 0)
{
thisTemp = new StackNode(otherTemp->dataItem, 0);
thisPrevious->next = 0;
thisPrevious = top->next;
otherTemp = top;
}
}
return *this;
}
template <typename DataType>
StackLinked<DataType>::~StackLinked()
{
clear();
}
template <typename DataType>
void StackLinked<DataType>::push(const DataType& newDataItem) throw (logic_error)
{
if(isFull())
throw logic_error("List is Full");
StackNode *temp;
temp = new StackNode(newDataItem, top);
top = temp;
}
template <typename DataType>
DataType StackLinked<DataType>::pop() throw (logic_error)
{
if (isEmpty())
throw logic_error("Linked List is Empty.");
else {
// save the location of the top item, copy the top data item
old_top = top;
top_data = top->dataItem;
// update top
top = top->next; // works even on last node
// remove the data item from the stack
delete old_top;
old_top = NULL;
// return the data item
return top_data;
}
}
template <typename DataType>
void StackLinked<DataType>::clear()
{
this->top = NULL;
}
template <typename DataType>
bool StackLinked<DataType>::isEmpty() const
{
return (top == NULL);
}
template <typename DataType>
bool StackLinked<DataType>::isFull() const
{
return false;
}
template <typename DataType>
void StackLinked<DataType>::showStructure() const
{
if( isEmpty() )
{
cout << "Empty stack" << endl;
}
else
{
cout << "Top ";
for (StackNode* temp = top; temp != 0; temp = temp->next) {
if( temp == top ) {
cout << "[" << temp->dataItem << "] ";
}
else {
cout << temp->dataItem << " ";
}
}
cout << "Bottom" << endl;
}
}
Explanation / Answer
Given below is the modified code. Not sure why maxNumber is being passed in constructor and where you would want to use it.
#include "StackLinked.h"
template <typename DataType>
StackLinked<DataType>::StackLinked (int maxNumber)
{
top = NULL;
}
template <typename DataType>
StackLinked<DataType>::StackLinked(const StackLinked& other)
{
top = NULL;
*this=other;
}
template <typename DataType>
StackLinked<DataType>& StackLinked<DataType>::operator=(const StackLinked& other)
{
if(this != &other) //not self assigning
{
clear();
if (!other.isEmpty()) {
top = new StackNode(other.top->dataItem, 0);
StackNode *thisNode = top;
StackNode *otherNode = other.top->next;
while(otherNode != 0)
{
thisNode->next = new StackNode(otherNode->dataItem, 0);
thisNode = thisNode->next;
otherNode = otherNode->next;
}
}
}
return *this;
}
template <typename DataType>
StackLinked<DataType>::~StackLinked()
{
clear();
}
template <typename DataType>
void StackLinked<DataType>::push(const DataType& newDataItem) throw (logic_error)
{
if(isFull())
throw logic_error("List is Full");
top = new StackNode(newDataItem, top);
}
template <typename DataType>
DataType StackLinked<DataType>::pop() throw (logic_error)
{
if (isEmpty())
throw logic_error("Linked List is Empty.");
// save the location of the top item, copy the top data item
StackNode* old_top = top;
DataType top_data = top->dataItem;
// update top
top = top->next; // works even on last node
// remove the data item from the stack
delete old_top;
old_top = NULL;
// return the data item
return top_data;
}
template <typename DataType>
void StackLinked<DataType>::clear()
{
StackNode* next;
while(top != NULL)
{
next = top->next;
delete top;
top = next;
}
}
template <typename DataType>
bool StackLinked<DataType>::isEmpty() const
{
return (top == NULL);
}
template <typename DataType>
bool StackLinked<DataType>::isFull() const
{
return false;
}
template <typename DataType>
void StackLinked<DataType>::showStructure() const
{
if( isEmpty() )
{
cout << "Empty stack" << endl;
}
else
{
cout << "Top ";
for (StackNode* temp = top; temp != 0; temp = temp->next) {
if( temp == top ) {
cout << "[" << temp->dataItem << "] ";
}
else {
cout << temp->dataItem << " ";
}
}
cout << "Bottom" << endl;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.