Question

C++ Please ensure code is fully working, will rate Question to be answered:    Repeat Exercise...

C++

Please ensure code is fully working, will rate

Question to be answered:

   Repeat Exercise 1 for the class linkedStackType

question one:

       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 this operator.

Code from Q1 build if needed

#include
#include
#include
using namespace std;
template
class stackType
{
private:
int maxStackSize; //variable to store the maximum stack size
int stackTop; //variable to point to the top of the stack


T *array; //pointer to the array that holds the stack elements
public:
//determines whether the stack is empty
bool isEmpty()
{
return(stackTop == 0);
}
//determines whether the stack is full
bool isFull()
{
return(stackTop == maxStackSize);
}

//adds newItem to the stack
void push(const T& newItem)
{
if (!isFull())
{
//adds newItem to the top of the stack
array[stackTop] = newItem;
stackTop++; //increment stackTop
}
else
cout << "Cannot add to a full stack." << endl;
}

//returns the top element of the stack
T top()
{
//if stack is empty terminate the program
assert(stackTop != 0);
//return the element of the stack indicated by stackTop - 1   
return array[stackTop - 1];
}

//removes the top element of the stack
void pop()
{
if (!isEmpty())
stackTop--; //decrement stackTop
else
cout << "Cannot remove from an empty stack." << endl;
}
//gets the stack size
int size()
{
return stackTop;
}
//constructor
//Creates an array of the size stackSize to hold
//the stack elements. The default stack size is 50.
stackType(int stackSize = 50)
{
if (stackSize <= 0)
{
cout << "Size of the array must be positive."<< endl;
cout << "Creating an array of size 50." << endl;
maxStackSize = 50;
}
else
maxStackSize = stackSize; //set the stack size
stackTop = 0; //set stackTop to 0
array = new T[maxStackSize]; //create the array
}
};
template
bool operator==(const stackType& firstStack, const stackType& secondStack)
{
stackType stack1, stack2;
stack1 = firstStack;
stack2 = secondStack;
int size1 = stack1.size();
int size2 = stack2.size();
bool isSame = true;
//Check if the two stacks are the same size

if(size1 == size2)
{
for(int i = 0; i < size1; i++)
{
if(stack1.top() != stack2.top())
{
isSame = false;
}
stack1.pop();
stack2.pop();
}
}
else
{
//The stacks are not of same size
isSame = false;
}
return isSame;
}
//main function
int main()
{
stackType firstStack;
stackType secondStack;
firstStack.push(10);
firstStack.push(109);
firstStack.push(28);
firstStack.push(91);
secondStack.push(10);
secondStack.push(109);
secondStack.push(28);
secondStack.push(91);
if(firstStack == secondStack)
{
cout << "The Stacks Are Equal" << endl;
}
else
{
cout << "The Stacks Are NOT Equal" << endl;
}
return 0;
}

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

#include<iostream>

#include<cstdlib>

#include <assert.h>

using namespace std;

template<class T>

class stackType

{

    private:

       

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

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

       

       

        T *array; //pointer to the array that holds the stack elements

       

    public:

       

        //determines whether the stack is empty

        bool isEmpty()

        {

            return(stackTop == 0);

        }

        //determines whether the stack is full

        bool isFull()

        {

            return(stackTop == maxStackSize);

        }

        

        //adds newItem to the stack

        void push(const T& newItem)

        {

            if (!isFull())

            {

                //adds newItem to the top of the stack

                array[stackTop] = newItem;

                stackTop++; //increment stackTop

            }

            else

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

        }

       

        //returns the top element of the stack

        T top()

        {

            //if stack is empty terminate the program

            assert(stackTop != 0);

           

            //return the element of the stack indicated by stackTop - 1  

            return array[stackTop - 1];

        }

       

        //removes the top element of the stack

        void pop()

        {

            if (!isEmpty())

                stackTop--; //decrement stackTop

            else

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

        }

        //gets the stack size

        int size()

        {

            return stackTop;

        }

        //constructor

        //Creates an array of the size stackSize to hold

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

        stackType(int stackSize = 50)

        {

            if (stackSize <= 0)

            {

                cout << "Size of the array must be positive."<< endl;

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

                maxStackSize = 50;

            }

            else

                maxStackSize = stackSize; //set the stack size

           

            stackTop = 0; //set stackTop to 0

            array = new T[maxStackSize]; //create the array

        }

};

template<class T>

bool operator==(const stackType<T>& firstStack, const stackType<T>& secondStack)

{

    stackType<T> stack1, stack2;

    stack1 = firstStack;

    stack2 = secondStack;

    int size1 = stack1.size();

    int size2 = stack2.size();

    bool isSame = true;

    //Check if the two stacks are the same size

   

    if(size1 == size2)

    {

        for(int i = 0; i < size1; i++)

        {

            if(stack1.top() != stack2.top())

            {

                isSame = false;

            }

           

            stack1.pop();

            stack2.pop();

        }

  }

    else

    {

        //The stacks are not of same size

        isSame = false;

    }

   

    return isSame;

}

//main function

int main()

{

    stackType<int> firstStack;

    stackType<int> secondStack;

    firstStack.push(10);

    firstStack.push(109);

    firstStack.push(28);

    firstStack.push(91);

    secondStack.push(10);

    secondStack.push(109);

    secondStack.push(28);

    secondStack.push(91);

   

    if(firstStack == secondStack)

    {

        cout << "The Stacks Are Equal" << endl;

  }

    else

    {

        cout << "The Stacks Are NOT Equal" << endl;

    }

    return 0;

}

Sample Output

