我很困惑,我将如何使用链接列表在堆栈上重载+运算符 [英] I'm confused on how i'd go about overloading the + operator on a stack using a linked list

查看:160
本文介绍了我很困惑,我将如何使用链接列表在堆栈上重载+运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我知道如何使用链接列表一般,我将粘贴如下:

  List操作符+(const List& l)const 
{
//复制第一个列表
List t = * this;
Node * n = l.head;

//遍历第二个列表并将每个元素复制到新列表
while(n!= NULL&&!t.IsFull())
{
t.InsertAfter(n-> data);
n = n-> link;
}

return t;
}

现在在我的堆栈中我有一个像这样的东西:

 堆栈运算符+(const Stack& s)const 
{
//复制第一个列表
Stack t = * this;
Node * n = s.top;

//遍历第二个列表并将每个元素复制到新列表
while(n!= NULL&&!t.IsFull())
{
t.Push(n-> data);
n = n-> link;
}

return t;
}

我的问题我想是,这将是正确的方式它?如果这个问题的标题不是很清楚,我很想提前道歉,我有麻烦,甚至弄清楚如何说的话。



编辑: / p>

我发现了一个循环,看起来像这样参考Caleb的回答:

  Stack t = * this; 
stack u = * this;
Node * n = s.top;
while(n!= NULL&!t.IsFull())
{
t.Push(n-> data);
u.Push(n-> data);
n = n-> link;
}

return t;
}


解决方案



您正在从的顶部推项目, t ,这意味着 s 中的项目将出现在 t ,但是与它们在 s 上的顺序相反。如果这是你想要的,你的实现是好的,但很难想到一种情况,那种行为将是有用的或预期的。从底部 s 到顶部可以解决这个问题,但这不是一个选项,如果你必须严格遵守堆栈操作。仅使用堆栈操作的另一个选项是首先通过将它们一次推送到中间堆栈上来反转 s ,然后通过推入 t


First of all, i know how to do this using a linked list in general, which i will paste below:

    List operator+(const List& l) const
    {
        // copy the first list
        List t = *this;
        Node *n = l.head;

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

        return t;
    }

Now in my Stack i have something that looks like this:

Stack operator+(const Stack& s) const
    {
        // copy the first list
        Stack t = *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);
            n = n->link;
        }

        return t;
    }

My question i guess would be, would this be the right way of going about it? And i'd like to apologize in advance if the Title of this question isn't very clear, i had trouble even figuring out how to word it.

Edit:

I came out with a loop looking like this in reference to Caleb's answer:

        Stack t = *this;
        Stack u = *this;
        Node *n = s.top;
while (n != NULL && !t.IsFull())
        {
            t.Push(n->data);
            u.Push(n->data);
            n = n->link;
        }

        return t;
    }

解决方案

would this be the right way of going about it?

You're pushing items from the top of s onto t, and that means that the items from s will appear on t, but in reverse order compared to what they were on s. If that's what you want, your implementation is fine, but it's hard to think of a situation where that behavior would be useful or expected. Pushing from the bottom of s to the top would fix the problem, but that's not an option if you have to stick strictly to stack operations. Another option that uses only stack operations is to first reverse s by pushing them one at a time onto an intermediate stack, and then reverse again by pushing onto t.

这篇关于我很困惑,我将如何使用链接列表在堆栈上重载+运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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