【问题标题】:My 3rd stack is printing out in reverse order from my 1st and 2nd stack我的第 3 叠以与第 1 叠和第 2 叠相反的顺序打印出来
【发布时间】:2014-02-14 00:26:34
【问题描述】:

下面我将显示我的程序运行时的输出:

现在我将显示预期的输出:

我将展示我教授的代码正在实现的函数,或者更确切地说是我在下面制作的“复制构造函数”或重载函数:

void operator=(const Stack& s)
    {
        if (s.top == NULL){
            num_items = 0;
            top = NULL;}
        else
        {
            top = new Node;
            top->data = s.top->data;
            Node* newP = top;
                num_items = 1;
                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;
                    ++num_items;
                    }
                }
        }
    }

最后我会展示使用这个函数的代码,我的导师的代码:

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

我尝试了各种方法,例如制作一个机器人指针来尝试确定堆栈底部的位置,然后像这样打印出来,但它似乎没有用,或者我写错了。

根据要求,这是 operator+ 代码:

    Stack operator+(const Stack& s) const
    {
        // copy the first list
        Stack t = *this;
        Stack u = *this;
        Node *n = s.top;

        // iterate through the second list and copy each element to the new list
        while (n != NULL && !t.IsFull())
        {
            t.Push(n->data);
            u.Push(n->data);
            n = n->link;
        }

        return u;
    }

【问题讨论】:

  • s3 应该是s2 的副本?那你为什么要s3 = s3 + s2
  • @remyabel 我的导师写了这个的主要功能,所以老实说我不确定,但我不允许更改他的代码中的任何内容
  • 在这种情况下,显示operator+
  • @remyabel 我已经对其进行了编辑以显示该功能
  • @remyabel ideone.com/o3n1fG

标签: c++ linked-list stack


【解决方案1】:

无论如何,您似乎没有正确遵循Caleb的建议:

您将项目从 s 的顶部推到 t,这意味着 s 中的项目将出现在 t 上,但与 他们在s。 [...] 另一个仅使用堆栈操作的选项 是首先将s 一次推到一个 中间堆栈,然后通过推入 t 再次反转。

t 将是您的中间堆栈,u 将以正确的顺序包含项目。将您的代码更改为以下内容:

// Populate the intermediate stack
while (n != NULL && !t.IsFull())
{
  t.Push(n->data);
  n = n->link;
}

// Begin popping the intermediate stack
// into the resulting stack
n = t.top;
while (n != NULL && !t.IsEmpty())
{
  u.Push(n->data);
  t.Pop();
  n = t.top;
}

另外,为了修正不正确的尺寸,请使用您的其他问题之一中的代码。将您当前的 operator= 代码替换为 following 可以解决此问题:

Stack& operator=( const Stack& rhs ){
  // 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;
}

Live Example

输出:

*declare s3 as a copy of s2 (stack s3 = s2)
s3=81 64 49 36 25 16 9 4 1 0 
s3.Size()=10
s3.IsEmpty()=F
s3.IsFull()=F
s3.Peek()=81

【讨论】:

  • 哇..非常感谢,我误解了他所说的话,并认为我的功能正在以正确的方式扭转它。
  • @Breon 没问题,感谢您发布完整的代码示例,但您的代码的某些部分仍然不正确。我建议您回顾一下您的其他问题并更详细地查看答案,因为您可以看到我使用其中两个来解决问题。
  • 是的,我刚刚注意到,我仍然有一些错误,并且在不应该出现的地方打印了一个堆栈,再次感谢您。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-09-28
  • 1970-01-01
  • 1970-01-01
  • 2019-09-12
  • 1970-01-01
  • 1970-01-01
  • 2017-07-21
相关资源
最近更新 更多