A quick overview of interesting queue

Queue Introduction

Cohort analysis

Code demonstrates



Queue Introduction

The definition of
what a queue in the end is? On Baidu says so:

A queue is a special linear form, is special in that it only allows deletion at the front end of the table (Front), while the rear end of insertion, the table (REAR), and stack as a queue by the operating linear restriction table. Referred to as the tail end of the end, delete operation will be referred to as head-of-insertion operation. When there is no queue element, called an empty queue.

In my understanding it seems, in fact, among the queues and queue data structures that we live among almost, when we went to line up to buy tea color, is the first to line up in front of people, to the post came in the back, most the team is in front of the head, the final surface is the tail. Increase team is back from behind into, buying tea Yan leave, it is the first team from leaving. We queue data structure which is true, insert elements from the back, remove elements from the front.
Here Insert Picture Description
Category
1. the queues:

Establishing an order queue structure must be statically assigned or dynamically for continuous application of a storage space, and two pointer management is provided. A head pointer is the team front, which points to the head elements; the other is the tail pointer rear, which points to the next memory location enqueued element

As the name implies, the order of the queue is like we usually line up, like a line, big long queue. But there is a drawback of the queues in front of people gone, empty space is down, it will lead to a waste of space, so to be able to reuse the space, we will use the following circular queue.
2. The circular queue:

In actual use the queue, the queue in order to make the space can be reused, often using a slight modification of the method of the queue: regardless of insertion or deletion of the queue exceeds the allocated space once the front or rear pointer is incremented by a pointer, let it points to the start position of this continuous space. They really changed from MaxSize-1 by 1-0, can be used to take over operation and rear% MaxSize front% MaxSize achieved. This is actually the queue space imagine an annular space, the annular space in the storage unit using the loop, this method also called a cyclic queue management queue. In addition to the simple application, truly practical queue is a circular queue.

Is circular queue head and tail are linked together, in fact, we usually use circular queue, some of the more tricky.

Cohort analysis

1. Simple Analysis: is a class queue, since it is a class, you are bound to have methods and attributes. For property, we can simply think about, we will usually line up a team head and the tail, therefore, also a team queue head and tail of the team, we need to mark them in the property. The team is definitely a length of time we traverse must know the size of the team length, in order to have the cycle. We can also look at the maximum queue length is defined, it is possible to have a capacity. Since it is a queue elements arranged in a team, we can define an array of pointers.
Therefore, attributes are:

private:
		int *m_queue;           //队列数组指针 
		int length;             //队列元素个数 
		int capacity;           //队列容量 
    	int head;               //队头标记
		int tail;               //队尾标记,都用数组下标表示 

For such a queue, we will certainly have constructor initializes the allocated space, the destructor to reclaim space, the queue emptying function, it is determined whether the queue is empty, the enqueue function (additive element), dequeue function ( remove elements), etc. traversal function:
method:

        Queue(int pcapacity)                //构造函数
		
		virtual ~Queue()                    //析构函数 
		
		void clear()                        //清空队列 
		
		bool Empty()                        //判断队列是否为空 
	
		bool queueFull()
		
        int Queuealength()                  //返回队列长度
        
		bool push(int element)              //入队
	
		bool pop(int &element)              //队头出队
	
		void queueTraverse()                //遍历队列 
		
	

Code demonstrates

Constructor: We can give a queue limit initial capacity, and the property is initialized, the array is defined.

	Queue(int pcapacity)                //构造函数
		{
			capacity=pcapacity;
			head=0;
			tail=0;
			length=0;
			m_queue=new int[capacity];
		} 

Destructor function and queue empty:

        virtual ~Queue()                    //析构函数 
		{
			delete []m_queue;
			m_queue=NULL;
		} 
		void clear()                        //清空队列 
		{
			head=0;
			tail=0;
			length=0;
		} 

Determining whether empty: just queue length determined size can be a length of 0, nature is empty.

	bool Empty()                        //判断队列是否为空 
		{
			if(length==0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

Into the team: Each into the first team, we have to change the position of the tail of the team, the index plus one, but we can not have crossed the line capacity is only so much, then we need special treatment, into the first team, plus a team of length .

bool push(int element)              //入队
		{
			if(queueFull())
			{
				return false;
			}
			else
			{
				m_queue[tail]=element;
				tail++; 
				tail=tail%capacity;          //防止tail越界,若超过。则超过部分继续从零开始。 
				length++;
				return true;
			}
		}

A team: a team every time, the position of the head of the team will change, we need to add a length minus one, but the first team can not overflow and thus have the same index as the tail treatment:

	bool pop(int &element)              //队头出队
		{
			if(Empty())
			{
				return false;
			}
			else
			{
				element=m_queue[head];
				head++;
				head=head%capacity;         
				length--;
				return true;
			}
		}

Traversal:

	void queueTraverse()                //遍历队列 
		{
			for(int i=head;i<length+head;i++)
			{
				cout<<m_queue[i%capacity]<<endl;
			}
			cout<<endl;
		}

The queue was quite close to our real life, they can contact each other about it and there are so similar a little stack.

Published 22 original articles · won praise 27 · views 2852

Guess you like

Origin blog.csdn.net/weixin_44346470/article/details/100162603