Add a comment
Know the answer?
Add Answer to:
C++ Please ensure code is fully working, will rate Question to be answered:    Repeat Exercise...
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
  • (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...

  • Use C++! This program uses the class myStack to determine the highest GPA from a list of students with their GPA.The program also outputs the names of the students who received the highest GPA. Redo t...

    Use C++! This program uses the class myStack to determine the highest GPA from a list of students with their GPA.The program also outputs the names of the students who received the highest GPA. Redo this program so that it uses the STL list and STL queue! Thank you! HighestGPAData.txt* 3.4 Randy 3.2 Kathy 2.5 Colt 3.4 Tom 3.8 Ron 3.8 Mickey 3.6 Peter 3.5 Donald 3.8 Cindy 3.7 Dome 3.9 Andy 3.8 Fox 3.9 Minnie 2.7 Gilda 3.9 Vinay...

  • Given an array-based stack of integers, sort it largest to smallest using recursion. Do NOT use...

    Given an array-based stack of integers, sort it largest to smallest using recursion. Do NOT use any loop constructs such as while, for and so on. Only use the following stack ADT functions in the recursion: IsEmpty Push Pop Top (note: doesn’t remove anything from the stack). Your program should read 10 integers into a stack from a file named input.txt (outputting them to the screen first) then implement the recursions. Use stacks only, no queues. The program should then...

  • stack.h template class Stack{ public: Stack(int max = 10); ~Stack() {delete [] stack;} bool isEmpty() const...

    stack.h template class Stack{ public: Stack(int max = 10); ~Stack() {delete [] stack;} bool isEmpty() const { return top == -1; } bool isFull() const { return top == MaxStackSize; } T peek() const; void push(const T& x); void pop(); private: int top; int MaxTop; T * stack; } source.cpp What is printed by the following program segment? Stack s; int n; s.push(4); s.push(6); s.push(8); while(!s.isEmpty()) { n = s.peek(); cout << n << ‘ ‘; s.pop(); } cout<< endl;...

  • USING THE C++ CODE BELOW, CAN YOU PLEASE SOLVE THE QUESTION AND EXPLAIN THE STEPS. class...

    USING THE C++ CODE BELOW, CAN YOU PLEASE SOLVE THE QUESTION AND EXPLAIN THE STEPS. class StaticStack { private: int *stack; int capacity; int top; // index of the top element public: StaticStack(int size); // constructor ~StaticStack(); bool isFull(); bool isEmpty(); void push(int); int pop(); }; #include "StaticStack.h" #include <iostream> using namespace std; StaticStack::StaticStack(int size) { stack = new int[size]; // constructing a size sized array capacity = size; top = -1; // empty stack    } StaticStack::~StaticStack() { delete...

  • - implement the Stack ADT using array – based approach. Use C++ program language #include "StackArray.h"...

    - implement the Stack ADT using array – based approach. Use C++ program language #include "StackArray.h" template <typename DataType> StackArray<DataType>::StackArray(int maxNumber) { } template <typename DataType> StackArray<DataType>::StackArray(const StackArray& other) { } template <typename DataType> StackArray<DataType>& StackArray<DataType>::operator=(const StackArray& other) { } template <typename DataType> StackArray<DataType>::~StackArray() { } template <typename DataType> void StackArray<DataType>::push(const DataType& newDataItem) throw (logic_error) { } template <typename DataType> DataType StackArray<DataType>::pop() throw (logic_error) { } template <typename DataType> void StackArray<DataType>::clear() { } template <typename DataType> bool StackArray<DataType>::isEmpty() const {...

  • In C++ Implement a queue data structure using two stacks. Remember a queue has enqueue and...

    In C++ Implement a queue data structure using two stacks. Remember a queue has enqueue and dequeue functions. You could use either the array or linked list implementation for stacks and queues. Source for stack array: --------------------------------------------------- #include<iostream> #define SIZE 100 #define NO_ELEMENT -999999 using namespace std; class Stack { int arr[SIZE]; // array to store Stack elements int top; public: Stack() { top = -1; } void push(int); // push an element into Stack int pop(); // pop the...

  • HI USING C++ CAN YOU PLEASE PROGRAM THIS ASSIGNMENT AND ADD COMMENTS: stackARRAY: #include<iostream> #define SIZE...

    HI USING C++ CAN YOU PLEASE PROGRAM THIS ASSIGNMENT AND ADD COMMENTS: stackARRAY: #include<iostream> #define SIZE 100 #define NO_ELEMENT -999999 using namespace std; class Stack { int arr[SIZE]; // array to store Stack elements int top; public: Stack() { top = -1; } void push(int); // push an element into Stack int pop(); // pop the top element from Stack int topElement(); // get the top element void display(); // display Stack elements from top to bottom }; void Stack...

  • I need help fixing my code.   My output should be the following. Hello, world! : false...

    I need help fixing my code.   My output should be the following. Hello, world! : false A dog, a panic in a pagoda : true A dog, a plan, a canal, pagoda : true Aman, a plan, a canal--Panama! : true civic : true If I had a hi-fi : true Do geese see God? : true Madam, I’m Adam. : true Madam, in Eden, I’m Adam. : true Neil, a trap! Sid is part alien! : true Never odd...

  • c++ please no global varaible write a value returning function input data to dynamically allocate a...

    c++ please no global varaible write a value returning function input data to dynamically allocate a short array of size elements and store the input data entered from the disk file into array.Return the base address of the array allocated.Read the data from the text file Hello.txt and store the data in reverse order in the array.the size of the data set will be in the file size 10 and the data was 1 2 3 4 5 6 7...

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