创建复制构造函数,该构造函数使用链接列表堆栈实现逆转堆栈
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对于该功能中这两个指针的首发声明而言,
这不是很有意义的。他们互相复制。使用一个指针穿越列表
RHS
就足够了。如果要创建传递列表的副本,则功能
push
不合适。您可以以以下方式定义复制构造函数。
我希望类节点的构造函数将创建节点的数据成员设置为
nullptr
。For starters declarations of these two pointers within the function
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.
I hope that the constructor of the class Node sets the data member down of the created node to
nullptr
.务实的方法可能是仅复制数据两次:
A pragmatic approach could be to just copy the data twice: