我的名单副本之一的大小在我的筹码不正确打印出来(The size of one of my list

2019-10-19 08:29发布

首先,这份名单的最大尺寸为30,中创建存储在项数每个列表,这是递增,由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或“空”相反,但必须有一个办法让它停止这样做。

Answer 1:

同时保持节点的单一列表最好复制**店指向需要被设置为下一个节点的变量*:

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 */;
}

除非小心地删除任何现有条目这赋值运算符将产生一个内存泄漏。 也许,不过已经有了明确的()方法?

后来:这些构造函数可用于:

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;
  }
}


Answer 2:

Stack<int> s2(s1);

这将调用默认的拷贝构造函数,而不是运营商=。 然而,由于您的副本逻辑是运营商实现=,节点永远不会复制到S2。

你需要写一个拷贝构造函数和移动从逻辑运算符=到其中:

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


文章来源: The size of one of my list copies is printing out incorrectly in my stack