只使用栈操作的排序

655阅读 0评论2010-09-16 wqfhenanxc_cu
分类:C/C++

只使用栈操作的排序

Write a C program to sort a stack in ascending order. You should not
make any assumptions about how the stack is implemented. The following
are the only
functions that should be used to write this program:
Push | Pop | Top | IsEmpty | IsFull

解答:
 The algorithm is O(N^2) and appears below.

void sort_stack(Stack * src, Stack * dest)
{
    while (!src->IsEmpty())
   {
       Int tmp = src->Pop();
       while(!dest->IsEmpty() && dest->Top() > tmp)
       {
          src->Push(dest->Pop());
       }
       dest->Push(tmp);
   }


}


上一篇:最大连续子序列问题求解方法总结 转载
下一篇:动态规划相关问题总结 转载