数据结构与程序设计 作业

E2, 采用栈的方法,为下面的说明编写一个函数 copy_satck:

         Error_codecopy_stack(stack &dest,Stack &source);

         前置条件:无。

         后置条件:Stack dest已经成为 Stack source 的一个完全副本,而source未被改变。

                              如果检测到错误,则返回一个合适的代码,否则返回success。

(b 使用Stack方法和一个临时的Stack,从Stack source中抽取元素并将每个元素加入到Stack dest 中,并回复Stack source。


Error_code Stack::copy_stack(Stack&dest,Stack &source)

/*Pre:None.

 Post:Stack dest has become an exact copy of Stack source;source

  isunchanged.If an error is deteced,an apporpriate code is returned.*/

  {

     Error_code outcome = success;

     Stack T; //临时的Stack

     Stack_entry item; //临时储存元素

     while(outcome == success&&!source.empty())

     {

         outcome = source.top(item);

         outcome = source.pop();

         if(outcome == success)

           outcome = T.push(item);

     }

     while(outcome == success&&!T.empty())

     {

         outcome = T.top(item);

         outcome = T.pop();

         if(outcome == success)

         {

              outcome = dest.push(item);

             outcome = source.push(item);

         }

     }

     return outcome;

  }


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值