本文整理汇总了C#中Stack类的典型用法代码示例。如果您正苦于以下问题:C# Stack类的具体用法?C# Stack怎么用?C# Stack使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
Stack类属于命名空间,在下文中一共展示了Stack类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetValue
public IItem GetValue(IDictionary<string, IItem> variables) {
var stack = new Stack<object>();
int i = 0;
try {
for (; i < tokens.Count; i++) {
var token = tokens[i];
double d;
if (TryParse(token, out d)) {
stack.Push(d);
} else if (token.StartsWith("\"")) {
stack.Push(GetVariableValue(variables, token.Substring(1, token.Length - 2).Replace("\\\"", "\"")));
} else if (token.StartsWith("'")) {
stack.Push(token.Substring(1, token.Length - 2).Replace("\\'", "'"));
} else {
Apply(token, stack, variables);
}
}
} catch (Exception x) {
throw new Exception(string.Format(
"Calculation of '{1}'{0}failed at token #{2}: {3} {0}current stack is: {0}{4}", Environment.NewLine,
Formula, i, TokenWithContext(tokens, i, 3),
string.Join(Environment.NewLine, stack.Select(AsString))),
x);
}
if (stack.Count != 1)
throw new Exception(
string.Format("Invalid final evaluation stack size {0} (should be 1) in formula '{1}'",
stack.Count, Formula));
var result = stack.Pop();
if (result is string) return new StringValue((string)result);
if (result is int) return new IntValue((int)result);
if (result is double) return new DoubleValue((double)result);
if (result is bool) return new BoolValue((bool)result);
return null;
}
示例2: BufferManager
public BufferManager(Int32 totalBytes, Int32 totalBufferBytesInEachSaeaObject)
{
totalBytesInBufferBlock = totalBytes;
this.currentIndex = 0;
this.bufferBytesAllocatedForEachSaea = totalBufferBytesInEachSaeaObject;
this.freeIndexPool = new Stack<int>();
}
示例3: Main
static void Main(string[] args)
{
const int NumberOfAnimals = 10;
Stack<Animal> animalStack = new Stack<Animal>();
Queue<Animal> animalQueue = new Queue<Animal>();
Console.WriteLine("/// ORDER OF ENTRY...");
Random r = new Random();
for (int index = 0; index < NumberOfAnimals; index++)
{
var animalShouldBeCat = (r.Next(2) == 0);
uint nextWeight = (uint)r.Next(10, 40);
Animal nextAnimal = animalShouldBeCat ? (Animal)(new Cat(nextWeight, "John")) : (Animal)(new Dog(nextWeight, "Dave"));
animalStack.Push(nextAnimal);
animalQueue.Enqueue(nextAnimal);
Console.WriteLine(nextAnimal);
}
Console.WriteLine();
Console.WriteLine("/// OUTPUT FROM STACK...");
foreach (Animal animal in animalStack)
{
Console.WriteLine(animal);
}
Console.WriteLine();
Console.WriteLine("/// OUTPUT FROM QUEUE...");
foreach (Animal animal in animalQueue)
{
Console.WriteLine(animal);
}
}
示例4: GetRecursiveDependentsAsync
///<summary>Gets all files that indirectly depend on the specified file.</summary>
public async Task<IEnumerable<string>> GetRecursiveDependentsAsync(string fileName)
{
HashSet<GraphNode> visited;
fileName = Path.GetFullPath(fileName);
using (await rwLock.ReadLockAsync())
{
GraphNode rootNode;
if (!nodes.TryGetValue(fileName, out rootNode))
return Enumerable.Empty<string>();
var stack = new Stack<GraphNode>();
stack.Push(rootNode);
visited = new HashSet<GraphNode> { rootNode };
while (stack.Count > 0)
{
foreach (var child in stack.Pop().Dependents)
{
if (!visited.Add(child)) continue;
stack.Push(child);
}
}
// Don't return the original file.
visited.Remove(rootNode);
}
return visited.Select(n => n.FileName);
}
示例5: Validate
public static bool Validate(string lineOfMarkupLanguage)
{
string[] splitted = lineOfMarkupLanguage.Split(new char[] { '>' }, StringSplitOptions.RemoveEmptyEntries);
Stack<string> mainStack = new Stack<string>();
foreach (var tag in splitted)
{
if (IsOpeniningTag(tag))
{
mainStack.Push(tag);
}
else
{
if (mainStack.Count == 0)
{
return false;
}
string currentOpeningTag = mainStack.Pop();
if (!SpecialComparer(currentOpeningTag, tag))
{
return false;
}
}
}
return (mainStack.Count == 0);
}
示例6: SaveAllConncectedComponents
private static void SaveAllConncectedComponents(Node<int> node,
HashSet<int> visitedNodes, List<string> connectedComponents)
{
string graphs = string.Empty;
Stack<Node<int>> nodesStack = new Stack<Node<int>>();
nodesStack.Push(node);
while (nodesStack.Count > 0)
{
Node<int> currentNode = nodesStack.Pop();
visitedNodes.Add(currentNode.Value);
graphs += " -> " + currentNode.Value;
foreach (var child in currentNode.Children)
{
if (!visitedNodes.Contains(child.Value))
{
visitedNodes.Add(child.Value);
nodesStack.Push(child);
}
}
}
connectedComponents.Add(graphs.Substring(4));
}
示例7: SpinOff
public ExpansionContext SpinOff(MethodBase callee)
{
Stack.Contains(callee).AssertFalse();
var new_stack = new Stack<MethodBase>();
callee.Concat(Stack).Reverse().ForEach(new_stack.Push);
return new ExpansionContext(new_stack, Scope, Names, Env){Parent = this};
}
示例8: makeWhere
private string makeWhere(Uri url)
{
Stack<string> hostStack = new Stack<string>(url.Host.Split('.'));
StringBuilder hostBuilder = new StringBuilder('.' + hostStack.Pop());
string[] pathes = url.Segments;
StringBuilder sb = new StringBuilder();
sb.Append("WHERE (");
bool needOr = false;
while (hostStack.Count != 0) {
if (needOr) {
sb.Append(" OR");
}
if (hostStack.Count != 1) {
hostBuilder.Insert(0, '.' + hostStack.Pop());
sb.AppendFormat(" host = \"{0}\"", hostBuilder.ToString());
} else {
hostBuilder.Insert(0, '%' + hostStack.Pop());
sb.AppendFormat(" host LIKE \"{0}\"", hostBuilder.ToString());
}
needOr = true;
}
sb.Append(')');
return sb.ToString();
}
示例9: StartNavigationLink
protected void StartNavigationLink(Stack<ResponseNode> nodeStack, string linkName)
{
nodeStack.Push(new ResponseNode
{
LinkName = linkName,
});
}
示例10: StartEntry
protected void StartEntry(Stack<ResponseNode> nodeStack)
{
nodeStack.Push(new ResponseNode
{
Entry = new AnnotatedEntry(new Dictionary<string, object>())
});
}
示例11: StartFeed
protected void StartFeed(Stack<ResponseNode> nodeStack, ODataFeedAnnotations feedAnnotations)
{
nodeStack.Push(new ResponseNode
{
Feed = new AnnotatedFeed(new List<AnnotatedEntry>()),
});
}
示例12: GetChunkStack
private static Stack<WeakReference> GetChunkStack(int size)
{
Stack<WeakReference> s;
if (!_chunks.TryGetValue(size, out s))
s = _chunks[size] = new Stack<WeakReference>();
return s;
}
示例13: GetBracketMismatch
//Returns the location of bracket mismatch
//Rewrite this to make use of better suited container for brackets
public int GetBracketMismatch(string code)
{
Dictionary<char, char> bracketMap = new Dictionary<char, char>()
{ {'{', '}'}, {'(', ')'}, {'[', ']'}, {'<', '>'} };//, {'\'', '\''} }; //{} () [] <> "" ''
Stack<char> bracketStack = new Stack<char>();
int counter = 0;
foreach (char c in code)
{
if("(){}<>".Contains(c))
{
if (!")}>".Contains(c))
{
bracketStack.Push(c);
}
else if (bracketMap.Any(q => q.Key == bracketMap[bracketStack.Last()]))
{
bracketStack.Pop();
}
else
{
return counter;
}
}
counter++;
}
return -1;
}
示例14: Print
private static void Print(Stack<int> stack)
{
while (stack.Count > 0)
{
if (stack.Count == 1)
{
Console.Write(stack.Pop());
}
else
{
Console.Write(stack.Pop() + ",");
}
}
Console.WriteLine();
for (int i = 0; i < labyrinth.GetLength(0); i++)
{
for (int j = 0; j < labyrinth.GetLength(1); j++)
{
Console.Write("{0, 2} ", labyrinth[i, j]);
}
Console.WriteLine();
}
Console.WriteLine();
}
示例15: CheckPath
private static void CheckPath(int row, int col, int count, Stack<int> stack)
{
if (row < 0 || col < 0 ||
row >= labyrinth.GetLength(0) || col >= labyrinth.GetLength(1))
{
return;
}
if (labyrinth[row, col] == "e")
{
Console.WriteLine("Exit found at [{0},{1}]", row, col);
Print(new Stack<int>(stack));
}
if (labyrinth[row, col] != " ")
{
return;
}
stack.Push(count);
labyrinth[row, col] = count.ToString();
count++;
CheckPath(row, col - 1, count, stack);
CheckPath(row - 1, col, count, stack);
CheckPath(row, col + 1, count, stack);
CheckPath(row + 1, col, count, stack);
labyrinth[row, col] = " ";
if (stack.Count > 0)
{
stack.Pop();
}
}