本文整理汇总了C#中System.Collections.Stack类的典型用法代码示例。如果您正苦于以下问题:C# Stack类的具体用法?C# Stack怎么用?C# Stack使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Stack类属于System.Collections命名空间,在下文中一共展示了Stack类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
private static void Main()
{
string inputSentence = SampleSentence;
string splitPattern = @"(\s+|\s*\,\s*|\s*\-\s*|\s*\!|\s*\.)";
string[] elements = Regex.Split(inputSentence, splitPattern);
Stack words = new Stack();
Queue separators = new Queue();
StringBuilder result = new StringBuilder();
foreach (var element in elements)
{
if (Regex.IsMatch(element, splitPattern))
{
separators.Enqueue(element);
}
else if (Regex.IsMatch(element, @"\S"))
{
words.Push(element);
}
}
while (words.Count > 0)
{
result.Append(words.Pop());
result.Append(separators.Dequeue());
}
Console.WriteLine(result);
}
示例2: ReversePolishNotationEvaluator
public ReversePolishNotationEvaluator()
{
output = new Queue();
ops = new Stack();
postfixExpression = string.Empty;
}
示例3: InputMapper
public InputMapper(Dictionary<string, InputContext> contexts, int playerIndex)
{
_contexts = contexts;
_activeContexts = new Stack<InputContext>();
_callbacks = new List<Action<MappedInput>>();
_currentFrameMappedInput = new MappedInput(playerIndex);
}
示例4: ReadPassword
public static string ReadPassword()
{
var passbits = new Stack<string>();
//keep reading
for (ConsoleKeyInfo cki = Console.ReadKey(true); cki.Key != ConsoleKey.Enter; cki = Console.ReadKey(true))
{
if (cki.Key == ConsoleKey.Backspace)
{
//rollback the cursor and write a space so it looks backspaced to the user
Console.SetCursorPosition(Console.CursorLeft - 1, Console.CursorTop);
Console.Write(" ");
Console.SetCursorPosition(Console.CursorLeft - 1, Console.CursorTop);
passbits.Pop();
}
else
{
Console.Write("*");
passbits.Push(cki.KeyChar.ToString());
}
}
string[] pass = passbits.ToArray();
Array.Reverse(pass);
Console.Write(Environment.NewLine);
return string.Join(string.Empty, pass);
}
示例5: 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;
}
示例6: Map
public IEnumerable<IPath> Map(object data)
{
JToken jToken = JToken.Parse(data.ToString());
var propertyStack = new Stack<Tuple<JProperty, bool>>();
return BuildPaths(jToken, propertyStack, jToken);
}
示例7: RenderXml
TextWriter tw; // where the output is going
#endregion Fields
#region Constructors
public RenderXml(Report rep, IStreamGen sg)
{
r = rep;
tw = sg.GetTextWriter();
stkReportItem = new Stack();
stkContainers = new Stack();
}
示例8: InvokeEstablish
void InvokeEstablish()
{
var types = new Stack<Type>();
var type = GetType();
do
{
types.Push(type);
#if NET_STANDARD
type = type.GetTypeInfo().BaseType;
#else
type = type.BaseType;
#endif
} while (type != typeof (ContextSpecification));
foreach (var t in types)
{
var fieldInfos = t.GetFields(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy);
FieldInfo establishFieldInfo = null;
foreach (var info in fieldInfos)
{
if (info.FieldType.Name.Equals("Establish"))
establishFieldInfo = info;
}
Delegate establish = null;
if (establishFieldInfo != null) establish = establishFieldInfo.GetValue(this) as Delegate;
if (establish != null) Exception = Catch.Exception(() => establish.DynamicInvoke(null));
}
}
示例9: ResolveTemplate
private string ResolveTemplate (string tag, Stack<string> resolvestack)
{
string template = FindTemplate(tag);
if (template == null) return Cache(tag, tag.Trim());
string translation = ExpandTemplate(template, tag, resolvestack).Trim();
return Cache(tag, translation);
}
示例10: depthFirstSearch
/// <summary>
/// returns true if path exists from startVertex to endVertex
/// </summary>
private bool depthFirstSearch(object startVertex, object endVertex)
{
var stack = new Stack();
var vertextQueue = new Queue();
bool found = false;
graph.clearMarks();
stack.Push(startVertex);
do
{
object vertex = stack.Pop();
if (vertex == endVertex) // general case when path is found
found = true;
else
{
if (!graph.isMarked(vertex)) // if not marked
{
graph.markVertex(vertex); // then mark that vertex
vertextQueue = graph.getToVertices(vertex); //get all adjecent vertexes
while (vertextQueue.Count > 0) // then for each of those adjecent vertexes
{
object item = vertextQueue.Dequeue();
if (!graph.isMarked(item))
stack.Push(item);
}
}
}
} while (stack.Count > 0 && !found);
return found;
}
示例11: MarkNodes
public Dictionary<HClusterNode,System.Drawing.Color> MarkNodes(List<string> toMark,System.Drawing.Color color)
{
Dictionary<HClusterNode, System.Drawing.Color> returnList = new Dictionary<HClusterNode, System.Drawing.Color>();
Stack<HClusterNode> st = new Stack<HClusterNode>();
HClusterNode current = null;
st.Push(this);
while (st.Count != 0)
{
current = st.Pop();
if (current.joined == null || current.joined.Count == 0)
{
foreach(var item in toMark)
if(current.setStruct.Contains(item))
{
returnList.Add(current,color);
break;
}
}
else
if (current.joined != null)
foreach (var item in current.joined)
st.Push(item);
}
return returnList;
}
示例12: TagListViewProcessor
public TagListViewProcessor(ListView listview)
{
_TagTable = new Hashtable(1023);
_Tags = new Stack<string>();
_Item_LV = listview;
_Item_LV.ItemCheck += new System.Windows.Forms.ItemCheckEventHandler(this._Item_LV_ItemCheck);
}
示例13: LevelOrderBottom
public IList<IList<int>> LevelOrderBottom(TreeNode root)
{
IList<IList<int>> result = new List<IList<int>>();
if (root == null)
return result;
Stack<IList<int>> stack = new Stack<IList<int>>();
Queue<TreeNode> queue = new Queue<TreeNode>();
queue.Enqueue(root);
while (queue.Count > 0)
{
IList<int> items = new List<int>();
int num = queue.Count;
for (int i = 0; i < num; i++)
{
TreeNode node = queue.Dequeue();
items.Add(node.val);
if (node.left != null)
queue.Enqueue(node.left);
if (node.right != null)
queue.Enqueue(node.right);
}
stack.Push(items);
}
while (stack.Count > 0)
result.Add(stack.Pop());
return result;
}
示例14: ZnajdzOtoczke
public void ZnajdzOtoczke(List<Punkt> lista)
{
List<Punkt> listaTmp = new List<Punkt>();
Stack stos = new Stack();
stos.Push(lista[0]);
stos.Push(lista[1]);
stos.Push(lista[2]);
listaTmp.Add(lista[0]);
listaTmp.Add(lista[1]);
listaTmp.Add(lista[2]);
for (int i = 3; i < lista.Count; i++)
{
int j = i;
while (ObliczDet(listaTmp[stos.Count - 2], listaTmp[stos.Count - 1], lista[i]) < 0)
{
//Console.WriteLine(ObliczDet(lista[j - 2], lista[j - 1], lista[i]));
stos.Pop();
listaTmp.RemoveRange(stos.Count, 1);
}
stos.Push(lista[i]);
listaTmp.Add(lista[i]);
}
int ileWierz = stos.Count;
for (int i = 0; i < ileWierz; i++)
{
wierzcholki.Add((Punkt)stos.Pop());
}
wierzcholki.Reverse();
}
示例15: TrimString
//Taken from http://stackoverflow.com/a/18884516 by pkuderov
private static string TrimString(StringBuilder builder)
{
string initialString = builder.ToString();
char[] s = new char[initialString.Length];
char toRemove = '$';
Stack<int> stack = new Stack<int>();
for (int i = 0; i < s.Length; i++)
{
s[i] = initialString[i];
if (s[i] == '(')
stack.Push(i);
else if (s[i] == ')')
{
int start = stack.Pop();
var endParanthesis = start == 0 && i == s.Length - 1;
var middleParanthesis = start != 0 && s[start - 1] == '(' && s[i + 1] == ')';
if (endParanthesis || middleParanthesis)
{
s[start] = s[i] = toRemove;
}
}
}
var newString = new string((from c in s where c != toRemove select c).ToArray());
return newString.Trim();
}