简体   繁体   English

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

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

To start, the max size of this list is 30 and the number of items in each list created is stored in num_items, which is incremented and decremented by push and pop methods i have elsewhere but i am wondering if i need to keep track of the num_items here as well. 首先,此列表的最大大小为30,每个创建的列表中的项目数存储在num_items中,通过我在其他地方使用的push和pop方法增加和减少该数量,但是我想知道是否需要跟踪这里也有num_items。 I will show the output i'm expecting along with the output i am getting: 我将显示期望的输出以及得到的输出:

在此处输入图片说明

I will now show the code that copies my stack: 现在,我将显示复制我的堆栈的代码:

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

The code that is supplying the output is: 提供输出的代码是:

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;

Edit: 编辑:

After initializing num_items = 0; 初始化后num_items = 0; in the code i will show below 在我将在下面显示的代码中

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

The output i get for my size turns out to be 1, i will show the whole output again in an image: 我为我的尺寸得到的输出结果为1,我将再次在图像中显示整个输出:

在此处输入图片说明

Second Edit: 第二次编辑:

I have now modified my code to the following: 我现在将代码修改为以下内容:

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

with this though i my size only counts up to 9 instead of 10, i figure because my loop is skipping over 0 or "NULL" rather, but there must be a way to make it stop doing that. 尽管我的大小仅由9而不是10决定,但我认为这是因为我的循环实际上跳过了0或“ NULL”,但是必须有一种使其停止的方法。

A single list is best copied while maintaining a Node** store pointing to the variable that needs to be set to the next Node*: 最好复制单个列表,同时维护一个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 */;
}

This assignment operator will produce a memory leak unless care is taken to remove any existing entries. 除非小心删除所有现有条目,否则此赋值运算符将产生内存泄漏。 Perhaps there's already a clear() method? 也许已经有一个clear()方法?

Later: These constructors might be used: 以后:可以使用这些构造函数:

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

This clear method should be used where indicated: 应该在指示的地方使用这种清晰的方法:

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

This calls the default copy constructor and not the operator=. 这将调用默认的复制构造函数,而不是operator =。 However, since your copy logic is implemented in the operator=, the nodes are never copied into s2. 但是,由于复制逻辑是在operator =中实现的,因此节点永远不会复制到s2中。

You need to write a copy constructor and move the logic from operator= into it: 您需要编写一个复制构造函数,并将逻辑从operator =移入其中:

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

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

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