I have this for part 1 just need the rest. part 1 #include <iostream> #include<c
ID: 667669 • Letter: I
Question
I have this for part 1 just need the rest.
part 1
#include <iostream>
#include<cassert>
using namespace std;
//definition of the template class stackType
template <class Type>
class stackType
{
private:
int maxStackSize;
int stackTop;
Type *list;
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>&);
};
template <class Type>
void stackType<Type>::initializeStack()
{
stackTop = 0;
}
template <class Type>
bool stackType<Type>::isFullStack() const
{
return (stackTop == maxStackSize);
}
template <class Type>
bool stackType<Type>::isEmptyStack() const
{
return (stackTop == 0);
}
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";
}
template <class Type>
void stackType<Type>::pop()
{
if (!isEmptyStack())
stackTop--;
else
cout << " Can not remove from an empty stack";
}
template <class Type>
Type stackType<Type>::top() const
{
assert(stackTop != 0);
return list[stackTop - 1];
}
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];
}
template <class Type>
stackType<Type>::~stackType()
{
delete[] list;
}
template <class Type>
bool stackType<Type>::operator==
(const stackType<Type>& right)
{
if (this->stackTop != right.stackTop)
return false;
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;
}
I need these answers:
2. Repeat Exercise 1 for the class linkedStackType.
3. a. Add the following operation to the class stackType:
void reverseStack(stackType<Type> &otherStack);
This operation copies the elements of a stack in reverse order onto
another stack.
Consider the following statements:
stackType<int> stack1;
stackType<int> stack2;
The statement
stack1.reverseStack(stack2);
copies the elements of stack1 onto stack2 in reverse order. That is,
the top element of stack1 is the bottom element of stack2, and so on.
The old contents of stack2 are destroyed and stack1 is unchanged.
b. Write the definition of the function template to implement the operation
reverseStack.
4. Repeat Exercises 3a and 3b for the class linkedStackType.
Explanation / Answer
#include
#include
using namespace std;
//definition of the template class stackType
template
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& );
}; // end template class stackType
// initialize the stack
template
void stackType::initializeStack()
{
stackTop = 0;
} // end function initializeStack
// check for stack fullness
template
bool stackType::isFullStack() const
{
return ( stackTop == maxStackSize );
} // end function isFullStack
// check for stack empty
template
bool stackType::isEmptyStack() const
{
return ( stackTop == 0 );
} // end function isEmptyStack
// insert an element into stack
template
void stackType::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
void stackType::pop()
{
if ( !isEmptyStack() )
stackTop--;
else
cout << " Can not remove from an empty stack";
} // end function pop
// return the value of stack-top
template
Type stackType::top() const
{
assert( stackTop != 0 );
return list[ stackTop - 1 ];
} // end function top
// constructor for the class stackType
template
stackType::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
stackType::~stackType()
{
delete[] list;
} // end destructor stackType
// overload the equality operator
template
bool stackType::operator==
( const stackType& 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 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 s1( 12 );
stackType 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 << " ";
return 0;
}
Read more: http://www.justanswer.com/computer-programming/6be95-data-structures-using-c.html#ixzz3kor043xn