-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueues.cs
More file actions
56 lines (46 loc) · 1.98 KB
/
Queues.cs
File metadata and controls
56 lines (46 loc) · 1.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
using System;
using System.Collections.Generic;
class Example
{
public static void Main()
{
Queue<string> numbers = new Queue<string>();
numbers.Enqueue("one");
numbers.Enqueue("two");
numbers.Enqueue("three");
numbers.Enqueue("four");
numbers.Enqueue("five");
// A queue can be enumerated without disturbing its contents.
foreach( string number in numbers )
{
Console.WriteLine("First queue elements: " + number);
}
Console.WriteLine("\nDequeuing '{0}'", numbers.Dequeue());
Console.WriteLine("Dequeuing '{0}'", numbers.Dequeue());
Console.WriteLine("Peek at next item to dequeue: {0}, {1}, {2}",
numbers.Peek(), "FIFO", numbers.Count);
// Create a copy of the queue, using the ToArray method and the
// constructor that accepts an IEnumerable<T>.
Queue<string> copyOfNumbers = new Queue<string>(numbers.ToArray());
Console.WriteLine("\nCopy of first queue");
foreach(Object obj in copyOfNumbers){
Console.WriteLine("Copy of first queue elements: " + obj);
}
// Create an array twice the size of the queue and copy the
// elements of the queue, starting at the middle of the
// array.
Console.WriteLine("\nArray Twice the size of first queue, copied to array");
string[] arrayQueue = new string[numbers.Count *2];
numbers.CopyTo(arrayQueue, numbers.Count);
foreach(Object obj in arrayQueue){
Console.WriteLine("Array elements: " + obj);
}
// Create a second queue, using the constructor that accepts an
// IEnumerable(Of T).
Queue<string> numbers2 = new Queue<string>(arrayQueue);
Console.WriteLine("\nNew Queue with accepted arguemnts of IEnumerable(Of T)");
foreach(Object obj in numbers2){
Console.WriteLine("2nd Queue elements: " + obj);
}
}
}