我的列表副本之一的大小在我的堆栈中打印不正确 [英] The size of one of my list copies is printing out incorrectly in my stack

查看:130
本文介绍了我的列表副本之一的大小在我的堆栈中打印不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要开始,这个列表的最大大小是30,并且创建的每个列表中的项目数量存储在num_items中,通过push和pop方法我在其他地方增加和减少,但我想知道我是否需要保持这里的num_items的轨道。我会显示输出我期待与输出我得到:

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

提供输出的代码是:

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; 在代码中我将显示如下

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

输出我得到我的大小结果是1,我会显示整个输出再次在一个图像:

The output i get for my size turns out to be 1, i will show the whole output again in an image:

第二个编辑:

我现在已经修改我的代码如下:

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

这个虽然我的大小只计数到9而不是10,我想,因为我的循环跳过0或NULL,而是,但必须有一种方法,使它停止这样做。

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.

推荐答案

最好复制单个列表,同时保持Node ** store指向需要设置到下一个Node *的变量:

A single list is best copied while maintaining a Node** store pointing to the variable that needs to be set to the next 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?

后来
这些构造函数可能被使用:

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

这篇关于我的列表副本之一的大小在我的堆栈中打印不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