本文整理汇总了C#中Stack.IsEmpty方法的典型用法代码示例。如果您正苦于以下问题:C# Stack.IsEmpty方法的具体用法?C# Stack.IsEmpty怎么用?C# Stack.IsEmpty使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Stack
的用法示例。
在下文中一共展示了Stack.IsEmpty方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Uppgift1B
public Uppgift1B()
{
Console.WriteLine("Enter text to qualify! If you write exit the application will terminate");
while(true)
{
String input = Console.ReadLine();
if(input.ToLower() == "exit") //Parses the input text to lowercase and if exit is entered closes the application
{
break;
}
Stack<char> stack = new Stack<char>(); //Creates the stack and assigns the type char
StringBuilder errorHandling = new StringBuilder(); //Creates an errorhandler that will display the problems
foreach(char c in input)
{
if(openChar.Contains(c)) //Goes through the String and then pushes "openers" onto the stack
{
stack.Push(c);
}
else if(closeChar.Contains(c))
{
if(stack.IsEmpty())
{
errorHandling.AppendLine("\n" + c + " does not have it's corresponding opener!");
continue;
}
char previousElement = (char)stack.Pop();
if(!Qualify(previousElement,c))
{
errorHandling.AppendLine("\n" + previousElement + "and " + c + " does not match!");
}
}
}
if(!stack.IsEmpty()) //If there are any elements on the stack one or more tags are faulty
{
errorHandling.AppendLine(" \n You messed up");
}
if(errorHandling.Length != 0) //Prints out any errors that occurred during runtime
{
Console.WriteLine("The following errors occured: \n" + errorHandling);
}
}
}
示例2: GetValue2
private static void GetValue2(ExpressionOperatorItem item, Stack<ExpressionOperatorItem> operatorStack, List<ExpressionItem> result)
{
if (!operatorStack.IsEmpty())
{
do
{
ExpressionOperatorItem tmpItem = operatorStack.Pop();
if (item.Value.Priority > tmpItem.Value.Priority)
//如果栈顶元素的优先级小于读取到的操作符
{
operatorStack.Push(tmpItem); //将栈顶元素放回堆栈
operatorStack.Push(item); //将读取到的操作符放回堆栈
break;
}
else //如果栈顶元素的优先级比较高或者两者相等时
{
result.Add(tmpItem); //将栈顶元素弹出,放入B中
if (operatorStack.IsEmpty())
{
operatorStack.Push(item); //将读取到的操作符压入堆栈中
break;
}
}
} while (!operatorStack.IsEmpty());
}
else //如果堆栈为空,就把操作符放入堆栈中
{
operatorStack.Push(item);
}
}
示例3: 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
}
}
示例4: InOrderTraverseLoop
static void InOrderTraverseLoop(TreeNode<int> root)
{
Stack<TreeNode<int>> stack = new Stack<TreeNode<int>>();
TreeNode<int> cur = root;
while (cur != null)
{
if (cur.Visited == false && cur.Left != null)
{
stack.Push(cur);
cur = cur.Left;
continue;
}
Console.Write(cur.Value);
Console.Write(" ");
if (cur.Right != null)
{
cur = cur.Right;
continue;
}
if (!stack.IsEmpty())
{
cur = stack.Pop();
cur.Visited = true;
}
else
cur = null;
}
}
示例5: Translate
/// <summary> 将表达式集合翻译为后缀表达式集合
/// </summary>
/// <param name="expressionItems"></param>
/// <returns></returns>
internal static List<ExpressionItem> Translate(IEnumerable<ExpressionItem> expressionItems)
{
List<ExpressionItem> result = new List<ExpressionItem>();
Stack<ExpressionOperatorItem> operatorStack = new Stack<ExpressionOperatorItem>();
foreach (ExpressionItem item in expressionItems)
{
if (!(item is ExpressionOperatorItem))
{
// 如果不是操作符,直接放入结果
result.Add(item);
}
else
{
GetValue((ExpressionOperatorItem)item, operatorStack, result);
}
}
//将堆栈中剩下的操作符输出到B中
while (!operatorStack.IsEmpty())
{
result.Add(operatorStack.Pop());
}
return result;
}
示例6: GetValue
private static void GetValue(ExpressionOperatorItem item, Stack<ExpressionOperatorItem> operatorStack, List<ExpressionItem> result)
{
if (item.Value.ToString() == OperatorConst.LeftBracket)
{
//如果是'(',将它放入堆栈中
operatorStack.Push(item);
}
else if (item.Value.ToString() == OperatorConst.RightBracket) //如果是')'
{
while (!operatorStack.IsEmpty()) //不停地弹出堆栈中的内容,直到遇到'('
{
var tmpItem = operatorStack.Pop();
if (tmpItem.Value.ToString() == OperatorConst.LeftBracket)
{
break;
}
else
{
result.Add(tmpItem); //将堆栈中弹出的内容放入B中
}
}
}
else //既不是'(',也不是')',是其它操作符,比如+, -, *, /之类的
{
GetValue2(item, operatorStack, result);
}
}
示例7: Run
public void Run()
{
const string fieName = "tobe.txt";
var @in = new In($"Files\\Collections\\{fieName}");
var words = @in.ReadAllStrings();
var stack = new Stack<string>();
foreach (var word in words)
{
stack.Push(word);
}
Console.WriteLine("size of stack = {0}", stack.Size());
foreach (var item in stack)
{
Console.WriteLine("item = {0}", item);
}
while (!stack.IsEmpty())
{
var item = stack.Pop();
Console.WriteLine("item = {0}", item);
}
Console.ReadLine();
}
示例8: UppgB
/// <summary>
/// Default constructor starts the exercise
/// </summary>
public UppgB()
{
Console.WriteLine("Starting UppgiftB! ");
// loop while user want to loop. Write exit to exit the loop
Console.WriteLine("Enter text to parse:");
while (true)
{
String inputData = Console.ReadLine();
if (inputData.ToLower() == "exit") break;
Stack<char> myStack = new Stack<char>();
StringBuilder errors = new StringBuilder();
foreach (char c in inputData)
{
if (startBrackets.Contains(c))
{
myStack.Push(c);
}
else if (endBrackets.Contains(c))
{
if (myStack.IsEmpty())
{
errors.AppendLine("\t" + c + " is missing the starttag");
continue;
}
char previousBracket = myStack.Pop();
if (!Match(previousBracket, c))
{
errors.AppendLine("\t" + previousBracket + " doesn't match with " + c);
}
}
}
// If my stack contains an uneven amount
if (myStack.Count != 0)
{
errors.AppendLine("\tBracket is missing");
}
// Print errors if there is any
if (errors.Length != 0)
{
Console.WriteLine("Errors: \n" + errors);
}
Console.WriteLine("Text parsed...");
if (inputData.ToLower() == "clear") Console.Clear();
Console.WriteLine("Enter text to parse: (exit to exit)");
}
Console.WriteLine("Ending UppgiftB!\n");
}
示例9: IsEmpty_NoElementsOnStack_ReturnsTrue
public void IsEmpty_NoElementsOnStack_ReturnsTrue()
{
var stack = new Stack<object>();
var result = stack.IsEmpty();
Assert.IsTrue(result);
}
示例10: IsEmpty_OneStringElementPushedOnStack_ReturnsFalse
public void IsEmpty_OneStringElementPushedOnStack_ReturnsFalse()
{
var stack = new Stack<string>();
stack.Push("myElement");
var result = stack.IsEmpty();
Assert.IsFalse(result);
}
示例11: IsEmpty_OneIntElementPushedOnStack_ReturnsFalse
public void IsEmpty_OneIntElementPushedOnStack_ReturnsFalse()
{
var stack = new Stack<int>();
stack.Push(1);
var result = stack.IsEmpty();
Assert.IsFalse(result);
}
示例12: IsEmpty_OneElementPushedAndThenPoped_ReturnIsTrue
public void IsEmpty_OneElementPushedAndThenPoped_ReturnIsTrue()
{
var stack = new Stack<string>();
const string elementToBePopped = "toBePopped";
stack.Push(elementToBePopped);
stack.Pop();
var result = stack.IsEmpty();
Assert.IsTrue(result);
}
示例13: 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;
}
示例14: ProcessCommand
public void ProcessCommand(Stack<string> stack, Arguments arguements)
{
string name = LongName;
object value;
if (stack.IsEmpty() || stack.Peek().IsCommand())
value = null;
else
value = stack.Pop();
arguements.Add(name, value);
if (Callback != null)
Callback((value??"").ToString());
}
示例15: DoPrimaryDecompilation
private void DoPrimaryDecompilation()
{
var loopholes = new List<Loophole>();
Func<Loophole> emit_loophole = () =>
{
var loophole = new Loophole();
loopholes.Add(loophole);
return loophole;
};
_stack = new Stack<Expression>();
foreach (var op in _cil)
{
_currentOp = op;
_args = op.OpSpec.Pops.Times(_ =>
_stack.PopOrDefault() ?? emit_loophole()
).Reverse().ToReadOnly();
Dispatch(op);
if (_stack.IsEmpty() && _qualified.IsNotEmpty())
{
var fixt = FixupEvaluationOrder(_qualified.ToReadOnly());
fixt.ForEach(q => _block.BalancedCode.Add(q));
_qualified.Clear();
}
}
var pending = _qualified.Concat(_stack.Reverse().Cast<Node>()).ToReadOnly();
if (pending.IsNotEmpty())
{
var p_fixt = FixupEvaluationOrder(pending);
Func<Node, int> stackBalance = n =>
!(n is Expression) ? 0 :
n is Assign ? 0 :
n is Eval ? (n.InvokedMethod().Ret() == typeof(void) ? 0 : 1) :
/* otherwise */ 1;;
var delim = 0.UpTo(p_fixt.Count() - 1).Single(i =>
p_fixt.Take(i).All(n => stackBalance(n) == 0) &&
p_fixt.Skip(i).All(n => stackBalance(n) == 1));
p_fixt.Take(delim).ForEach(p => _block.BalancedCode.Add(p));
p_fixt.Skip(delim).ForEach(p => _block.Residue.Add(p.AssertCast<Expression>()));
_qualified.Clear();
}
loopholes.Reverse();
loopholes.ForEach((l, i) => l.Tag = i);
}