创建复制构造函数,该构造函数使用链接列表堆栈实现逆转堆栈

发布于 2025-01-24 13:31:30 字数 591 浏览 5 评论 0原文

linkList::linkList(linkList const& rhs){
    Node *temp = rhs.top;
    Node *temp_stack = rhs.top;
    while(temp){
        char value = temp->letter;
//        push(value);
        push(temp_stack->letter);
        temp = temp_stack->down;
        temp_stack = temp_stack->down;
//        temp = temp->down;
    }
}

void linkList::push(char c) {
    Node* new_top = new Node(c);
    new_top->down = top;
    top = new_top;
}

我的复制构造函数上有一个问题,即当我调用它时,它会以相反的形式显示链接列表,因为我将其推向新链接列表的背面。假设我的功能正常工作100%,我无法更改功能。我将如何反向添加它? 我在这里查看了夫妇解决方案,但没有什么帮助。

linkList::linkList(linkList const& rhs){
    Node *temp = rhs.top;
    Node *temp_stack = rhs.top;
    while(temp){
        char value = temp->letter;
//        push(value);
        push(temp_stack->letter);
        temp = temp_stack->down;
        temp_stack = temp_stack->down;
//        temp = temp->down;
    }
}

void linkList::push(char c) {
    Node* new_top = new Node(c);
    new_top->down = top;
    top = new_top;
}

I have a problem on my copy constructor that when I call it it, it display the link-list in reverse which make sense cause I am pushing it to the back of the new link-list. assuming that my function is working 100 percent and I cant change the function. how would I go about adding it in reverse?
I looked over couple solution in here but no pretty helpful.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

爱格式化 2025-01-31 13:31:30

对于该功能中这两个指针的首发声明而言,

    Node *temp = rhs.top;
    Node *temp_stack = rhs.top;

这不是很有意义的。他们互相复制。使用一个指针穿越列表RHS就足够了。

如果要创建传递列表的副本,则功能push不合适。

您可以以以下方式定义复制构造函数。

linkList::linkList( linkList const& rhs ) : top( nullptr )
{
    Node **current = ⊤

    for ( Node *temp = rhs.top; temp != nullptr; temp = temp->down )
    {
        *current = new Node( temp->letter );
        current = &( *current )->down;
    } 
}

我希望类节点的构造函数将创建节点的数据成员设置为nullptr

For starters declarations of these two pointers within the function

    Node *temp = rhs.top;
    Node *temp_stack = rhs.top;

does not make a great sense. They duplicate each other. It is enough to use one pointer to traverse the list rhs.

If you want to create a copy of the passed list then the function push is not suitable.

You could define the copy constructor the following way.

linkList::linkList( linkList const& rhs ) : top( nullptr )
{
    Node **current = ⊤

    for ( Node *temp = rhs.top; temp != nullptr; temp = temp->down )
    {
        *current = new Node( temp->letter );
        current = &( *current )->down;
    } 
}

I hope that the constructor of the class Node sets the data member down of the created node to nullptr.

戏剧牡丹亭 2025-01-31 13:31:30

务实的方法可能是仅复制数据两次:

linkList(linkList const& rhs) {
    linkList tmp;
    // first copy to `tmp`, which will have them in reverse:
    for(Node* curr = rhs.top; curr; curr = curr->down) 
        tmp.push(curr->letter);

    // then populate *this from `tmp` which will then have them
    // in the original order:
    for(Node* curr = tmp.top; curr; curr = curr->down)
        push(curr->letter);
}

A pragmatic approach could be to just copy the data twice:

linkList(linkList const& rhs) {
    linkList tmp;
    // first copy to `tmp`, which will have them in reverse:
    for(Node* curr = rhs.top; curr; curr = curr->down) 
        tmp.push(curr->letter);

    // then populate *this from `tmp` which will then have them
    // in the original order:
    for(Node* curr = tmp.top; curr; curr = curr->down)
        push(curr->letter);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文