posts - 71,  comments - 41,  trackbacks - 0
被考过两次
class ZStackQueue
{
public:
    
class ZStack
    
{
    
public:
        ZStack(
int size);
        
~ZStack();

        
int Pop();
        
void Push(int e);
        
bool Empty(){return top == 0;}

    
private:
        
int *s;
        
int top;
        
int size;
    }
;

public:
    ZStackQueue(
int n) : s1(n), s2(n){}
    
void EnQueue(int e);
    
int DeQueue();

private:
    ZStack s1, s2;
}
;

ZStackQueue::ZStack::ZStack(
int n)
: s(
0), top(0), size(n)
{
    s 
= new int[size]();
}


ZStackQueue::ZStack::
~ZStack()
{
    
if (s)
    
{
        delete [] s;
        s 
= 0;
    }


    top 
= 0;
    size 
= 0;
}


int ZStackQueue::ZStack::Pop()
{
    
if (top > 0)
        
return s[--top];
    
else
        
return ~0;
}


void ZStackQueue::ZStack::Push(int e)
{
    
if (top < size)
        s[top
++= e;
}


void ZStackQueue::EnQueue(int e)
{
    s1.Push(e);
}


int ZStackQueue::DeQueue()
{
    
if (!s2.Empty())
    
{
        
return s2.Pop();
    }
 
    
else
    
{
        
while (!s1.Empty())
            s2.Push(s1.Pop());

        
return s2.Pop();
    }

}
posted on 2006-11-26 18:45 Charles 阅读(1399) 评论(3)  编辑 收藏 引用 所属分类: 面试小算法

FeedBack:
# re: 两个堆栈模拟一个队列
2006-11-28 11:47 | 踏雪赤兔
呵呵~~我学数据结构时也做过这道题  回复  更多评论
  
# re: 两个堆栈模拟一个队列
2006-11-28 11:48 | 踏雪赤兔
呵呵~~我学数据结构时也做过这道题  回复  更多评论
  
# re: 两个堆栈模拟一个队列
2006-11-28 19:01 | erran
郁闷 当初数据结构是用c语言的教材..   回复  更多评论
  

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


<2006年11月>
2930311234
567891011
12131415161718
19202122232425
262728293012
3456789

决定开始写工作日记,记录一下自己的轨迹...

常用链接

留言簿(4)

随笔分类(70)

随笔档案(71)

charles推荐访问

搜索

  •  

积分与排名

  • 积分 - 48113
  • 排名 - 455

最新评论

阅读排行榜

评论排行榜