C # Data structures and algorithms--queues

Source: Internet
Author: User

What is a queue, the so-called queue is the linear table in which the queue is an insert operation that is limited to the tail of the table and other operations are scoped to the head of the table. The end of the insert operation is called the tail of the queue (Rear), and the head of the other operation is called the team Head (Front). When there are no data elements in the column, it is called an empty pair of columns (empty Queue). The queue is usually written as: q= (A1,a2,..., an), Q is the 1th letter of the English word queue. A1 is the team head element, an is the tail element of the team. These n elements are in accordance with the order of A1,a2,..., an, in order to queue, out of the same sequence, A1 first out of the team, an last out of the team. Therefore, the operation of the column is based on the principle of first-in-a-go-out, or last-in-out, which is like queuing up to buy a ticket and doing it. Therefore, the queue is also known as a FIFO table or a Lilo table. The operation of the queue Q. Specific situation,:

650) this.width=650; "src=" Http://files.jb51.net/file_images/article/201211/2012110120523845.png "/>

The formal definition of a queue is: queue précis-writers is Q, is a two-tuple, q = (d, R) where: D is a finite set of data elements, a finite set of relationships between data elements. There are many examples in real life that are similar to queues. For example, the queue to take money, first come first fetch, later in the queue at the end.

Similarly, we represent the queue in the C # language's generic interface, and the method members in the interface represent basic operations. In order to express the convenience and simplicity, the generic queue interface is named iqueue<t> (in fact, in C #, the generic queue class is inherited from the ienumerable<t>, ICollection, and IEnumerable interfaces, no iqueue<t>-generic interface). The definition of the queue interface iqueue<t> source code is as follows.

Public interface Iqueue<t> {
int GetLength (); The length of the queue; Initial conditions: queue exists; operation Result: Returns the number of data elements in the queue. Everything starts with:

650) this.width=650; "src=" Http://files.jb51.net/file_images/article/201211/2012110120523846.png "/>

BOOL IsEmpty ();  Determines whether the column is empty; the initial condition: the queue exists; Operation Result: Returns True if the queue is empty, otherwise false. In all cases:

650) this.width=650; "src=" Http://files.jb51.net/file_images/article/201211/2012110120523847.png "/>

void Clear (); empty queue; Initial conditions: queue exists; operation result: Leave the queue empty.

void in (T item); Queued Initial conditions: queue exists; action Result: Adds a new data element with a value of item to the end of the queue, and the queues are changed.

650) this.width=650; "Width=" 620 "height=" 437 "src=" http://files.jb51.net/file_images/article/201211/ 2012110120523848.png "/>


T out (); Out of the team. Return to the head node specific situation

The complexity of this algorithm is O (1)

650) this.width=650; "src=" Http://files.jb51.net/file_images/article/201211/2012110120523849.png "/>


T Getfront (); Take the head element of the first element in detail

The complexity of this algorithm is O (1)

650) this.width=650; "src=" Http://files.jb51.net/file_images/article/201211/2012110120523850.png "/>

The complexity of this algorithm is O (1)


}

This is the basic description of the queue.


Reference: http://www.jb51.net/article/31699.htm

This article comes from the "Ricky's blog" blog, please be sure to keep this source http://57388.blog.51cto.com/47388/1663516

C # Data structures and algorithms--queues

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.