What is a ring-shaped queue?
Queues are a commonly used data structure that ensures that data is operated in accordance with the "FIFO" principle, The first element that comes in is also the first element. The ring queue is a special queue structure, which guarantees that the elements are also FIFO, but the difference from the general queues is that they are circular, that is, the last element of the queue head is the tail of the queue, usually a closed loop that holds the number of elements fixed.
Second, the advantages of the ring queue
1. To ensure that the elements are advanced first out
is guaranteed by the nature of the queue, which is guaranteed by the sequential access to the queue in the ring queue.
2. Element space can be reused
Because the general ring queue is a closed loop with a fixed number of elements, the fixed memory space can be allocated when the ring queue is initialized, and only the address of the specified element's memory space needs to be returned when entering or out of the team, and the memory space can be reused to avoid the overhead of frequent memory allocation and release.
3. Provides an efficient mechanism for multi-threaded data communication.
In the most typical producer-consumer model, if you introduce a ring queue, then the generator only needs to generate "something" and put it in the ring queue, and consumers only need to take "things" from the ring queue and consumption can be, without any lock or wait, clever and efficient implementation of multithreading data communication.
Implementation of C # ring queue
Read a data structure of the tutorial, is written in C + +, can own C # or a rookie, let alone C + +, but still bold attempt to use C # to write out the implementation of the ring queue, the first code:
public class Myqueue<t>: IDisposable {private t[] queue;
private int length;
private int capacity;
private int head = 0;
private int tail = 0;
public myqueue (int capacity) {this.capacity = capacity;
This.head = 0;
This.tail = 0;
this.length = 0;
This.queue = new T[capacity];
public void Clear () {head = 0;
tail = 0;
length = 0;
public bool IsEmpty () {return length = 0;
public bool Isfull () {return length = = capacity;
public int Length () {return length; public bool EnQueue (T node) {if (!
Isfull ()) {Queue[tail] = node;
Tail = (++tail)% capacity;
length++;
return true;
return false;
Public T Dequeue () {t node = default (t); if (!
IsEmpty ()) {node = Queue[head];
Head = (++head)% capacity;
length--;
} return node; public void Traverse () {to (int i = head; I < length + head; i++) {Console.WriteLine (queue[i% capacity]);
Console.WriteLine ($ "Before {i-head}");
The public void Dispose () {queue = null; }
}
In order to be universal, it uses generics to implement the Ring queue class. The most important thing here is to enter the team () EnQueue
and out of the team ( DeQueue
) two methods, into the team or out of the rear and the end of the position to be obtained by the modulo operation, because it is a ring queue, you know.
1. Simple Type queue
Okay, test the team:
Class program
{
static void Main (string[] args) {
myqueue<int> queue = new myqueue<int> (4);
Queue. EnQueue (ten);
Queue. EnQueue ();
Queue. EnQueue ();
Queue. EnQueue (a);
Queue. Traverse ();
Console.read ();
}
Show Results:
Then test the next team:
Class program
{
static void Main (string[] args) {
myqueue<int> queue = new myqueue<int> (4);
Queue. EnQueue (ten);
Queue. EnQueue ();
Queue. EnQueue ();
Queue. EnQueue (a);
Queue. Traverse ();
Console.WriteLine ("Play two Out");
Queue. Dequeue ();
Queue. Dequeue ();
Console.WriteLine ();
Queue. Traverse ();
Console.read ();
}
Run Result:
2. Complex Type queues
Previously said, this queue class is written in generic, corresponding to the C + + template, which means that any type can use this queue class, to test a custom class to try, the following first define a Customer
class:
public class Customer
{public
string Name {get; set;}
public int Age {get; set;}
public void Pringinfo () {
Console.WriteLine ("Name:" + name);
Console.WriteLine ("Age:" + aged);
Console.WriteLine ();
}
Then proceed to the team, as follows:
Class program
{
static void Main (string[] args) {
myqueue<customer> queue = new Myqueue<customer > (5);
Queue. EnQueue (New Customer () {Name = "song Xiao II", age =});
Queue. EnQueue (New Customer () {Name = "Chen Xiaoxan", age =});
Queue. EnQueue (New Customer () {Name = "Wang Xiao si", age =});
Queue. EnQueue (New Customer () {Name = "Zhu Xiao Five", age =});
for (int i = 0; i < queue. Length (); i++) {
queue[i]. Pringinfo ();
}
Console.read ();
}
The above code queue[i].PringInfo();
is implemented through the index, so we have to implement the index in the queue class, add the following code to the MyQueue.cs
class, as follows:
Public T This[int index] {get
{return
queue[index];
}
}
The sense of using a for loop to traverse or not good enough to use foreach
, that is to MyQueue
add a traversal interface to the class, as follows:
This interface is then implemented as follows:
Public ienumerator<t> GetEnumerator () {
foreach (T-node in-queue) {
if (node!= null) {
yield return node;
}
}
}
IEnumerator Ienumerable.getenumerator () {return
GetEnumerator ();
}
This is where the traversal can be changed foreach
, as follows:
Execution results:
Summarize:
The idea of programming is the most important, irrelevant language. The above is the entire content of this article, I hope to be able to learn or work to bring certain help, if you have questions you can message exchange.