Csharp/CSharp Tutorial/Data Structure/Queue

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Clear a Queue

using System;
using System.Collections;
class MainClass
{
  static void Main(string[] args)
  {
    Queue a = new Queue(10);
    int x = 0;
    a.Enqueue(x);
    x++;
    a.Enqueue(x);
    foreach (int y in a)
    {
      Console.WriteLine(y);
    }
    a.Dequeue();
    a.Clear();
  }
}
0
1

Creating a list from a queue

using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text;
public class MainClass
{
    public static void Main()
    {
        Queue <string> q = new Queue<string>();
        List<string> stringList = new List<string>(q);
    }
}

Dequeue and Peek

using System;
using System.Collections.Generic;
    public class Tester
    {
        static void Main()
        {
            Queue<Int32> intQueue = new Queue<Int32>();
            for (int i = 0; i < 5; i++)
            {
                intQueue.Enqueue(i * 5);
            }
            PrintValues(intQueue);
            Console.WriteLine("\n(Dequeue)\t{0}", intQueue.Dequeue());
            Console.Write("intQueue values:\t");
            PrintValues(intQueue);
            Console.WriteLine("\n(Dequeue)\t{0}", intQueue.Dequeue());
            PrintValues(intQueue);
            Console.WriteLine("\n(Peek) \t{0}", intQueue.Peek());
            Console.Write("intQueue values:\t");
            PrintValues(intQueue);
        }
        public static void PrintValues(IEnumerable<Int32> myCollection)
        {
            IEnumerator <Int32> myEnumerator = myCollection.GetEnumerator();
            while (myEnumerator.MoveNext())
                Console.Write("{0} ", myEnumerator.Current);
         }
    }

Enqueue and dequeue

using System; 
using System.Collections; 
  
class MainClass { 
 
  public static void Main() { 
    Queue q = new Queue(); 
 
    q.Enqueue(1); 
    q.Enqueue(2); 
    q.Enqueue(3); 
    q.Enqueue(4); 
 
    Console.Write("queue: "); 
    foreach(int i in q) 
      Console.Write(i + " "); 
 
    Console.WriteLine();         
    Console.Write("Dequeue -> "); 
    int a = (int) q.Dequeue(); 
    Console.WriteLine(a); 
 
    Console.Write("queue: "); 
    foreach(int i in q) 
      Console.Write(i + " "); 
 
    Console.WriteLine();         
 
  } 
}
queue: 1 2 3 4
Dequeue -> 1
queue: 2 3 4

Peek a queue

using System;
using System.Collections;
using System.Collections.Specialized;
class MyClass{
   public string MyName="A";   
}
class MainClass
{
  static void Main(string[] args)
  {
    Queue classQueue = new Queue();
    classQueue.Enqueue(new MyClass());
    classQueue.Enqueue(new MyClass());
    classQueue.Enqueue(new MyClass());
    // Peek at first car in Q.
    Console.WriteLine("First in Q is {0}",
      ((MyClass)classQueue.Peek()).MyName);
  }
}
First in Q is A