Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2014 Nancy France September 15 19 2014 Proceedings Part I Lnai 8724

Authors: Toon Calders ,Floriana Esposito ,Eyke Hullermeier ,Rosa Meo

2014th Edition

3662448475, 978-3662448472

More Books

Students also viewed these Databases questions