Question

(C++) Two stacks of the same type are the same if they have the same number...

(C++) Two stacks of the same type are the same if they have the same number of elements and their elements at the corresponding positions are the same.

Overload the relational operator == for the class stackType that returns true if two stacks of the same type are the same; it returns false otherwise.

Also, write the definition of the function template to overload this operator.

Write a program to test the various overloaded operators and functions of classstackType.

**Please complete all the code below.**

main.cpp

#include
#include "myStack.h"

using namespace std;

int main() {
// Write your main here
return 0;
}

myStack.h

//Header file: myStack.h

#ifndef H_StackType
#define H_StackType

#include
#include

#include "stackADT.h"

using namespace std;
  
template
class stackType: public stackADT
{
public:
const stackType& operator=(const stackType&);
//Overload the assignment operator.

void initializeStack();
//Function to initialize the stack to an empty state.
//Postcondition: stackTop = 0

bool isEmptyStack() const;
//Function to determine whether the stack is empty.
//Postcondition: Returns true if the stack is empty,
// otherwise returns false.

bool isFullStack() const;
//Function to determine whether the stack is full.
//Postcondition: Returns true if the stack is full,
// otherwise returns false.

void push(const Type& newItem);
//Function to add newItem to the stack.
//Precondition: The stack exists and is not full.
//Postcondition: The stack is changed and newItem
// is added to the top of the stack.

Type top() const;
//Function to return the top element of the stack.
//Precondition: The stack exists and is not empty.
//Postcondition: If the stack is empty, the program
// terminates; otherwise, the top element
// of the stack is returned.

void pop();
//Function to remove the top element of the stack.
//Precondition: The stack exists and is not empty.
//Postcondition: The stack is changed and the top
// element is removed from the stack.

stackType(int stackSize = 100);
//constructor
//Create an array of the size stackSize to hold
//the stack elements. The default stack size is 100.
//Postcondition: The variable list contains the base
// address of the array, stackTop = 0, and
// maxStackSize = stackSize.

stackType(const stackType& otherStack);
//copy constructor

~stackType();
//destructor
//Remove all the elements from the stack.
//Postcondition: The array (list) holding the stack
// elements is deleted.
  
bool operator==(const stackType& otherStack) const;

private:
int maxStackSize; //variable to store the maximum stack size
int stackTop; //variable to point to the top of the stack
Type *list; //pointer to the array that holds the
//stack elements

void copyStack(const stackType& otherStack);
//Function to make a copy of otherStack.
//Postcondition: A copy of otherStack is created and
// assigned to this stack.
};


template
void stackType::initializeStack()
{
stackTop = 0;
}//end initializeStack


template
bool stackType::isEmptyStack() const
{
return(stackTop == 0);
}//end isEmptyStack

template
bool stackType::isFullStack() const
{
return(stackTop == maxStackSize);
} //end isFullStack

template
void stackType::push(const Type& newItem)
{
if (!isFullStack())
{
list[stackTop] = newItem; //add newItem to the
//top of the stack
stackTop++; //increment stackTop
}
else
cout << "Cannot add to a full stack." << endl;
}//end push

template
Type stackType::top() const
{
assert(stackTop != 0); //if stack is empty,
//terminate the program
return list[stackTop - 1]; //return the element of the
//stack indicated by
//stackTop - 1
}//end top

template
void stackType::pop()
{
if (!isEmptyStack())
stackTop--; //decrement stackTop
else
cout << "Cannot remove from an empty stack." << endl;
}//end pop

template
stackType::stackType(int stackSize)
{
if (stackSize <= 0)
{
cout << "Size of the array to hold the stack must "
<< "be positive." << endl;
cout << "Creating an array of size 100." << endl;

maxStackSize = 100;
}
else
maxStackSize = stackSize; //set the stack size to
//the value specified by
//the parameter stackSize

stackTop = 0; //set stackTop to 0
list = new Type[maxStackSize]; //create the array to
//hold the stack elements
}//end constructor

template
stackType::~stackType() //destructor
{
delete [] list; //deallocate the memory occupied
//by the array
}//end destructor

template
void stackType::copyStack(const stackType& otherStack)
{
delete [] list;                 
maxStackSize = otherStack.maxStackSize;         
stackTop = otherStack.stackTop;             
  
list = new Type[maxStackSize];                    

//copy otherStack into this stack
for (int j = 0; j < stackTop; j++)
list[j] = otherStack.list[j];
} //end copyStack


