本文整理汇总了C#中Stack.peek方法的典型用法代码示例。如果您正苦于以下问题:C# Stack.peek方法的具体用法?C# Stack.peek怎么用?C# Stack.peek使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Stack
的用法示例。
在下文中一共展示了Stack.peek方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: evaluatepostfixexp
public void evaluatepostfixexp(string s)
{
Stack stack=new Stack(s.Length);
char[] carr=s.ToCharArray();
foreach(char c in carr)
{
if(isDigit(c))
{
stack.push(c-'0');
Console.WriteLine(stack.peek());
}
else
{
int a =stack.pop();
int b=stack.pop();
switch(c)
{
case '+':stack.push(a+b);break;
case '-':stack.push(a-b);break;
case '*':stack.push(a*b);break;
case '/':stack.push(a/b);break;
}
}
}
Console.WriteLine(stack.peek());
}
示例2: SortStack
public static void SortStack(Stack s)
{
Stack helper = new Stack();
helper.push(s.pop());
while (true)
{
if (helper.peek() < s.peek())
{
helper.push(s.pop());
}
if (s.getSize() == 0)
{
break;
}
double loop = s.pop();
while (loop < helper.peek())
{
s.push(helper.pop());
}
helper.push(loop);
}
while (helper.getSize() != 0)
{
s.push(helper.pop());
}
}
示例3: SortStackV5
public static Stack SortStackV5(Stack s)
{
Stack n = new Stack();
while (s.getSize() != 0)
{
double tmp = s.pop();
while (n.getSize()!=0&&n.peek()<tmp)
{
s.push(n.pop());
}
n.push(tmp);
}
return n;
}
示例4: Sort
//O(n^2) time, O(n) space
static Stack<int> Sort(Stack<int> stack)
{
Stack<int> stack2 = new Stack<int>();
while (!stack.isEmpty()) {
int temp = stack.pop();
while ((!stack2.isEmpty()) && (temp < stack2.peek()))
{
stack.push(stack2.pop());
}
stack2.push(temp);
}
while (!stack2.isEmpty())
stack.push(stack2.pop());
return stack;
}
示例5: Main
static void Main()
{
// Console.WriteLine("Enter string");
// string s=Console.ReadLine();
// Stack s1=new Stack(s.Length);
// s=s1.reverse(s);
// Console.WriteLine(s);
Stack s=new Stack(5);
s.push('a');
s.push('b');
s.push('c');
s.push('d');
s.push('e');
Console.WriteLine(s.peek());
s.stackReverse();
Console.WriteLine(s.pop());
Console.WriteLine(s.pop());
Console.WriteLine(s.pop());
Console.WriteLine(s.pop());
}