public void Push(T val) { queLower.Enqueue(val); }
复制代码
public T Pop()
{
if (queLower.Count == 0 )
{
Console.Write("Stack is empty!");
return default(T);
}
if (queLower.Count > 0)
{
for (int i = 0; i < queLower.Count - 1;i++ )
{
queLower.Enqueue(queLower.Dequeue ());
}
}
return queLower.Dequeue();
}
复制代码
如何不用queue.count或queue.isempty()来实现? (该疑问源自这个帖子:http://www.careercup.com/question?id=3185682 有个人说他在微软技术面时候不让使用queue.count)
引用自该帖子:
I got the same question during a Microsoft technical screen and the guy is like I don't want you to use a count(q.Count) on the queue! Nor to check for queue.isEmtpy(). So my solution relied on catching the exception and handling it and I had a slight bug in my implementation which I discovered but wasn't able to fix. Guess what.. reject! We think the world is sane and we are programming for objects/collection and can count them, apparently we can't :)