C # forty-nine days of advanced programming ---- queue,
Queue
1. Queue Definition
The System. Collections. Queue class indicates the object's first-in-first-out set. objects stored in the Queue are inserted at one end and removed from the other end.
2. Advantages
(1). Ability to process the set in sequence (first-in-first-out)
(2) elements that can accept null values and repeat
3. Queue Constructor
Constructors |
Note |
Queue () |
Initialize a new instance of the Queue class. This instance is empty and has the default initial capacity (32) and uses the default growth factor (2.0) |
Queue (ICollection) |
Initializes a new instance of the Queue class. This instance contains elements copied from the specified set, has the same initial capacity as the number of copied elements, and uses the default growth factor. |
Queue (Int32) |
Initializes a new instance of the Queue class. This instance is empty and has the specified initial capacity and uses the default growth factor. |
Queue (Int32, single) |
Initializes a new instance of the Queue class. This instance is empty and has the specified initial capacity and uses the specified growth factor. |
4. Queue attributes
Attribute name |
Note |
Count |
Obtain the number of elements contained in the Queue |
5. Queue Method
Method Name |
Note |
Void Clear () |
Remove all objects from Queue |
Bool Contains (object obj) |
Determine whether an element is in the Queue |
Object Clone () |
Create a superficial copy of the Queue |
Void CopyTo (Array array, int index) |
Copying the Queue element to an existing Array from the specified Array index |
Object Dequeue () |
Remove and return the object at the beginning of Queue |
Void Enqueue () |
Add the object to the end of the Queue |
Object Peek () |
Returns the object at the beginning of the Queue without removing it. |
Object [] ToArray () |
Copy the Queue element to the new array |
Void TrimToSize () |
Set the capacity to the actual number of elements in the Queue |
6. Use Cases of Queue
Good
Using System;
Using System. Collections;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
Using System. Threading. Tasks;
Namespace queue
{
Class Program
{
Static void Main (string [] args)
{
// Create a queue
Queue myQ = new Queue ();
MyQ. Enqueue ("The"); // enter The queue
MyQ. Enqueue ("quick ");
MyQ. Enqueue ("brown ");
MyQ. Enqueue ("fox ");
MyQ. Enqueue (null); // Add null
MyQ. Enqueue ("fox"); // Add duplicate elements;
// Print the number and value of the queue
Console. WriteLine ("myQ ");
Console. WriteLine ("\ tCount: {0}", myQ. Count );
// Print all values in the queue
Console. WriteLine ("Queue Values ");
PrintValues (myQ );
// Print the first element in the queue and remove it.
Console. WriteLine ("(Dequeue \ t {0})", myQ. Dequeue ());
// Print all values in the queue
Console. WriteLine ("Queue Values ");
PrintValues (myQ );
// Print the first element in the queue
Console. WriteLine ("(Peek) \ t {0}", myQ. Peek ());
// Print all values in the queue
Console. WriteLine ("Queue Values ");
PrintValues (myQ );
Console. ReadKey ();
}
Public static void PrintValues (IEnumerable myCollection)
{
Foreach (object item in myCollection)
{
Console. WriteLine ("{0}", item );
}
Console. WriteLine ();
}
}
}
7. Remarks
(1) The capacity of the Queue is the number of elements that can be stored by the Queue. the default initial capacity of Queue is 32. when adding elements to the box Queue, the capacity will be automatically increased as needed by reallocation. you can call TrimToSize to reduce the capacity. the proportional factor is the number multiplied by the current capacity when a larger capacity is required. determine the growth factor during paparazzi Queue. the default growth factor is 2.0.
(2) Queue can accept null references as valid values and allow repeated elements.
(3) An empty reference can be added as a value to the Queue. to distinguish between the null value and the end of the Queue, check the Count attribute or catch the InvalidOperationException thrown when the Queue is empty.
Case: Working with threads
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
Using System. Threading;
Using System. Threading. Tasks;
/*
* Sets of elements processed in the first-in-first-out mode.
* For example, if an airplane is registered in a queue, the first plane is first boarded, but the queue has a priority.
* Similar to the economy class and business class teams, there are two different teams, and business class is preferred.
* In. NEt technology, using System. Collections. Generic. Queue <T>
* Is the queue generic version implementation.
* System. Collections. Queue is a non-generic implementation. The parameter is an object.
* Public class Queue <T>: IEnumerable <T>, ICollection, and IEnumerable
* From the definition of the queue, we can see that it implements iteration and collection interfaces, but it does not implement ICollection <T>, because
* The added Remove method defined in this method will destroy the queue.
* The main difference between a queue and a list is that the queue does not implement the IList interface.
* Therefore, you cannot use the indexer to access the queue. The queue can only add elements,
* This element can only be placed at the end of the queue (Enqueue (), and the Dequeue () element is removed from the header ()
* Enqueue inserts an element from the end of the queue, while Dequeue takes an element,
* The retrieved element is deleted first. For example, if it is called again, the next element is deleted.
*
*
* Case: Use a thread to add documents to the queue,
* Use another thread to read the queue
* The type of the storage queue is Document. We first define a Document class.
* Define a document processing class DocumentManager,
* This includes adding and reading methods, and an attribute used to determine whether the queue is empty.
* Then we define a ProcessDocuments to process threads and operation documents.
*/
Namespace queue and thread
{
Class Program
{
Static void Main (string [] args)
{
DocumentManager mg = new DocumentManager ();
ProcessDocuments process = new ProcessDocuments (mg );
// Start the read thread, but there is no content now. You can read it after adding it.
ProcessDocuments. Start (mg );
Document doc = null;
For (int I = 0; I <500; I ++)
{
Doc = new Document ("syx:" + I, "hello, I love You ");
Mg. AddDocument (doc );
// Sleep for other threads
Thread. Sleep (20 );
}
Console. ReadKey ();
}
}
/// <Summary>
/// Document class: Describes the title and content of a class document.
/// </Summary>
Public class Document
{
Public string title;
Public string content;
Public Document (string title, string content)
{
This. title = title;
This. content = content;
}
}
Public class DocumentManager
{
// Define a queue set
Private readonly Queue <Document> docQueue = new Queue <Document> ();
// Add document
Public void AddDocument (Document doc)
{
// Insert content from one end of the queue
DocQueue. Enqueue (doc );
Console. WriteLine ("successfully inserted document: {0}", doc. title );
}
// Read the document
Public Document GetDocument ()
{
Document doc = null;
Lock (this)
{
Doc = docQueue. Dequeue ();
Return doc;
}
}
// Read-only attribute to check whether there are any elements in the queue
Public bool IsDocumentAvailable
{
Get {return docQueue. Count> 0 ;}
}
}
// Process Document
Public class ProcessDocuments
{
Private DocumentManager dm;
Public ProcessDocuments (DocumentManager dm)
{
This. dm = dm;
}
Public static void Start (DocumentManager manager)
{
// Parameter: public delegate void ThreadStart ();
New Thread (new ProcessDocuments (manager). DoThreadFunc). Start ();
}
Public void DoThreadFunc ()
{
While (true)
{
If (this. dm. IsDocumentAvailable)
{
Document doc = this. dm. GetDocument ();
Console. WriteLine ("read from the queue and delete the document title: {0} content: {1}", doc. title, doc. content );
}
}
}
}
}
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.