数据结构与程序设计课后题2.2

来源:互联网 发布:淘宝上的折扇哪家好 编辑:程序博客网 时间:2024/06/06 13:11

 

exercises2.2

E2 (b)

use the stack methods and a temporary stack to retrieve entries from the stack source and add each entry to the stack dest and restore the stack source.

 

answer:

 

 Error_code copy_stack(Stack & dest , Stack & source)

 {

  Error_code detected = success ;

  Stack temp ;

  Stack_entry item ;

  while (detected == success && ! source.empty()) {

  detected = source.top( item ) ;

  detected = source.pop() ;

  if (detected == success ) detected = temp.push(item) ;

}

  while(detected == success && ! temp.empty())  {

    detected = temp.top(item) ;

    detected = temp.pop() ;

    if (detected == success) detected = source.push(item) ;

    if(detected == success) detected = dest.push(item) ;

  }

return detected;

}

 

 

原创粉丝点击