繁体   English   中英

我的一份列表副本的尺寸在我的纸叠中打印不正确

[英]The size of one of my list copies is printing out incorrectly in my stack

首先,此列表的最大大小为30,每个创建的列表中的项目数存储在num_items中,通过我在其他地方使用的push和pop方法增加和减少该数量,但是我想知道是否需要跟踪这里也有num_items。 我将显示期望的输出以及得到的输出:

在此处输入图片说明

现在,我将显示复制我的堆栈的代码:

void operator=(const Stack& s)
    {
        if (s.top == NULL)
            top = NULL;
        else
        {
            top = new Node;
            top->data = s.top->data;
            Node* newP = top;

                for(Node* curr = s.top->link; curr != NULL; curr = curr->link)
                {
                    if(num_items != MAX_SIZE)
                    {
                    newP->link = new Node;
                    newP = newP->link;
                    newP->data = curr->data;
                    }
                }
        }
    }

提供输出的代码是:

Stack<int> s2(s1); // s2 declared as a copy of s1
    cout << "*declare s2 as a copy of s1 (stack s2(s1))\ns2=" << s2 << endl;
    cout << "s2.Size()=" << s2.Size() << endl;
    cout << "s2.IsEmpty()=" << ((s2.IsEmpty()) ? "T" : "F") << endl;
    cout << "s2.IsFull()=" << ((s2.IsFull()) ? "T" : "F") << endl;
    cout << "s2.Peek()=" << s2.Peek() << endl;
    cout << endl;

编辑:

初始化后num_items = 0; 在我将在下面显示的代码中

        void operator=(const Stack& s)
    {
        if (s.top == NULL)
            top = NULL;
        else
        {
            top = new Node;
            top->data = s.top->data;
            Node* newP = top;

                for(Node* curr = s.top->link; curr != NULL; curr = curr->link)
                {
                    num_items = 0;
                    if(num_items != MAX_SIZE)
                    {
                    newP->link = new Node;
                    newP = newP->link;
                    newP->data = curr->data;
                    num_items++;
                    }
                }
        }
    }

我为我的尺寸得到的输出结果为1,我将再次在图像中显示整个输出:

在此处输入图片说明

第二次编辑:

我现在将代码修改为以下内容:

void operator=(const Stack& s)
    {
        if (s.top == NULL)
            top = NULL;
        else
        {
            top = new Node;
            top->data = s.top->data;
            Node* newP = top;
                num_items = 0;
                for(Node* curr = s.top->link; curr = NULL; curr = curr->link)

                {

                    if(num_items != MAX_SIZE)
                    cout<< num_items;
                    {
                    newP->link = new Node;
                    newP = newP->link;
                    newP->data = curr->data;
                    ++num_items;
                    }
                }
        }
    }

尽管我的大小仅由9而不是10决定,但我认为这是因为我的循环实际上跳过了0或“ NULL”,但是必须有一种使其停止的方法。

最好复制单个列表,同时维护一个Node **存储,该存储指向需要设置为下一个Node *的变量:

void operator=( const Stack& rhs ){ // or return Stack&
  // call this->clear() to avoid memory leak
  if( rhs.top == NULL ){ top = NULL; return /* *this */; }
  Node** store = &top;
  for( Node* curr = rhs.top; curr != NULL; curr = curr->link ){
    Node* newNode = new Node;
    num_items++;
    newNode->data = curr->data;
    *store = newNode;
    store = &newNode->link;
  }
  return /* *this */;
}

除非小心删除所有现有条目,否则此赋值运算符将产生内存泄漏。 也许已经有一个clear()方法?

以后:可以使用这些构造函数:

Stack() : num_items(0), top(NULL) {}
Stack( const Stack& other ) {
  *this = other;
}

应该在指示的地方使用这种清晰的方法:

void clear(){
  Node* curr = top;
  while( curr != NULL ){
    Node* next = curr->link;
    delete curr;
    curr = next;
  }
}
Stack<int> s2(s1);

这将调用默认的复制构造函数,而不是operator =。 但是,由于复制逻辑是在operator =中实现的,因此节点永远不会复制到s2中。

您需要编写一个复制构造函数,并将逻辑从operator =移入其中:

Stack(const Stack & other)
{
    // Copy other stack here
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM