本文整理汇总了C#中Stack.Top方法的典型用法代码示例。如果您正苦于以下问题:C# Stack.Top方法的具体用法?C# Stack.Top怎么用?C# Stack.Top使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Stack
的用法示例。
在下文中一共展示了Stack.Top方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: TestStack
public void TestStack()
{
var st = new Stack<int>();
for (int i = 0; i < 10; i++)
{
st.Push(i);
}
Assert.AreEqual(st.Top(), 9);
Assert.AreEqual(st.Pop(), 9);
Assert.AreEqual(st.Top(), 8);
while (!st.IsEmpty())
st.Pop();
try
{
var t = st.Top(); // should raise Exception here
Assert.IsTrue(false);
}
catch (Exception)
{
// should be here
}
}
示例2: Top_OneElementIsPushed_ReturnsTheElement
public void Top_OneElementIsPushed_ReturnsTheElement()
{
var stack = new Stack<string>();
const string element = "element";
stack.Push(element);
var result = stack.Top();
Assert.AreEqual(element, result);
}
示例3: Parse
public ITreeNode Parse(Grammer grammer, IInput input, IGrammerRuleHandler ruleHandler)
{
States states = States.BuildStates(grammer);
input.Parse();
Stack stack = new Stack();
TreeNodeStack treeNodeStack = new TreeNodeStack();
ruleHandler.TreeNodeStack = treeNodeStack;
IAction action = states.GetAction(stack.Top(), input.Get());
while(!action.GetType().Equals(typeof(Accept))){
action.Do(input,stack,ruleHandler);
action = states.GetAction(stack.Top(),input.Get());
}
return treeNodeStack.Pop();
}
示例4: FindNearestSmallerValues
static Tuple<int, int>[] FindNearestSmallerValues(int[] array, bool r2l)
{
Stack<Tuple<int, int>> stack = new Stack<Tuple<int,int>>();
Tuple<int, int>[] result = new Tuple<int, int>[array.Length];
for(int i = 0; i < array.Length; i++)
{
int index = r2l ? array.Length - 1 - i : i;
while(!stack.IsEmpty() && stack.Top().Item1 >= array[index])
stack.Pop();
if (!stack.IsEmpty())
result[index] = stack.Top();
else
result[index] = new Tuple<int, int>(-1, -1);
stack.Push(new Tuple<int, int>(array[index], index));
}
return result;
}
示例5: SortStack
// Keypoint r is always ascending.
// r will need to pop up everything until the one smaller than the one to add.
static Stack<int> SortStack(Stack<int> stack)
{
Stack<int> r = new Stack<int>();
while (!stack.IsEmpty())
{
int t = stack.Pop();
while (!r.IsEmpty() && r.Top() > t)
stack.Push(r.Pop());
r.Push(t);
}
return r;
}