Question: //-------------------------------------------------------------------- // // Laboratory 6 StackArray.cpp // // //-------------------------------------------------------------------- #ifndef STACKARRAY_CPP #define STACKARRAY_CPP #include #include StackArray.h //-------------------------------------------------------------------- template StackArray ::StackArray(int maxNumber) // Creates an empty
//-------------------------------------------------------------------- // // Laboratory 6 StackArray.cpp // // //-------------------------------------------------------------------- #ifndef STACKARRAY_CPP #define STACKARRAY_CPP #include#include "StackArray.h" //-------------------------------------------------------------------- template StackArray ::StackArray(int maxNumber) // Creates an empty stack. : maxSize(maxNumber), top(-1) { dataItems = new DataType[maxNumber]; } //-------------------------------------------------------------------- template StackArray ::StackArray(const StackArray& other) // Copy constructor for linked stack : maxSize(other.maxSize), top(other.top) { //Add code here } //-------------------------------------------------------------------- template StackArray & StackArray ::operator=(const StackArray& other) // Overloaded assignment operator for the StackArray class. // Because this function returns a StackArray object reference, // it allows chained assignment (e.g., stack1 = stack2 = stack3). { // Self-assignment protection if (this == &other) return *this; if (maxSize < other.maxSize) { // This object's array is smaller than the other object's array. // Make an equally big array. delete[] dataItems; dataItems = new DataType[other.maxSize]; } // Now proceed to copy state data over from other object. maxSize = other.maxSize; top = other.top; for (int i = 0; i <= top; i++) { dataItems[i] = other.dataItems[i]; } return *this; } //-------------------------------------------------------------------- template StackArray ::~StackArray() // Frees the memory used by a stack. { //Add code here } //-------------------------------------------------------------------- template void StackArray ::push(const DataType& newDataItem) throw (logic_error) // Inserts newDataItem onto the top of a stack. { if (isFull()) { throw logic_error("push() while stack full"); } //Add code here } //-------------------------------------------------------------------- template DataType StackArray ::pop() throw (logic_error) // Removes the topmost data item from a stack and returns it. { if (isEmpty()) { throw logic_error("pop() while stack empty"); } //Add code here } //-------------------------------------------------------------------- template void StackArray ::clear() // Removes all the data items from a stack. { //Add code here } //-------------------------------------------------------------------- template bool StackArray ::isEmpty() const // Returns true if a stack is empty. Otherwise, returns false. { //Add code here } //-------------------------------------------------------------------- template bool StackArray ::isFull() const // Returns true if a stack is full. Otherwise, returns false. { //Add code here } //-------------------------------------------------------------------- template void StackArray ::showStructure() const // Array implementation. Outputs the data items in a stack. If the // stack is empty, outputs "Empty stack". This operation is intended // for testing and debugging purposes only. { if (isEmpty()) { cout << "Empty stack." << endl; } else { int j; cout << "top = " << top << endl; for (j = 0; j < maxSize; j++) cout << j << "\t"; cout << endl; for (j = 0; j <= top; j++) { if (j == top) { cout << '[' << dataItems[j] << ']' << "\t"; // Identify top } else { cout << dataItems[j] << "\t"; } } cout << endl; } cout << endl; } #endif // #ifndef STACKARRAY_CPP
//-------------------------------------------------------------------- // // Laboratory 6 StackLinked.cpp // // // //--------------------------------------------------------------------
#ifndef STACKLINKED_CPP #define STACKLINKED_CPP
#include
#include "StackLinked.h"
//--------------------------------------------------------------------
template
// Creates a stack node containing item newDataItem and next pointer // nextPtr.
: dataItem(newDataItem), next(nextPtr) { }
//--------------------------------------------------------------------
template
{ }
//--------------------------------------------------------------------
template
// Copy constructor for linked stack
: top(0) { (void) operator=(other); // Use operator=, ignore return value
}
//--------------------------------------------------------------------
template
// Overloaded assignment operator for the StackLinked class. // Because this function returns a StackLinked object reference, // it allows chained assignment (e.g., stack1 = stack2 = stack3).
{ // Self-assignment protection if (this == &other) return *this;
clear(); // Clear existing nodes if (!other.isEmpty()) { // Copy first node top = new StackNode(other.top->dataItem, 0); StackNode *otherTemp = other.top->next; StackNode *thisTemp = 0, *thisPrevious = top;
// Copy rest of nodes while (otherTemp != 0) { thisTemp = new StackNode(otherTemp->dataItem, 0); thisPrevious->next = thisTemp; thisPrevious = thisTemp; otherTemp = otherTemp->next; } }
return *this; }
//--------------------------------------------------------------------
template
// Destructor. Frees the memory used by a stack.
{ //Add code here }
//--------------------------------------------------------------------
template
// Inserts newDataItem onto the top of a stack.
{ if (isFull()) { // Not likely with linked implementation throw logic_error("push() while stack full"); }
//Add code here }
//--------------------------------------------------------------------
template
// Removes the topmost item from a stack and returns it.
{ if (isEmpty()) { throw logic_error("pop() while stack empty"); }
StackNode* temp = top; top = top->next;
DataType value = temp->dataItem; delete temp;
return value; }
//--------------------------------------------------------------------
template
// Removes all the data items from a stack.
{ //Add code here }
//--------------------------------------------------------------------
template
// Returns true if a stack is empty. Otherwise, returns false.
{ //Add code here }
//--------------------------------------------------------------------
template
// Returns true if a stack is full. Otherwise, returns false.
{ return false;
}
//--------------------------------------------------------------------
template
// Linked list implementation. Outputs the data elements in a stack. // If the stack is empty, outputs "Empty stack". This operation is // intended for testing and debugging purposes only.
{ 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; } }
#endif //#ifndef STACKLINKED_CPP
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
