边看边写(判断一个序列是否为出栈序列)

发布时间:2016-12-10 16:49:20 编辑:www.fx114.net 分享查询网我要评论
本篇文章主要介绍了"边看边写(判断一个序列是否为出栈序列)",主要涉及到边看边写(判断一个序列是否为出栈序列)方面的内容,对于边看边写(判断一个序列是否为出栈序列)感兴趣的同学可以参考一下。

#region 栈操作 /// <summary> /// ISPopOrder()判断一个序列是否为出栈序列 /// </summary> /// <typeparam name="T"></typeparam> class MyStack<T> where T : IComparable<T> { private Stack<T> _stack; public Stack<T> stack { get { return _stack; } set { _stack = value; } } public MyStack(List<T> list) { if (stack == null) stack = new Stack<T>(); foreach (T item in list) { stack.Push(item); } } public MyStack() { if (stack == null) stack = new Stack<T>(); } public void Push(T item) { stack.Push(item); } public T Pop() { if (stack.Count > 0) return stack.Pop(); return default(T); } public bool ISPopOrder(List<T> pop) { bool bPossible = false; List<T> push = new List<T>(); while (stack.Count > 0) push.Add(stack.Pop()); push.Reverse(); push.ForEach(q => stack.Push(q)); if (push.Count != 0 && pop.Count != 0) { Stack<T> stackData = new Stack<T>(); while (pop.Count > 0) { while (stackData.Count == 0 || stackData.Peek().CompareTo(pop.First()) != 0) { if (push.Count == 0) break; stackData.Push(push.First()); push.RemoveAt(0); } if (stackData.Peek().CompareTo(pop.First()) != 0) break; stackData.Pop(); pop.RemoveAt(0); } if (stackData.Count == 0 && pop.Count == 0) bPossible = true; } return bPossible; } } #endregion

上一篇:poj 3624 最基本01背包问题
下一篇:

相关文章

相关评论