whats wrong with this answer. Two stacks of the same type are the same if they h
ID: 3651732 • Letter: W
Question
whats wrong with this answer.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, false otherwise. Also, write the definition of the function template to overload the operator
#include <iostream>
#include<cassert>
using namespace std;
//definition of the template class stackType
template <class Type>
class stackType
{
// data memebers of the class
private :
int maxStackSize;
int stackTop;
Type *list;
// data methods of the class
public :
void initializeStack();
bool isFullStack() const;
bool isEmptyStack() const;
void push( const Type& );
void pop();
Type top() const;
stackType( int = 20 );
~stackType();
bool operator==( const stackType<Type>& );
}; // end template class stackType
// initialize the stack
template <class Type>
void stackType<Type>::initializeStack()
{
stackTop = 0;
} // end function initializeStack
// check for stack fullness
template <class Type>
bool stackType<Type>::isFullStack() const
{
return ( stackTop == maxStackSize );
} // end function isFullStack
// check for stack empty
template <class Type>
bool stackType<Type>::isEmptyStack() const
{
return ( stackTop == 0 );
} // end function isEmptyStack
// insert an element into stack
template <class Type>
void stackType<Type>::push( const Type& newItem )
{
if ( !isFullStack() )
{
list[ stackTop ] = newItem;
stackTop++;
} // end if
else
cout << " Can not add to a full stack";
} // end function push
// delete an element from the stack
template <class Type>
void stackType<Type>::pop()
{
if ( !isEmptyStack() )
stackTop--;
else
cout << " Can not remove from an empty stack";
} // end function pop
// return the value of stack-top
template <class Type>
Type stackType<Type>::top() const
{
assert( stackTop != 0 );
return list[ stackTop - 1 ];
} // end function top
// constructor for the class stackType
template <class Type>
stackType<Type>::stackType( int stackSize )
{
if ( stackSize <= 0 )
{
cout << "Invalid size";
stackSize = 10;
} // end if
else
maxStackSize = stackSize;
stackTop = 0;
list = new Type[ maxStackSize ];
} // end constructor stackType
// destructor for the class stackType
template <class Type>
stackType<Type>::~stackType()
{
delete[] list;
} // end destructor stackType
// overload the equality operator
template <class Type>
bool stackType<Type>::operator==
( const stackType<Type>& right )
{
// check for same number of elements
if ( this->stackTop != right.stackTop )
return false;
//check for equality of elements at corresponding positions
for ( int i = 0; i < stackTop; i++ )
if ( this->list[ i ] != right.list[ i ] )
return false;
return true;
}
//main function
int main()
{
// let the user know about the program
cout << " Program to overload the realtional "
<< "operator == for the class stackType.";
// create objects of type stackType
stackType<int> s1( 12 );
stackType<int> s2( 15 );
// insert elements into the stacks
cout << " Inserting elements 5, 10, 15 ... "
<< "to both the stacks.";
for ( int i = 5; i < 50; i+=5 )
{
s1.push( i );
s2.push( i );
} // end for
//check and print whether the stacks are equal or not
if ( s1 == s2 )
cout << " Both the stacks are equal";
else
cout << " Both the stacks are not equal";
// insert one more element into the second stack
cout<<" Inserting element 11 to the second stack.";
s2.push( 11 );
//check and print whether the stacks are equal or not
if ( s1 == s2 )
cout << " Both the stacks are equal";
else
cout << " Both the stacks are not equal";
cout << " ";
system("pause");
return 0;
}
2. Repeate this exercise for the class linkedStackedType.
#include <iostream>
#include <cassert>
using namespace std;
template<class Type>
class stackType
{
public:
const stackType<Type>& operator=(const stackType<Type>&);
void initializeStack();
bool isEmptyStack();
bool isFullStack();
void destroyStack();
void push(const Type& newItem);
Type top();
void pop();
stackType(int stackSize = 100);
stackType(const stackType<Type>& otherStack);
~stackType();
private:
int maxStackSize;
int stackTop;
Type *list;
void copyStack(const stackType<Type>& otherStack);
};
template<class Type>
void stackType<Type>::copyStack(const stackType<Type>& otherStack)
{
delete [] list;
maxStackSize = otherStack.maxStackSize;
stackTop = otherStack.stackTop;
list = new Type[maxStackSize];
assert(list != NULL);
for(int j = 0; j < stackTop; j++)
list[j] = otherStack.list[j];
}
template<class Type>
stackType<Type>::stackType(const stackType<Type>& otherStack)
{
list = NULL;
copyStack(otherStack);
}
template<class Type>
const stackType<Type>& stackType<Type>::operator=(const stackType<Type>& otherStack)
{
if (this != &otherStack)
copyStack(otherStack);
return *this;
}
template<class Type>
void stackType<Type>::initializeStack()
{
stackTop = 0;
}
template<class Type>
void stackType<Type>::destroyStack()
{
stackTop = 0;
}
template<class Type>
bool stackType<Type>::isEmptyStack()
{
return(stackTop == maxStackSize);
}
template<class Type>
bool stackType<Type>::isFullStack()
{
return(stackTop == maxStackSize);
}
template<class Type>
void stackType<Type>::push(const Type& newItem)
{
if(!isFullStack())
{
list[stackTop] = newItem;
stackTop++;
}
else
cerr<<"Cannot add to a full stack."<<endl;
}
template<class Type>
Type stackType<Type>::top()
{
assert(stackTop != 0);
return list[stackTop - 1];
}
template<class Type>
void stackType<Type>::pop()
{
if(!isEmptyStack())
{
stackTop--;
}
else
cerr<<"Cannot remove from an empty stack."<<endl;
}
template<class Type>
stackType<Type>::stackType(int stackSize)
{
if(stackSize <= 0)
{
cerr<<"The size of the array to hold the stack must "
<< "be positive."<<endl;
cerr<<"Creating an array of size 100."<<endl;
maxStackSize = 100;
}
else
maxStackSize = stackSize;
stackTop = 0;
list = new Type[maxStackSize];
}
template<class Type>
stackType<Type>::~stackType()
{
delete [] list;
}
#endif
Explanation / Answer
answer: #ifndef H_StackType #define H_StackType #include #include using namespace std; template class stackType { public: const stackType& operator=(const stackType&); void initializeStack(); bool isEmptyStack(); bool isFullStack(); void destroyStack(); void push(const Type& newItem); Type top(); void pop(); stackType(int stackSize = 100); stackType(const stackType& otherStack); ~stackType(); private: int maxStackSize; int stackTop; Type *list; void copyStack(const stackType& otherStack); }; template void stackType::copyStack(const stackType& otherStack) { delete [] list; maxStackSize = otherStack.maxStackSize; stackTop = otherStack.stackTop; list = new Type[maxStackSize]; assert(list != NULL); for(int j = 0; jRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.