我需要进行深层复制,我是否正确使用了复制构造函数?

问题描述 投票:-1回答:1

我需要进行深度复制。我是否正确使用了复制构造函数?我应该改变什么?

#include <iostream>
#include <sstream>
using namespace std;

class LinkedList
{
    public:
        int data;
        LinkedList* prevNode;

        LinkedList()
        {
            int dd = 0;
            prevNode = nullptr;
        }

        LinkedList(int dd, LinkedList* pr)
        {
            data = dd;
            prevNode = pr;
        }
};

class Stack
{
    private:
        LinkedList* topNode;

    public:
        Stack();
        Stack(const Stack& original);
        ~Stack();

        bool isEmpty() const;
        int top() const;
        int pop();
        void push(int);
};

Stack::Stack()
{
    topNode = nullptr;
}

Stack::Stack(const Stack& original)
{
    this->topNode = original.topNode;
}

Stack::~Stack()
{
    while (!isEmpty())
    {
        pop();
    }
}

bool Stack::isEmpty() const
{
    if (topNode == NULL)
    {
        return true;
    }
    return false;
}

int Stack::top() const
{
    if (isEmpty())
    {
        throw runtime_error("error: stack is empty");
    }
    return topNode->data;
}

int Stack::pop()
{
    int topVal = top();

    LinkedList* oldtop = topNode;
    topNode = topNode->prevNode;
    return topVal;
}

void Stack::push(int newData)
{
    LinkedList* newNode = new LinkedList(newData, topNode);
    topNode = newNode;
}

int returnNumber(string inputString)
{
    istringstream fr(inputString); 
    int number;

    while (fr >> number)
    { 
        return number;
    }
    if (fr.fail())
    {
        throw runtime_error("error: not a number");
    }

    return number;
}

void list(Stack s)
{
    cout << "[";
    while (!s.isEmpty())
    {
        cout << s.pop();
        if (!s.isEmpty())
        {
            cout << ",";
        }
    }
    cout << "]" << endl;
}

void readCommands(Stack& newStack)
{
    string command = " ";
    while (command != "end")
    {
        cout << "stack> ";
        cin >> command;
        cout << endl;
        if (cin.eof())
        {
            break;
        }

        try 
        {
            if (command == "top")
            {
                cout << newStack.top() << endl;
            }
            else if (command == "pop")
            {
                cout << newStack.pop() << endl;
            }
            else if (command == "push")
            {
                string inputValue;
                cin >> inputValue;

                int number = returnNumber(inputValue);
                newStack.push(number);
                cin.ignore();
            }
            else if (command == "list")
            {
                list(newStack);
            }
            else
            {  
                if (command != "end")
                {
                    throw runtime_error("error: invalid command");
                }
            }
        }
        catch (runtime_error e)
        {
            cout << e.what() << endl;
        } 
    }
}

int main()
{
    Stack newStack;

    readCommands(newStack);
    return 0;
}
c++ linked-list stack copy-constructor
1个回答
2
投票

我需要进行深层复制,我是否正确使用了复制构造函数?

没有要进行深层复制,您需要在新堆栈中分配新空间。看起来像这样:

class LinkedList
    {
        public:
             static LinkedList* Copy(LinkedList* c) {
                  if(c == nullptr) {
                       return nullptr;
                  } else {
                      return new LinkedList(data, Copy(prevNode));
                  }
             }
};

此静态函数获取任何指针,并创建其值及其列表的副本。

然后,在您的堆栈中:

Stack::Stack(const Stack& original)
    {
        this->topNode = LinkedList::Copy(original.topNode);
    }

通过这种方式,您实际上是在创建新的内存,而不是盲目地复制指针。那是深层复制的大想法。

如果您仍然对深复制的工作方式感到困惑,建议您咨询教科书或教授。

© www.soinside.com 2019 - 2024. All rights reserved.