本文整理汇总了C#中System.Collections.Stack.Peek方法的典型用法代码示例。如果您正苦于以下问题:C# Stack.Peek方法的具体用法?C# Stack.Peek怎么用?C# Stack.Peek使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Collections.Stack
的用法示例。
在下文中一共展示了Stack.Peek方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
static void Main(string[] args)
{
Stack ast = new Stack();
ast.Push("Item 1");
ast.Push("Item 2");
ast.Push("Item 3");
ast.Push("Item 4");
Console.WriteLine("Count: {0}", ast.Count);
PrintValues(ast);
// Peek item but do not remove
object item = ast.Peek();
Console.WriteLine("Peek: {0}", item);
PrintValues(ast);
// Peek and cast but do not remove
string itemString = ast.Peek() as string; // fast cast
Console.WriteLine("Peek: {0}", item);
PrintValues(ast);
// Contains
Boolean contains = ast.Contains("Item 3");
Console.WriteLine("Contains: {0}", contains);
// Remove items
object item4 = ast.Pop();
object item3 = ast.Pop();
Console.WriteLine("Pop: {0} {1}", item4, item3);
PrintValues(ast);
Console.WriteLine("Count: {0}", ast.Count);
// no TrimToSize method
}
示例2: Intercambiar
public static bool Intercambiar(ListBox listaA, ListBox listaB, Stack destino, Stack origen)
{
Object item = listaB.SelectedItem;
if (item.ToString() != origen.Peek().ToString())
{
MessageBox.Show("El elemnto no se puede mover", "Torres de Hanoi", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
listaB.ClearSelected();
}
else{
if (destino.Count == 0 || ((int)origen.Peek() < (int)destino.Peek()))
{
listaA.Items.Insert(0, item);
listaB.Items.Remove(item);
destino.Push(origen.Peek());
origen.Pop();
return true;
}
else
{
MessageBox.Show("Movimiento no valido", "Torres de Hanoi", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
listaB.ClearSelected();
}
}
return false;
}
示例3: LongestValidParentheses
public int LongestValidParentheses(string s)
{
Stack<int> stack = new Stack<int>();
char[] arr = s.ToCharArray();
int result = 0;
for (int i = 0; i < arr.Length; i++)
{
if (arr[i] == '(')
{
stack.Push(i);
}
else
{
if (stack.Count != 0 && arr[stack.Peek()] == '(')
{
stack.Pop();
result = Math.Max((stack.Count == 0 ? i + 1 : i - stack.Peek()), result);
}
else
{
stack.Push(i);
}
}
}
return result;
}
示例4: largestRectangleArea2
/**
* Leetcode online judge: int[] as input argument
* 208ms
*/
public static int largestRectangleArea2(int[] height)
{
Stack S = new Stack();
int len = height.Length;
int[] newH = new int[len + 1];
for (int i = 0; i < len; i++)
newH[i] = height[i];
newH[len] = 0;
int sum = 0;
for (int i = 0; i < newH.Length; i++)
{
if (S.Count == 0 || (int)newH[i] > (int)newH[(int)S.Peek()])
S.Push(i);
else
{
int tmp = (int)S.Peek();
S.Pop();
int value_i = (int)newH[tmp];
sum = Math.Max(sum, value_i * ((S.Count == 0) ? i : i - (int)S.Peek() - 1));
i--;
}
}
return sum;
}
示例5: Main
static void Main(string[] args)
{
// Declare stack collection
Stack myStack = new Stack();
// Push elements onto the stack
myStack.Push("item 1");
myStack.Push("item 2");
myStack.Push("item 3");
// Display number of items
Console.WriteLine("{0} Items on the stack", myStack.Count);
// Have a peek at the item on top
Console.WriteLine("{0}", myStack.Peek());
// Pop off an element
myStack.Pop(); // gets top value
// Have a peek at the item on top
Console.WriteLine("{0}", myStack.Peek());
// Clear stack
myStack.Clear();
// Display number of items
Console.WriteLine("{0} Items on the stack", myStack.Count);
Console.ReadLine();
}
示例6: Evaluate
public static mysToken Evaluate(
mysSymbol symbol,
mysToken value,
Stack<mysSymbolSpace> spaceStack
)
{
// NOTICE THIS
// since each function has it's own internal space
// before grabbing our reference to the space in which
// we want to define our symbol, we need to pop the
// internal off, or we're going to be defining the symbol
// in our internal space, i.e. it will scope out as soon as
// we're done. So we pop the internal off, grab our reference
// to the space outside of that, then push the internal back on.
mysSymbolSpace top = spaceStack.Pop();
mysSymbolSpace ss = spaceStack.Peek();
if ( value.Type == typeof(mysFunction) ) {
defineFunction(
symbol,
value.Value as mysFunction,
spaceStack.Peek()
);
} else {
mysSymbolSpace space = symbol.DefinedIn( spaceStack );
if ( space != null ) {
space.Define( symbol, value );
} else {
ss.Define( symbol, value );
}
}
spaceStack.Push( top );
return null;
}
示例7: Main
static void Main(string[] args)
{
// Stack - A LIFO collection
Stack<string> stack = new Stack<string>();
stack.Push("A"); // Push adds to the stack
stack.Push("B");
stack.Push("C");
stack.Push("D");
Console.WriteLine(stack.Peek()); // D - Peek returns the last added value without removing it
Console.WriteLine(stack.Pop()); // D - Pop returna the last added value and removes it
Console.WriteLine(stack.Peek()); // C
// Queue - A FIFO collection
Queue<string> queue = new Queue<string>();
queue.Enqueue("a"); // Enqueue adds to the queue
queue.Enqueue("b");
queue.Enqueue("c");
queue.Enqueue("d");
Console.WriteLine(queue.Peek()); // a - Peek returns the beginning value without removing it
Console.WriteLine(queue.Dequeue()); // a - Dequeue returns the beginning value and removes it
Console.WriteLine(queue.Peek()); // b
//--
Console.ReadKey();
}
示例8: ObjectProcessComponent
internal void ObjectProcessComponent(int index, LineTypes.Component line, IParentObject obj, Stack<GameObject> objectStack, Stack<ParserContext> contextStack, ParserContext context)
{
#region Object context
if (context == this.objectContext) {
switch (index) {
case 0:
case 2:
return;
case 1:
MeshFilter filter = objectStack.Peek().AddComponent<MeshFilter>();
MeshRenderer renderer = objectStack.Peek().AddComponent<MeshRenderer>();
filter.mesh = Utility.MeshHelper.GetQuad();
renderer.material = new Material(Shader.Find("Standard"));
return;
}
}
#endregion
if (context == this.objectContext.Components[2].context) {
// COLLIDER
// todo: this
}
// other components that have sub-components
throw new Exception("Invalid member in parsed lines list");
}
示例9: stackButton_Click
private void stackButton_Click(object sender, EventArgs e)
{
Stack pilha = new Stack();
//Adicionando itens
pilha.Push("banana");
pilha.Push("laranja");
pilha.Push("uva");
//Exibindo os itens da coleção
foreach (string elemento in pilha)
{
listBox1.Items.Add(elemento);
}
listBox1.Items.Add("--------------");
//Exibindo o item do topo da pilha
listBox1.Items.Add("topo da pilha");
listBox1.Items.Add(pilha.Peek());
listBox1.Items.Add("--------------");
//Retirando um elemento da pilha (do topo)
pilha.Pop();
//Exibindo o item do topo da pilha
listBox1.Items.Add("topo da pilha");
listBox1.Items.Add(pilha.Peek());
}
示例10: Parse
public static LexemV8 Parse(string data)
{
if (String.IsNullOrEmpty(data))
{
return null;
}
Stack<LexemV8> _lexemStack = new Stack<LexemV8>();
LexemV8 lexemV8 = null;
for (int i = 0; i < data.Length; i++)
{
char ch = data[i];
if (ch == StartLexem
&& IsValidStringLexem(lexemV8))
{
LexemV8 braceLexemV8 = new LexemV8 { LexemType = LexemV8Type.Brace };
if (_lexemStack.Count > 0)
{
LexemV8 parentBrace = _lexemStack.Peek();
parentBrace.AddChild(braceLexemV8);
}
_lexemStack.Push(braceLexemV8);
}
else if (ch == DelimiterLexem
&& IsValidStringLexem(lexemV8))
{
LexemV8 parentBrace = _lexemStack.Peek();
parentBrace.AddChild(lexemV8);
lexemV8 = null;
}
else if(ch == EndLexem
&& IsValidStringLexem(lexemV8))
{
LexemV8 parentBrace = _lexemStack.Pop();
parentBrace.AddChild(lexemV8);
if (_lexemStack.Count == 0)
{
lexemV8 = parentBrace;
}
else
{
lexemV8 = null;
}
}
else if (NeedAddChar(lexemV8, ch))
{
if (lexemV8 == null)
{
lexemV8 = new LexemV8();
}
lexemV8.AddChar(ch);
}
}
return lexemV8;
}
示例11: ConvertToPostfixNotation
public string[] ConvertToPostfixNotation(string input)
{
List<string> outputSeparated = new List<string>();
Stack<string> stack = new Stack<string>();
foreach (string c in Separate(input))
{
if (operators.Contains(c))
{
if (stack.Count > 0 && !c.Equals("("))
{
if (c.Equals(")"))
{
string s = stack.Pop();
while (s != "(")
{
outputSeparated.Add(s);
s = stack.Pop();
}
}
else if (GetPriority(c) > GetPriority(stack.Peek()))
{
stack.Push(c);
}
else
{
while (stack.Count > 0 && GetPriority(c) <= GetPriority(stack.Peek()))
{
outputSeparated.Add(stack.Pop());
}
stack.Push(c);
}
}
else
{
stack.Push(c);
}
}
else
{
outputSeparated.Add(c);
}
}
if (stack.Count > 0)
{
foreach (string c in stack)
{
outputSeparated.Add(c);
}
}
return outputSeparated.ToArray();
}
示例12: button2_Click
private void button2_Click(object sender, EventArgs e)
{
//ʵ����
Stack myStack = new Stack();
//ʹ��Push������ջ
myStack.Push("A");
myStack.Push("B");
myStack.Push("C");
myStack.Push("C");
//����ջԪ��
Console.WriteLine("\nmyStack:");
foreach (string item in myStack)
{
Console.WriteLine("|_" + item);
}
//ʹ��Pop������ջ
Console.WriteLine("\nItem pulled from myStack: " + myStack.Pop().ToString());
Console.WriteLine("\nAfter myStack.Pop(),myStack:");
foreach (string item in myStack)
{
Console.WriteLine("|_" + item);
}
//Peek����ֻ����ջ��Ԫ�أ�����ջ��ɾ��
Console.WriteLine("\nmyStack.Peek() : " + myStack.Peek().ToString());
Console.WriteLine("\nAfter myStack.Peek(),myStack:");
foreach (string item in myStack)
{
Console.WriteLine("|_" + item);
}
}
示例13: Main_7_9_6
//Main_7_9_6
public static void Main_7_9_6()
{
Queue myQueue = new Queue();
//��Queueβ�����Ԫ��
myQueue.Enqueue("��");
myQueue.Enqueue("��");
myQueue.Enqueue("˭");
//����Queueu��ʼ��Ԫ��
Console.WriteLine(myQueue.Peek());
//���ز��Ƴ�Queue��ʼ��Ԫ��
myQueue.Dequeue();
//����Queue
foreach (object o in myQueue)
Console.WriteLine(o.ToString());
Stack myStack = new Stack();
//��Stack��������Ԫ��
myStack.Push("��");
myStack.Push("��");
myStack.Push("˭");
//����Stack������Ԫ��
Console.WriteLine(myStack.Peek());
//���ز��Ƴ�Stack������Ԫ��
myStack.Pop();
//����Stack
foreach (object o in myStack)
Console.WriteLine(o.ToString());
}
示例14: getLine
/// <summary>
/// Returns a line composed of the words in the stack that would
/// fit into the label.
/// The line is returned when the next word wouldn't fit or the last poped
/// word was a newline.
///
/// The stack is requiered to have all characters and all whitespaces in
/// separate elements. The labels text must be blank.
/// </summary>
public static string getLine( Stack<string> words, UILabel targetLabel, bool doLinePadding ){
string line = "";
string currWord = "";
Vector2 labelSize = new Vector2( targetLabel.width, targetLabel.height );
Vector2 textSize = new Vector2();
targetLabel.UpdateNGUIText();
//Add next word to the current line as long as the line would fit in the label
//and not cause a newline.
while( words.Count > 0 ){
currWord = words.Peek();
textSize = NGUIText.CalculatePrintedSize(line + currWord);
if( textSize.y > labelSize.y ){
//Check if the current word is a whitespace. If it is, remove it
if( currWord.Trim() == string.Empty ){
words.Pop();
line.Trim();
}
textSize = NGUIText.CalculatePrintedSize(line + " ");
while( textSize.y < labelSize.y && doLinePadding ){
line += " ";
textSize = NGUIText.CalculatePrintedSize(line + " ");
}
return line;
}
line += words.Pop();
}
return line;
}
示例15: Main
static void Main(string[] args)
{
string valor;
Stack mipila = new Stack();
//ingreso de elementos a la pila
mipila.Push("z");
mipila.Push("ba");
mipila.Push("nom");
//imprimir elementos de la pila pueden ser de tipo char, int, string o
//cualquier otro tipo ya que es un tipo object dependera de lo que el
//usuario necesite
foreach (string var in mipila)
{
Console.WriteLine(var);
}
Console.WriteLine("\n\n");
//Peek RETORNA el valor que esta al tope de la pila sin eliminarlo
Console.WriteLine("El tope de la pila es");
Console.WriteLine(mipila.Peek());
//retorna el valor del tope eliminandolo
valor = mipila.Pop().ToString();
Console.WriteLine("eliminado de la pila: " + valor);
Console.WriteLine("\n\n");
//mostrando contenido de la pila
foreach (string var in mipila)
{
Console.WriteLine(var);
}
Console.ReadLine();
}