[Translation] C # data structures and algorithms-Chapter 5 stack and queue (Part 2)

Source: Internet
Author: User

Queue, Queue class and one Queue class implementation

A queue is a data structure in which data enters from the end of the list and is removed from the List header. Queues are used to store items in the order they appear. A queue is a representative of the first-in-first-out (FIFO) data structure. Queues are usually used to process commands submitted to the operating system or to a print pool task. In addition, the simulation program uses the queue model to simulate waiting customers.

Queue operations

Two main operations related to the queue are to add a project to the queue and remove a project from the queue. The operation of adding an item to a queue is called an Enqueue, and the operation of removing an item from the queue is called a Dequeue ). The queuing operation adds an item to the end of the queue. The queuing operation removes an item from the queue header. Figure 5.2 shows these operations.

 

Another major operation performed on the queue is to view the project in the header. The Peek method, similar to the method with the same name in the Stack class, is used to view the project at the starting position. This method only returns this item rather than removing it from the queue.

The Queue class has many other attributes that can be used to assist our programming. However, before we discuss them, let's take a look at how to implement a queue class.

Implement a queue

Using ArrayList to implement a queue class is almost unnecessary, just as we implement the Stack class. Because of its built-in features, ArrayList is an excellent choice for implementing these types of data structures. When we need to insert an item to the queue, the Add method of ArrayList places the element in the next idle position in the list. When we need to remove the start position from the queue, ArrayList will move the remaining items in the list forward one position in turn. We do not need to maintain a placeholder flag, which may cause hidden errors in the code.

The following queue class includes the EnQueue, DeQueue, ClearQueue (clear Queue), the Peek method and Count attributes, and a default constructor:

Public Class CQueue

{

Private ArrayListPqueue;

 

PublicCQueue ()

{

Pqueue =New ArrayList();

}

 

Public VoidEnQueue (ObjectItem)

{

Pqueue. Add (item );

}

 

Public VoidDeQueue ()

{

Pqueue. RemoveAt (0 );

}

 

Public ObjectPeek ()

{

ReturnPqueue [0];

}

 

Public VoidClearQueue ()

{

Pqueue. Clear ();

}

 

Public IntCount ()

{

ReturnPqueue. Count;

}

}

Queue class: A sample program

We have already introduced QueueClass, and learned them in the process of implementing a queue class. We further explore these methods in a special programming problem that uses queues as the basic data structure. First, we need to mention the basic attributes of several Queue objects.

When a new Queue object is initialized, the default capacity of this Queue is 32. By definition, when the queue is filled up, its size will increase by 2 times. This means that when the initial capacity of a queue is filled up, the capacity will change to 64, and there is no limit on this number. When initializing a queue, you can specify a different initial capacity. As follows:

QueueMyQueue =New Queue(100 );

This statement sets the queue capacity to 100. You can also change the growth factor, which is passed into the constructor as the second parameter, as follows:

QueueMyQueue =New Queue(32, 3 );

This statement specifies3Times the growth rate. Even if the required capacity is the same as the default initial capacity, You need to manually specify it, because the class constructor looks for a function with different signatures than the default constructor.

The initialization method of a generic queue is as follows:

Queue<Int> Numbers =New Queue<Int> ();

As mentioned earlier, queues are often used to simulate queuing. One scenario is that we can use a queue to simulate the annual single midnight dance at Elks Lodge. Men and women each form a group to enter the gathering place. The stage is very small. At the same time, only three men and women are allowed. Due to the limitations of the stage space, when there is no space, the men and women in the queue to invite the first to form a pair of partners. When this pair is taken out from the queue, the last man and Lady is at the forefront of the queue.

When this action occurs, the program will show that this pair of partners is already at the top of the next queue. If there is no full partner, the next person in the queue will be announced. This will be announced if there are no remaining persons in the queue.

First, let's take a look at the data we use to simulate:

F Jennifer Ingram M Frank Opitz M Terrill Beckerman M Mike Dahly F Beata Lovelace M Raymond Williams F Shirley Yaw M Don Gundolf F Bernica Tackett M David Durr M Mike McMillan F Nikki Feldman

We use a Data Structure to represent every dancer. Two simple String-class methods (Chars and Substring) are used to construct a dancer. The following is a program:

UsingSystem;

UsingSystem. Collections;

UsingSystem. IO;

 

NamespaceCsqueue

{

Public Struct Dancer

{

Public StringName;

Public

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.