本文整理汇总了C#中Queue.ElementAt方法的典型用法代码示例。如果您正苦于以下问题:C# Queue.ElementAt方法的具体用法?C# Queue.ElementAt怎么用?C# Queue.ElementAt使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Queue
的用法示例。
在下文中一共展示了Queue.ElementAt方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: BFSTraversal
static void BFSTraversal(FakeGraph fg, string kotaasal)
{
ParentNode v = fg.pn[fg.findindex(kotaasal)];
List<ParentNode> discovered = new List<ParentNode>();
Queue<ParentNode> antrian = new Queue<ParentNode>();
antrian.Enqueue(v);
discovered.Add(v);
while (!antrian.Count.Equals(0))
{
v = antrian.Dequeue();
Queue<ParentNode> tetanggaku = new Queue<ParentNode>();
tetanggaku = fg.tetangganyadarikota(v);
for (int i = 0; i < tetanggaku.Count(); i++)
{
if(!sudahdiscoverkah(discovered,tetanggaku.ElementAt(i)))
{
antrian.Enqueue(tetanggaku.ElementAt(i));
discovered.Add(tetanggaku.ElementAt(i));
}
}
}
Console.WriteLine();
for (int i = 0; i < discovered.Count(); i++)
{
Console.WriteLine(discovered.ElementAt(i).nama);
}
/*
foreach (ParentNode p in discovered)
{
Console.WriteLine(p.nama);
}
*/
}
示例2: Main
static void Main(string[] args)
{
int n = 5;
int m = 16;
Queue<int> path = new Queue<int>();
path.Enqueue(m);
Console.Write("Shortest path between {0} and {1} is: ", n, m);
while (m / 2 >= n)
{
path.Enqueue(m / 2);
m = m / 2;
}
while (m - 2 >= n)
{
path.Enqueue(m - 2);
m = m - 2;
}
while (m - 1 >= n)
{
path.Enqueue(m - 1);
m = m - 1;
}
for (int i = path.Count - 1; i >= 0; i--)
{
if (i > 0)
Console.Write(path.ElementAt(i) + " -> ");
else
Console.Write(path.ElementAt(i) + "\n\n");
}
}
示例3: BFS
static void BFS(FakeGraph fg, string kotaasal, string kotadicari)
{
ParentNode v = fg.pn[fg.findindex(kotaasal)];
List<ParentNode> discovered = new List<ParentNode>();
Queue<ParentNode> antrian = new Queue<ParentNode>();
antrian.Enqueue(v);
discovered.Add(v);
while (!antrian.Count.Equals(0))
{
v = antrian.Dequeue();
Queue<ParentNode> tetanggaku = new Queue<ParentNode>();
tetanggaku = fg.tetangganyadarikota(v);
for (int i = 0; i < tetanggaku.Count(); i++)
{
if (!sudahdiscoverkah(discovered, tetanggaku.ElementAt(i)))
{
antrian.Enqueue(tetanggaku.ElementAt(i));
discovered.Add(tetanggaku.ElementAt(i));
if (tetanggaku.ElementAt(i).Equals(fg.pn[fg.findindex(kotadicari)]))
{
Console.WriteLine("Kota Ditemukan!");
Console.WriteLine("Jalur Penelusuran :");
for (int y = 0; y < discovered.Count(); y++)
{
Console.WriteLine(discovered.ElementAt(y).nama);
}
break;
}
}
}
}
}
示例4: Main
static void Main()
{
Console.Write("Enter first element of the sequence S1: ");
string line = Console.ReadLine();
int S1;
while (!int.TryParse(line, out S1))
{
Console.Write("Please enter an integer: ");
line = Console.ReadLine();
}
Console.WriteLine();
Console.Write("Enter number of the first sequence elements to be printed: ");
line = Console.ReadLine();
int N;
while (!(int.TryParse(line, out N) && (N > 0)))
{
Console.Write("Please enter a positive integer: ");
line = Console.ReadLine();
}
Console.WriteLine();
Queue<int> sequence = new Queue<int>(N);
sequence.Enqueue(S1);
int index = 0;
int currentElement = sequence.ElementAt(index);
int counter = 1;
while (counter < N)
{
sequence.Enqueue(currentElement + 1);
sequence.Enqueue(2 * currentElement + 1);
sequence.Enqueue(currentElement + 2);
index++;
currentElement = sequence.ElementAt(index);
counter += 3;
}
Console.WriteLine("First {0} members of the sequence are: ", N);
Console.WriteLine(String.Join(" ", sequence.Take(N)));
Console.WriteLine();
}
示例5: IndexOfAfterCRLFCRLF
internal static int IndexOfAfterCRLFCRLF(this IEnumerable<ArraySegment<byte>> buffers)
{
Queue<byte> lastFour = new Queue<byte>(4);
int i = 0;
foreach (var b in buffers.GetBytes())
{
if (lastFour.Count == 4)
lastFour.Dequeue();
lastFour.Enqueue(b);
if (lastFour.ElementAt(0) == 0x0d && lastFour.ElementAt(1) == 0x0a &&
lastFour.ElementAt(2) == 0x0d && lastFour.ElementAt(3) == 0x0a)
return i + 1;
i++;
}
return -1;
}
示例6: Main
static void Main()
{
Console.Write("n: ");
var n = int.Parse(Console.ReadLine());
Queue<int> sequence = new Queue<int>();
sequence.Enqueue(n);
int currentElement = 0;
for (int i = 0; i < 16; i++)
{
sequence.Enqueue(sequence.ElementAt(currentElement) + 1);
sequence.Enqueue(sequence.ElementAt(currentElement) * 2 + 1);
sequence.Enqueue(sequence.ElementAt(currentElement) + 2);
currentElement += 1;
}
Console.WriteLine(string.Join(", ", sequence));
}
示例7: Main
static void Main(string[] args)
{
Queue<int> sequence = new Queue<int>();
int n = 2;
sequence.Enqueue(n);
for (int i = 0; i < 50/3 - 1; i++)
{
int sValue = sequence.ElementAt(i);
sequence.Enqueue(sValue + 1);
sequence.Enqueue(2 * sValue + 1);
sequence.Enqueue(sValue + 2);
}
foreach (var item in sequence)
{
Console.Write("{0} ", item);
}
}
示例8: QueueExample
/// <summary>
/// Ilustrate queue
/// </summary>
private static void QueueExample()
{
//create new queue FIRST IN LAST OUT
var myQueue = new Queue<int>();
//add items in the queue
myQueue.Enqueue(1);
myQueue.Enqueue(2);
myQueue.Enqueue(3);
myQueue.Enqueue(4);
myQueue.Enqueue(5);
//print the queue
for (int i = 0; i < myQueue.Count; i++)
Console.WriteLine(myQueue.ElementAt<int>(i));
int tmpVal = myQueue.Dequeue();
Console.WriteLine("Dequeue {0}", tmpVal);
}
示例9: Main
public static void Main()
{
try
{
string input = Console.ReadLine();
if (string.IsNullOrEmpty(input))
{
throw new NullReferenceException("The input cannot be empty.");
}
int n = int.Parse(input);
Queue<int> numbers = new Queue<int>();
numbers.Enqueue(n);
int index = 0;
while (numbers.Count < 50)
{
int current = numbers.ElementAt(index);
index++;
numbers.Enqueue(current + 1);
numbers.Enqueue((2 * current) + 1);
numbers.Enqueue(current + 2);
}
Console.WriteLine(string.Join(" ", numbers));
}
catch (NullReferenceException nre)
{
Console.WriteLine(nre.Message);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
示例10: GenerateGroups
public static List<ClientGroup> GenerateGroups(ref Queue<ClientPair> queue, int currentInterval)
{
var clientGroups = new List<ClientGroup>();
var clientGroup = new ClientGroup();
while (queue.Count >= GroupSize)
{
//
// For now, do a simple FIFO algorithm to generate the groups.
//
var clientPair = queue.Dequeue();
clientGroup.client1 = clientPair.client;
clientPair = queue.Dequeue();
clientGroup.client2 = clientPair.client;
/* ToDo: Re-enable this if Group Size goes back up to 4.
clientPair = queue.Dequeue();
clientGroup.client3 = clientPair.client;
clientPair = queue.Dequeue();
clientGroup.client4 = clientPair.client;
*/
clientGroups.Add(clientGroup);
}
//
// If there are any remaining clients, check to see if they have gone through a full
// cycle of intervals, which means that they should be dequeued.
//
if (queue.Count == 0)
{
return clientGroups;
}
//
// If a client has been in the queue for the maximum time, dequeue them into a
// new clientGroup which only contains them.
//
while (queue.ElementAt(0).IntervalsInQueue == currentInterval)
{
var cp = queue.Dequeue();
clientGroup = new ClientGroup();
clientGroup.client1 = cp.client;
clientGroups.Add(clientGroup);
if (queue.Count == 0)
{
break;
}
}
return clientGroups;
}
示例11: IsStable
/// <summary>
/// Check if a queue has stable elements in the end part
/// </summary>
/// <param name="queue">The queue to be checked</param>
/// <param name="tolerance">Max difference allowed</param>
/// <param name="count">Count of elements in the end part</param>
/// <returns>If the tolerance of the end part is less than or equal to tolerance</returns>
private bool IsStable(Queue<Joint> queue, float tolerance, int count)
{
int qCount = queue.Count;
if (qCount < count)
{
// queue is not stable
return false;
}
SkeletonPoint lastPoint = queue.ElementAt(qCount - 1).Position;
for (int i = 0; i < count; ++i)
{
// point from end to front
if (Distance(queue.ElementAt(qCount - 1 - i).Position,
lastPoint) > tolerance)
{
return false;
}
}
return true;
}
示例12: ParseCommand
void ParseCommand(String command)
{
Hashtable predArgs;
String predString = null;
Queue<String> argsStrings = null;
if (r.IsMatch (command)) { // if command matches predicate form
//Debug.Log ("ParseCommand: " + command);
Triple<String,String,String> triple = Helper.MakeRDFTriples(command);
if (triple.Item1 != "" && triple.Item2 != "" && triple.Item3 != "") {
preds.rdfTriples.Add(triple);
Helper.PrintRDFTriples(preds.rdfTriples);
}
else {
Debug.Log ("Failed to make RDF triple");
}
predArgs = Helper.ParsePredicate(command);
foreach (DictionaryEntry entry in predArgs) {
predString = (String)entry.Key;
argsStrings = new Queue<String>(((String)entry.Value).Split (new char[] {','}));
StringBuilder sb = new StringBuilder("[");
foreach(String arg in argsStrings) {
sb.Append (arg + ",");
}
sb.Remove(sb.Length-1,1);
sb.Append("]");
String argsList = sb.ToString();
//Debug.Log(predString + " : " + argsList);
for(int i = 0; i < argsStrings.Count; i++) {
if (r.IsMatch (argsStrings.ElementAt (i))) {
String v = argVarPrefix+argVarIndex.ToString();
skolems[v] = argsStrings.ElementAt (i);
//Debug.Log (v + " : " + skolems[v]);
argVarIndex++;
sb = new StringBuilder(sb.ToString());
foreach(DictionaryEntry kv in skolems) {
argsList = argsList.Replace((String)kv.Value, (String)kv.Key);
//Debug.Log(predString + " : " + argsList);
//foreach(KeyValuePair<String,String> kkv in skolems) {
// if (kkv.Key != kv.Key) {
// skolems[kkv.Key] = kkv.Value.Replace(kv.Value, kv.Key);
// }
//}
//move(mug,to(center(square_table)))
//move(mug,ARG0)
//move(mug,to(ARG1))
}
//Debug.Log(predString + " : " + argsList);
}
ParseCommand (argsStrings.ElementAt (i));
}
}
}
//if (r.IsMatch(arguments)) Debug.Log (arguments);
//Debug.Log (predicate + ' ' + arguments);
/*GameObject bc = GameObject.Find ("BehaviorController");
List<GameObject> objs = new List<GameObject>();
while (argsStrings.Count > 0) {
objs.Add(GameObject.Find(argsStrings.Dequeue()));
}
Predicates preds = bc.GetComponent<Predicates> ();
MethodInfo method = preds.GetType().GetMethod(predString);
object obj = method.Invoke(preds, new object[]{objs.ToArray<GameObject>()});
Debug.Log (obj);*/
}
示例13: ToRPN
//ToRPN converts the tokens into an RPN queue (using the Shunting Yard Algorithm)
private static Queue<object> ToRPN(Queue<object> str)
{
Stack<object> Stack = new Stack<object>();
Queue<object> Queue = new Queue<object>();
for (int TokenCounter = 0; TokenCounter < str.Count; TokenCounter++)
{
//if the token is a number, add it to the output queue.
#region Numbers
if (str.ElementAt(TokenCounter) is double) Queue.Enqueue(str.ElementAt(TokenCounter));
#endregion
//if the token is a left parentheses, push it onto the stack
#region Left Paren
else if (str.ElementAt(TokenCounter).Equals(Symbols.LEFT_PAREN))
{
//if it's something like "15(5 + 2)" the 15 should be multiplied to make the expression "15 * (5 + 2)"
//fix this later, but the only way I can think of doing this is going back, appending a '*', and continuing
if (TokenCounter >= 1 && (str.ElementAt(TokenCounter - 1) is double || str.ElementAt(TokenCounter - 1).Equals(Symbols.RIGHT_PAREN)))
{
List<object> list = new List<object>(str);
list.Insert(TokenCounter, Symbols.MUL);
str = new Queue<object>(list);
TokenCounter--;
}
else Stack.Push(Symbols.LEFT_PAREN);
}
#endregion
#region Right Paren
else if (str.ElementAt(TokenCounter).Equals(Symbols.RIGHT_PAREN))
{
bool IsParenMatched = false;
//Until the token at the top of the stack is a left parentheses, pop operators off the stack onto the output queue.
while (Stack.Count > 0 && !IsParenMatched)
{
//Pop the left parentheses from the stack, but not onto the output queue.
if (Stack.Peek().Equals(Symbols.LEFT_PAREN))
{
Stack.Pop();
IsParenMatched = true;
}
else Queue.Enqueue(Stack.Pop());
}
//If the stack runs out without finding a left parentheses, then there are mismatched parentheses.
if (!IsParenMatched) throw new Exception("Parentheses mismatch");
}
#endregion
#region characters
else
{
object current = str.ElementAt(TokenCounter);
//Use the shunting yard algorithm to turn this into an RPN queue
/* HIGHEST PRECEDENCE */
if (current.Equals(Symbols.MOD) || current.Equals(Symbols.MUL) || current.Equals(Symbols.DIV))
{
while (Stack.Count > 0 &&
!Stack.Peek().Equals(Symbols.LEFT_PAREN) &&
!(Stack.Peek().Equals(Symbols.ADD) || Stack.Peek().Equals(Symbols.SUB)))
{
Queue.Enqueue(Stack.Pop());
}
}
/* LOWEST PRECEDENCE */
if (current.Equals(Symbols.ADD) || current.Equals(Symbols.SUB))
{
while (Stack.Count > 0 &&
!Stack.Peek().Equals(Symbols.LEFT_PAREN))
{
Queue.Enqueue(Stack.Pop());
}
}
//push the current operator onto the stack
Stack.Push(current);
}
#endregion
}
//When there are no more tokens to read:
//While there are still operator tokens in the stack:
// If the operator token on the top of the stack is a parentheses, then there are mismatched parentheses.
// Pop the operator onto the output queue.
while (Stack.Count > 0)
{
if (Stack.Peek().Equals(Symbols.LEFT_PAREN)) throw new Exception("Parentheses mismatch");
Queue.Enqueue(Stack.Pop());
}
return Queue;
}
示例14: Parser
//.........这里部分代码省略.........
case Symbols.MUL:
Queue.Enqueue(Symbols.MUL);
continue;
case Symbols.EXP:
Queue.Enqueue(Symbols.EXP);
continue;
case Symbols.DIV:
Queue.Enqueue(Symbols.DIV);
continue;
case Symbols.MOD:
Queue.Enqueue(Symbols.MOD);
continue;
case Symbols.ADD:
Queue.Enqueue(Symbols.ADD);
continue;
case Symbols.SUB:
Queue.Enqueue(Symbols.SUB);
continue;
}
if (Char.IsWhiteSpace(Stringbuilder[TokenCounter])) continue;
throw new Exception("Unknown symbol: '" + Stringbuilder[TokenCounter] + "'");
}
#endregion
}
//fix negatives, if any
for (int i = 1; i < Queue.Count; i++)
{
//two tokens in a row and the second token being '-' means it a negative number
bool negate = false;
//if we're at the beginning
if (i == 1)
{
if (Queue.ElementAt(i - 1).Equals(Symbols.SUB)) negate = true;
}
//otherwise
else
{
if (Queue.ElementAt(i - 1).Equals(Symbols.SUB) && !(Queue.ElementAt(i - 2) is double)) negate = true;
}
if (negate)
{
if (Queue.ElementAt(i) is double)
{
//negate the double
List<object> list = new List<object>(Queue);
list[i] = -(double)list[i];
list.RemoveAt(i - 1);
Queue = new Queue<object>(list);
}
else
{
//place parentheses around expression and multiply by -1
int iterator;
int paren_count = 0;
for (iterator = i; iterator < Queue.Count && paren_count >= 0; iterator++)
{
if (Queue.ElementAt(i).Equals(Symbols.LEFT_PAREN)) paren_count++;
else if (Queue.ElementAt(i).Equals(Symbols.RIGHT_PAREN)) paren_count--;
}
List<object> list = new List<object>(Queue);
//insert (-1 * ( ... ) ) to avoid any ambiguity
list.RemoveAt(i-1); //remove '-'
示例15: validGesture
int validGesture(Queue<SkeletonPoint> rightHandQ, Queue<SkeletonPoint> leftHandQ, SkeletonPoint head)
{
int valid = 0;
SkeletonPoint old = new SkeletonPoint();
old = rightHandQ.ElementAt(0);
if (
((rightHandQ.ElementAt(0).X - rightHandQ.ElementAt(gestureLen - 1).X) < -.3)
&& (Math.Abs((rightHandQ.ElementAt(0).Y - rightHandQ.ElementAt(gestureLen - 1).Y)) < .1))//right
{
valid = 4;
}
else if//left
(
((rightHandQ.ElementAt(0).X - rightHandQ.ElementAt(gestureLen - 1).X) > .3)
&& (Math.Abs((rightHandQ.ElementAt(0).Y - rightHandQ.ElementAt(gestureLen - 1).Y)) < .1))
{
valid = 3;
}
else if//down
(
((rightHandQ.ElementAt(0).Y - rightHandQ.ElementAt(gestureLen - 1).Y) > .5)
&& (Math.Abs((rightHandQ.ElementAt(0).X - rightHandQ.ElementAt(gestureLen - 1).X)) < .1))
{
valid = 2;
}
else if//up
(
((rightHandQ.ElementAt(0).Y - rightHandQ.ElementAt(gestureLen - 1).Y) < -.5)
&& (Math.Abs((rightHandQ.ElementAt(0).X - rightHandQ.ElementAt(gestureLen - 1).X)) < .1)
&& (Math.Abs((leftHandQ.ElementAt(0).Y - leftHandQ.ElementAt(gestureLen - 1).Y)) < .1))
{
valid = 1;
}
else if//Lleft
(
((leftHandQ.ElementAt(0).X - leftHandQ.ElementAt(gestureLen - 1).X) > .3)
&& (Math.Abs((leftHandQ.ElementAt(0).Y - leftHandQ.ElementAt(gestureLen - 1).Y)) < .1))
{
valid = (int)gesCode.lLeft;
}
else if//lup
(
/* ((leftHandQ.ElementAt(0).Y - leftHandQ.ElementAt(gestureLen - 1).Y) < -.5)
&& (Math.Abs((leftHandQ.ElementAt(0).X - leftHandQ.ElementAt(gestureLen - 1).X)) < .1)
&& (Math.Abs((rightHandQ.ElementAt(0).Y - rightHandQ.ElementAt(gestureLen - 1).Y)) < .1))*/
leftHandQ.ElementAt(gestureLen-1).Y > head.Y)
{
valid = (int)gesCode.lUp;
}
return valid;
}//returns right hand swipes and left up or left left