Data structure-queue

Queue
1. Introduction to
Queue Queue is a first-in-first-out data structure, which is different from the stack, but it is easier to understand. Similar to queuing up in a cafeteria for food, and queuing up to buy tickets at a station.
Those who came later were at the end of the line, and those who came first served meals or bought tickets. As shown
Insert picture description here

The queue always inserts elements from the end of the queue and removes elements from the head of the queue, satisfying the first-in first-out rule. Therefore, a front pointer is required to point to the previous position of the first element, and a rear pointer rear is used to point to the last element.
, A front pointer is needed to point to the previous position of the first element of the team, and a rear pointer rear is used to point to the last element of the team. Similar to the stack, when the array is used to implement the queue, the head pointer front and the tail pointer rear are int variables (array subscripts start from 0); when using a linked list to implement the queue, they are int* variables. pointer. In this way, when using an array to implement the above example, the head of the team pointer front and the end of the team pointer rear are shown in Figure 7-3. Next, we will introduce the common operations of the queue, including clear, get the number of elements in the queue (size), empty, enter the queue (push), dequeue (pop), get the first element of the queue (get_front) ). Get the tail element (get _rear) and so on. The following will use the array q[] to implement the queue, and the int type variable front stores the index of the previous element of the first element of the queue, and rear stores the index of the last element of the queue (the array index starts from 0). The following is a demonstration of common operations:Insert picture description here

(1) Clear

When using an array to implement a queue, the initial state is front=-1 and rear=-1. The first step in the previous figure is that rear points to 0 because there is already an element in the queue. If there is no element, rear should point to -1 position.

void clear()
{
    
    
    front=rear=-1;
}

(2) Obtaining the number of queue elements (size)
Obviously rear-front is the number of queue elements, which is easy to see from the figure above.

int size()
{
    
    
    return rear-front;
}

(3) Empty (empty)
The condition for judging that the queue is empty is front==rear.

bool empty()
{
    
     
  if(front==rear) return true;
  else return false ;
}

(4) Enqueue (push)
Because the tail pointer rear points to the tail element, when entering the element, you need to add 1 to the rear and then store it to the position pointed to by the rear.

void push(int x)
{
    
    
  q[++rear]=x;
}

(5) Popping
can directly add 1 to the head of the team to achieve the effect of popping .

void pop()
{
    
    
   front++;
}

(6) Get the head element of the team (get_front)
Since the head pointer front points to the element before the head element of the team, front+1 is the position of the head element.

int get_front()
{
    
    
   return q[front+1];
}

(7) Take the tail element of the team, the tail pointer points to the tail element, so the position of rear can be directly accessed.

int get_rear()
{
    
    
   return q[rear];
   }

Of course, here I am more accustomed to another way of writing,
giving a template

//front表示队头,rear表示队尾
int q[N],front = 0, rear = -1;
//向队尾插入一个数
q[++rear] = x;
//从队头弹出一个数
front++;
//队头的值
q[front];
//队尾的值
q[rear];
//判断队列是否为空
if (front <= rear)
{
    
    
}

Implement a queue, the queue is initially empty, and supports four operations:

(1) "push x"-insert a number x to the end of the line;

(2) "pop"-pop a number from the head of the line;

(3) "empty"-judge whether the queue is empty;

(4) "query"-Query the head of the team.

Now we have to perform M operations on the queue, each of which must output corresponding results for operation 3 and operation 4.

Input format The
first line contains the integer M, which represents the number of operations.

The next M lines, each line contains an operation command, the operation command is one of "push x", "pop", "empty", and "query".

Output format
For each "empty" and "query" operation, a query result must be output, and each result occupies a row.

Among them, the query result of the "empty" operation is "YES" or "NO", and the query result of the "query" operation is an integer, which represents the value of the head element.

The data range is
1≤M≤100000,
1≤x≤109, and
all operations are guaranteed to be legal.

Input example:
10
push 6
empty
query
pop
empty
push 3
push 4
pop
query
push 6
Output example:
NO
6
YES
4

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<string>
using namespace std;
const int maxn = 1e5 + 10;
int q[maxn],hh=0,tt=-1;
int main()
{
    
    
	int M;
	cin >> M;
	while (M--)
	{
    
    
		string order;
		int x;
		cin >> order;
		if (order == "push")   //插入
		{
    
    
			cin >> x;
			q[++tt] = x;
		}
		else if (order == "query") //查询队头
		{
    
    
			cout << q[hh] << endl;
		}
		else if (order == "pop")  //弹出队头
		{
    
    
			hh++;
		}
		else
		{
    
    
			if (hh <= tt)
			{
    
    
				cout << "NO" << endl;
			}
			else
			{
    
    
				cout << "YES" << endl;
			}
		}
	}
	return 0;
}

STL
(1) push()
push(x) enqueue x with a time complexity of 0(1).
(2) front() and back()
front() and back() can get the head element and The end element of the team has a time complexity of O(1)
(3) pop()
pop() makes the head element of the team dequeue, and the time complexity is O(1).

#include<iostream>
#include<queue>
using namespace std;
int main()
{
    
    
	queue<int>q;
	for (int i = 1; i <= 5; i++)
	{
    
    
		q.push(i);  //将1 2 3 4 5 依次入队
	}
	for (int i = 1; i <= 3; i++)
	{
    
    
		q.pop();   //将 1 2 3 依次出队
	}
	cout << q.front() << endl; //输出4
	return 0;
}

(4) empty()
empty() detects whether the queue is empty, returns true if it is empty, otherwise returns false, and the time complexity is O(1).
(5) size()
size() returns the number of elements in the queue, and the time complexity is O(1).

#include<iostream>
#include<queue>
using namespace std;
int main()
{
    
    
	queue<int>q;
	for (int i = 1; i <= 5; i++)
	{
    
    
		q.push(i);
	}
	if (q.empty() == true)
	{
    
    
		cout << "NO" << endl;  //队列非空
	}
	cout << q.size() << endl;   //输出5
}

Priority queue
priority_ _queue is also called priority queue, and its bottom layer is implemented by heap. In the priority queue, the head element of the queue must be the one with the highest priority in the current queue. For example, there are the following elements in the queue, and the priority is defined:

Peach (Priority 3)
Pear (Priority 4)
Apple (Priority 1)

Then the order of departure is pear (4) → peach (3) → apple (1).

Of course, you can add (push) elements to the priority queue at any time, and the data structure heap at the bottom of the priority queue will adjust the structure at any time, so that the first element of the queue has the highest priority every time.

The priority here is stipulated. For example, in the above example, you can also specify that the smaller the number, the higher the priority (in German courses, a score of 1 is considered excellent, and a score of 5 or 6 is a failure).

1. The definition of priority_ _queue

To use the priority queue, you should add the header file #include first, and add "usingnamespace std;" below the header file, and then you can use it.

Its definition is written in the same way as other STL containers, typename can be any basic data type or container: priority_ queue<typename> name;

2. Access to elements in the priority_queue container

Unlike the queue, the priority queue does not have a front() function and a back() function, but can only access the top element (also called the top element of the heap) through the top(() function, which is the highest priority element.

#include <stdio.h>

#include <queue>

using namespace std;

int main() {
    
    

priority_ queue<int> q;

q.push(3) ;

q.push(4);

q.push(1);

printf("%d\n", q.top());   //输出4

return 0;

Knowledge points about priority queues will be added later.

Guess you like

Origin blog.csdn.net/weixin_45629285/article/details/107352399