C++栈的深层次复制。求助啊
答案:1 悬赏:10
解决时间 2021-03-23 03:31
- 提问者网友:暖心后
- 2021-03-22 11:29
蛋疼,代码太长了,粘贴不了。。1621337716QQ,,热心的加一下啊
最佳答案
- 二级知识专家网友:花一样艳美的陌生人
- 2021-03-22 12:19
template <class Type>
void LinkedStack<Type>::Copy(const LinkedStack<Type> &other)
{
Node<Type> *newNode;
Node<Type> *current;
if(top != NULL) //若当前这个堆栈不为空,则先销毁这个堆栈
Destroy();
if(other.top == NULL)
top = NULL;
else
{
current = other.top;
top = new Node<Type>;
top->info = current->info; //复制第一个结点
top->link = NULL;
current = current->link;
while(current != NULL) //复制余下的结点
{
newNode = new Node<Type>;
newNode->info = current->info;
newNode->link = top;
top = newNode;
current = current->link;
}
}
}
void LinkedStack<Type>::Copy(const LinkedStack<Type> &other)
{
Node<Type> *newNode;
Node<Type> *current;
if(top != NULL) //若当前这个堆栈不为空,则先销毁这个堆栈
Destroy();
if(other.top == NULL)
top = NULL;
else
{
current = other.top;
top = new Node<Type>;
top->info = current->info; //复制第一个结点
top->link = NULL;
current = current->link;
while(current != NULL) //复制余下的结点
{
newNode = new Node<Type>;
newNode->info = current->info;
newNode->link = top;
top = newNode;
current = current->link;
}
}
}
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