原题链接:https://leetcode-cn.com/problems/implement-stack-using-queues/
本题为基础知识,根本不难,只是对这个这类内存形式的存储方式有点陌生,但是刷多了以后会发现这个东西真的有用,能解决很多问题,尤其是关于存储方面的问题。
public class MyStack {
Queue<int> q = null;
/** Initialize your data structure here. */
public MyStack()
{
q = new Queue<int>();
}
/** Push element x onto stack. */
public void Push(int x)
{
var queue = new Queue<int>();
queue.Enqueue(x);//add in end
foreach (var elemet in q)
{
queue.Enqueue(elemet);
}
q = queue;
}
/** Removes the element on top of the stack and returns that element. */
public int Pop()
{
return q.Dequeue();
}
/** Get the top element. */
public int Top()
{
return q.First();
}
/** Returns whether the stack is empty. */
public bool Empty()
{
return !q.Any();
}
}
/**
* Your MyStack object will be instantiated and called as such:
* MyStack obj = new MyStack();
* obj.Push(x);
* int param_2 = obj.Pop();
* int param_3 = obj.Top();
* bool param_4 = obj.Empty();
*/