C # data structure (queue class)

xiaoxiao2021-03-06  43

After implementing the stack class, we will implement the queue class. In fact, the queue is also an extension of the list. It is actually a special linked list, such as the stack, the difference between it and the stack is, the stack is advanced, the principle is The use of advanced first-out principles. What is advanced? It can be seen everywhere in daily life, such as buy train tickets, everyone must queue, first buy tickets first, then queue in the back, in the team, casually inserting is illegal. You can buy a ticket after you have finished bought it. Of course, this is just a small instance, the queue is the foundation programming technology in programming, now let us use C # to implement it. Such as: The figure below is the team in the team and out of the team.

1 In the process of entering the team: is added to the tail of the queue, the queue data is added, and the tail pointer is moved.

2 During the process of introduction: After the data taken in the head of the queue, then remove the data, the head pointer is moved.

In the following procedure, the list of the List class (C # Data) (1 Link table)) is used, and the chain list can be easily implemented, and the implementation is as follows:

Using system;

Namespace list {/// Queue class public class cqueue {private clist m_list; public cqueue () {// Constructor // Here you can use List m_list = new clist ();} /// into the team public void enqueue (int DATAVALUE) {// Function: Add queue, here use the list of the list of list: // Add data to the tail, data plus 1 m_List.Append (DataValue);} // Out Team public int deveue () { // function: Out of the queue // Return Value: 2147483647 Indicates that the air queue does not return INT QueValue; if (! Isnull ()) {// does not empty queue // move to the head m_list.movefrist (); // Get the current value quevalue = m_list.getCurrentValue (); // Delete the queue's data m_list.delete (); return quevalue;} Return 2147483647;} /// judgment queue is empty public bool isnull () {/ / Features: Judging whether it is empty queue return m_list.isnull ();

/// Empty queue public void clear () {// Clear Link list m_list.clear ();}

/// Take the queue's data number PUBLIC INT QueueCount {get {// Number of queues Return M_List.ListCount;}}

}

Ok, as long as we write a chain class, we can easily implement the queue, you can save a lot of code.

转载请注明原文地址:https://www.9cbs.com/read-55057.html

New Post(0)