Question: Did I do this right? #include StackLinked.h template StackLinked ::StackLinked (int maxNumber) { top = NULL; } template StackLinked ::StackLinked(const StackLinked& other) { operator=(other); }

Did I do this right?

#include "StackLinked.h"

template

StackLinked::StackLinked (int maxNumber)

{

top = NULL;

}

template

StackLinked::StackLinked(const StackLinked& other)

{

operator=(other);

}

template

StackLinked& StackLinked::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

StackLinked::~StackLinked()

{

clear();

}

template

void StackLinked::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

DataType StackLinked::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

void StackLinked::clear()

{

this->top = NULL;

}

template

bool StackLinked::isEmpty() const

{

return (top == NULL);

}

template

bool StackLinked::isFull() const

{

return false;

}

template

void StackLinked::showStructure() const

{

if( isEmpty() )

{

cout << "Empty stack" << endl;

}

else

{

cout << "Top\t";

for (StackNode* temp = top; temp != 0; temp = temp->next) {

if( temp == top ) {

cout << "[" << temp->dataItem << "]\t";

}

else {

cout << temp->dataItem << "\t";

}

}

cout << "Bottom" << endl;

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!