-3

This is what im supposed to do:
Add the following operation to the class stackType:

void reverseStack(stackType &otherStack);

This operation copies the elements of a stack in reverse order onto another stack.

Consider the following statements.

stackType stack1;
stackType 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 is destroyed and stack1 is unchanged.

And here is the code i have for it.

#include<iostream>
#include<stdlib.h>

using namespace std;

template<class Type>
class stackType
{
    Type s[10];
    int top, n;

public:
    stackType()
    {
        top = -1;
        n = 50;
    }

    stackType(int size)
    {
        top = -1;
        n = size;
    }

    void push(Type elt)
    {
        if (top < n - 1)
            s[++top] = elt;
        else
            cout << "\n\tstack is full.Can't insert " << elt << endl;
    }

    void pop()
    {
        if (top < 0)
            cout << "\n\tstack is empty.\n";
        else
            cout << "\n\tPoped elt : " << s[top--];
    }

    void display() {
        for (int i = 0; i <= top; i++) {
            cout << s[i] << " ";
        }
        cout << endl;
    }

    void reverseStack(stackType<Type> &otherStack) {
        //Destroy content of other stack by making top to -1
        otherStack.top = -1;
        //Iterate the current stack and push the elements to other stack
        for (int i = top; i >= 0; i--) {
            otherStack.push(s[i]);
        }
    }
};

int main()
{
    stackType<int> stack1;

    stack1.push(10);
    stack1.push(20);
    stack1.push(30);

    cout << "Stack1 content: \n";
    stack1.display();

    stackType<int> stack2;
    stack1.reverseStack(stack2);

    cout << "Stack2 content: \n";
    stack2.display();
    return 0;
}

The code the book gave me was this:

#include <iostream>
#include <cassert>

using namespace std;

template <class Type>
class stackADT
{
public:
    virtual void initializeStack() = 0;
    virtual bool isEmptyStack() const = 0;
    virtual bool isFullStack() const = 0;
    virtual void push(const Type& newItem) = 0;
    virtual Type top() const = 0;
    virtual void pop() = 0;
};

template <class Type>
class stackType : public stackADT<Type>
{
private:
    int maxStackSize;
    int stackTop;
    Type *list;

public:
    void initializeStack()
    {
        stackTop = 0;
        cout << "stackTop " << stackTop << endl;
    }

    void print()
    {
        for (int i = 0; i < stackTop; i++)
        {
            cout << list[i] << endl;
        }
    }

    bool isEmptyStack() const
    {
        return(stackTop == 0);
    }

    bool isFullStack() const
    {
        return(stackTop == maxStackSize);
    }

    void push(const Type& newItem)
    {
        if (!isFullStack())
        {
            list[stackTop] = newItem;
            stackTop++;
        }
        else
        {
            cout << "Cannot add to a full stack." << endl;
        }
        cout << "stacktop: " << stackTop << endl;
        system("pause");
    }

    Type top() const
    {
        assert(stackTop != 0); //if stack is empty, terminate the program.                            
        return list[stackTop - 1];
    }

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

    stackType(int stackSize = 100)
    {
        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;
            cout << "maxStackSize " << maxStackSize << endl;
        }
        stackTop = 0;
        list = new Type[maxStackSize];
    }

    stackType(const stackType<Type>& otherStack)
    {
        list = NULL;
        copyStack(otherStack);
    }

    ~stackType()
    {
        delete[] list;
    }

    const stackType<Type>& operator=(const stackType<Type>& otherStack)
    {
        if (this != &otherStack)
        {
            copyStack(otherStack);
        }
        return *this;
    }

    bool operator==(const stackType<Type>& otherStack) const
    {
        if (this == &otherStack)
        {
            return true;
        }
        else
        {
            if (stackTop != otherStack.stackTop)
            {
                return false;
            }
            else
            {
                for (int i = 0; i < stackTop; i++)
                {
                    if (list[i] != otherStack.list[i])
                    {
                        return false;
                    }
                    return true;
                }
            }
        }
    }

    void copyStack(const stackType<Type>& 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];
        }
    }
};

int main()
{
    stackType<int> stack1(50);
    stackType<int> stack2(50);

    stack1.initializeStack();
    stack1.push(23);
    stack1.push(45);
    stack1.push(38);
    stack1.print();

    stack2 = stack1;

    if (stack1 == stack2)
        cout << "stack1 and stack2 are identical" << endl;
    else
        cout << "stack1 and stack2 are not identical" << endl;

    stack2.pop();
    stack2.push(38);

    cout << "**** After pop and push operations on stack2 ****" << endl;
    if (stack1 == stack2)
        cout << "stack1 and stack2 are identical" << endl;
    else
        cout << "stack1 and stack2 are not identical" << endl;

    stack2.push(11);

    cout << "**** After another push operation on stack2 ****" << endl;
    if (stack1 == stack2)
        cout << "stack1 and stack2 are identical" << endl;
    else
        cout << "stack1 and stack2 are not identical" << endl;

    return 0;
}
Swordfish
  • 12,971
  • 3
  • 21
  • 43
elboytoy
  • 1
  • 1
  • "my code isnt executing visual studio 2017, c++" The code compiles and runs. So what is your question? – Swordfish Oct 21 '18 at 21:11
  • My window isnt opening up and it is giving me the The thread 0x5914 has exited with code 0 (0x0). The thread 0x242c has exited with code 0 (0x0). The thread 0x8760 has exited with code 0 (0x0). The thread 0x825c has exited with code 0 (0x0). The program '[38476] Project3.exe' has exited with code 0 (0x0). – elboytoy Oct 21 '18 at 22:05
  • Run your code with menu [Debug] => [Start without debugging]. – Swordfish Oct 21 '18 at 22:06
  • it just closes the pop up window as soon as it opens – elboytoy Oct 21 '18 at 23:11
  • Possible duplicate of [How to keep the console window open in Visual C++?](https://stackoverflow.com/questions/454681/how-to-keep-the-console-window-open-in-visual-c) – Steve Oct 21 '18 at 23:29
  • Menu [Tools] => [Options]. In the tree to the left go to [Debugging] => [General], in the list to the right untick "Automatically close the console when debugging stops". – Swordfish Oct 21 '18 at 23:54

1 Answers1

0

enter image description here

You need to create a breakpoint at the end of your code (End of your main function).