template
stackType::stackType(const stackType& otherStack)
{
list = nullptr;

copyStack(otherStack);
}//end copy constructor

template
const stackType& stackType::operator=
                    (const stackType& otherStack)
{
if (this != &otherStack) //avoid self-copy
copyStack(otherStack);

return *this;
} //end operator=   

#endif

stackADT.h

//Header file: stackADT.h

#ifndef H_StackADT
#define H_StackADT
  
template
class stackADT
{
public:
virtual void initializeStack() = 0;
//Method to initialize the stack to an empty state.
//Postcondition: Stack is empty

virtual bool isEmptyStack() const = 0;
//Function to determine whether the stack is empty.
//Postcondition: Returns true if the stack is empty,
// otherwise returns false.

virtual bool isFullStack() const = 0;
//Function to determine whether the stack is full.
//Postcondition: Returns true if the stack is full,
// otherwise returns false.

virtual void push(const Type& newItem) = 0;
//Function to add newItem to the stack.
//Precondition: The stack exists and is not full.
//Postcondition: The stack is changed and newItem
// is added to the top of the stack.

virtual Type top() const = 0;
//Function to return the top element of the stack.
//Precondition: The stack exists and is not empty.
//Postcondition: If the stack is empty, the program
// terminates; otherwise, the top element
// of the stack is returned.

virtual void pop() = 0;
//Function to remove the top element of the stack.
//Precondition: The stack exists and is not empty.
//Postcondition: The stack is changed and the top
// element is removed from the stack.
};
  
#endif

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

Note: stackADT.h is not modified, hence not attached.

//Header file: myStack.h

#ifndef H_StackType

#define H_StackType

#include <iostream>

#include <assert.h>

#include "stackADT.h"

using namespace std;

template <typename Type>

class stackType : public stackADT<Type> {

public:

    const stackType& operator=(const stackType&);

    //Overload the assignment operator.

    void initializeStack();

    //Function to initialize the stack to an empty state.

    //Postcondition: stackTop = 0

    bool isEmptyStack() const;

    //Function to determine whether the stack is empty.

    //Postcondition: Returns true if the stack is empty,

    // otherwise returns false.

    bool isFullStack() const;

    //Function to determine whether the stack is full.

    //Postcondition: Returns true if the stack is full,

    // otherwise returns false.

    void push(const Type& newItem);

    //Function to add newItem to the stack.

    //Precondition: The stack exists and is not full.

    //Postcondition: The stack is changed and newItem

    // is added to the top of the stack.

    Type top() const;

    //Function to return the top element of the stack.

    //Precondition: The stack exists and is not empty.

    //Postcondition: If the stack is empty, the program

    // terminates; otherwise, the top element

    // of the stack is returned.

    void pop();

    //Function to remove the top element of the stack.

    //Precondition: The stack exists and is not empty.

    //Postcondition: The stack is changed and the top

    // element is removed from the stack.

    stackType(int stackSize = 100);

    //constructor

    //Create an array of the size stackSize to hold

    //the stack elements. The default stack size is 100.

    //Postcondition: The variable list contains the base

    // address of the array, stackTop = 0, and

    // maxStackSize = stackSize.

    stackType(const stackType& otherStack);

    //copy constructor

    ~stackType();

    //destructor

    //Remove all the elements from the stack.

    //Postcondition: The array (list) holding the stack

    // elements is deleted.

    bool operator==(const stackType& otherStack) const;

    //compares two stack and return true if both are same

private:

    int maxStackSize; //variable to store the maximum stack size

    int stackTop; //variable to point to the top of the stack

    Type* list; //pointer to the array that holds the

    //stack elements

    void copyStack(const stackType& otherStack);

    //Function to make a copy of otherStack.

    //Postcondition: A copy of otherStack is created and

    // assigned to this stack.

};

template <typename Type>

void stackType<Type>::initializeStack()

{

    stackTop = 0;

} //end initializeStack

template <typename Type>

bool stackType<Type>::isEmptyStack() const

{

    return (stackTop == 0);

} //end isEmptyStack

template <typename Type>

bool stackType<Type>::isFullStack() const

