Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1071288
  • 博文数量: 242
  • 博客积分: 10209
  • 博客等级: 上将
  • 技术积分: 3028
  • 用 户 组: 普通用户
  • 注册时间: 2008-03-12 09:27
文章分类

全部博文(242)

文章存档

2014年(1)

2013年(1)

2010年(51)

2009年(65)

2008年(124)

我的朋友

分类: C/C++

2010-09-16 18:50:52

只使用栈操作的排序

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);
   }


}


阅读(1071) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~