首先,这份名单的最大尺寸为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或“空”相反,但必须有一个办法让它停止这样做。