{

    return (stackTop == maxStackSize);

} //end isFullStack

template <typename Type>

void stackType<Type>::push(const Type& newItem)

{

    if (!isFullStack()) {

        list[stackTop] = newItem; //add newItem to the

        //top of the stack

        stackTop++; //increment stackTop

    }

    else

        cout << "Cannot add to a full stack." << endl;

} //end push

template <typename Type>

Type stackType<Type>::top() const

{

    assert(stackTop != 0); //if stack is empty,

    //terminate the program

    return list[stackTop - 1]; //return the element of the

    //stack indicated by

    //stackTop - 1

} //end top

template <typename Type>

void stackType<Type>::pop()

{

    if (!isEmptyStack())

        stackTop--; //decrement stackTop

    else

        cout << "Cannot remove from an empty stack." << endl;

} //end pop

template <typename Type>

stackType<Type>::stackType(int stackSize)

{

    if (stackSize <= 0) {

        cout << "Size of the array to hold the stack must "

             << "be positive." << endl;

        cout << "Creating an array of size 100." << endl;

        maxStackSize = 100;

    }

    else

        maxStackSize = stackSize; //set the stack size to

    //the value specified by

    //the parameter stackSize

    stackTop = 0; //set stackTop to 0

    list = new Type[maxStackSize]; //create the array to

    //hold the stack elements

} //end constructor

template <typename Type>

stackType<Type>::~stackType() //destructor

{

    delete[] list; //deallocate the memory occupied

    //by the array

} //end destructor

template <typename Type>

void stackType<Type>::copyStack(const stackType& otherStack)

{

    delete[] list;

    maxStackSize = otherStack.maxStackSize;

    stackTop = otherStack.stackTop;

    list = new Type[maxStackSize];

    //copy otherStack into this stack

    for (int j = 0; j < stackTop; j++)

        list[j] = otherStack.list[j];

} //end copyStack

template <typename Type>

stackType<Type>::stackType(const stackType& otherStack)

{

    list = nullptr;

    copyStack(otherStack);

} //end copy constructor

template <typename Type>

const stackType<Type>& stackType<Type>::operator=(const stackType& otherStack)

{

    if (this != &otherStack) //avoid self-copy

        copyStack(otherStack);

    return *this;

} //end operator=

template <typename Type>

bool stackType<Type>::operator==(const stackType& otherStack) const{

                //comparing sizes

                if(stackTop!=otherStack.stackTop){

                                return false; //different number of elements

                }

                //comparing each element, one by one

                for(int i=0;i<stackTop;i++){

                                if(list[i]!=otherStack.list[i]){

                                               //elements mismatch

                                               return false;

                                }

                }

                //same elements in same positions

                return true;

}

#endif

main.cpp

#include <iostream>

#include "myStack.h"

using namespace std;

int main() {

                //creating an integer stack

                stackType<int> stk1;

                //adding numbers from 1 to 5

                for(int i=1;i<=5;i++){

                                stk1.push(i);

                }

                //creating another stack, copying all elements from stk1

                stackType<int> stk2=stk1;

                //enabling display of true/false instead of 1/0 for boolean values

                cout<<boolalpha;

                //checking each method

                cout<<"stk1 == stk2: "<<(stk1==stk2)<<endl; //true

                cout<<"stk1 is full: "<<stk1.isFullStack()<<endl; //false

                cout<<"stk1 is empty: "<<stk1.isEmptyStack()<<endl; //false

                cout<<"popping stk1 until empty"<<endl;

                //continuously popping from stk1 until stk1 becomes empty

                while(!stk1.isEmptyStack()){

                                cout<<stk1.top()<<" has been popped"<<endl;

                                stk1.pop();

                }

                cout<<"stk1 is empty: "<<stk1.isEmptyStack()<<endl; //true

                cout<<"stk1 == stk2: "<<(stk1==stk2)<<endl; //false

                return 0;

}

//OUTPUT

stk1 == stk2: true

stk1 is full: false

stk1 is empty: false

popping stk1 until empty

5 has been popped

4 has been popped

3 has been popped

2 has been popped

1 has been popped

stk1 is empty: true

stk1 == stk2: false

Add a comment
Know the answer?
Add Answer to:
(C++) Two stacks of the same type are the same if they have the same number...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT