Data Structure Essay - two kinds of queues, and the STL <queue>

We will encounter a suspected crash the operating system, just point your mouse also no effect, but after a while, it will put your mouse operations are performed again, in the process, on the use of such data queue sorting structure.

队列( queue )是只允许在一端进行插入操作,而在另一端进行删除操作的线性表。允许插入的一端称为队尾,允许删除的一端称为队头。

First, the chain queue

Chain queue, similar to the one kind of queue list, the space for a chain from the queue length is not fixed, the cycle than the queue order stored, the more a pointer field only.

definition

Need to define two structures, one node is a head and tail pointer

typedef int datatype;
typedef struct queue_node
{
	datatype data;
	queue_node* next; //下一个结点(结构体指针)
}node,*queueptr; //节点结构的指针
struct linked_queue //链式队列的结构
{
	queueptr front, rear;
};

Queue initialization

Initializing the queue, only it needs to be initialized to the queue structure, as to the application upon insertion node.

int initqueue(linked_queue &q) //初始化链式队列结构,引用传递
{
	q.front = q.rear = (queueptr)(malloc(sizeof(node)));
	if (!q.front)
	{
		cout << "初始化失败" << endl;
		return 0;
	}
	q.front->next = NULL; //此处一定要置空
	cout << q.front->next << endl;
	return 1;
}

Insert

A point worth noting is how to address the first element of the head node is assigned next?
First, during initialization, the head and tail front, rear equal, then use rear.next = (node address) the assignment, you can solve.

int insert_queue(linked_queue &q, datatype x)//将x插入队列
{
	queueptr s = (queueptr)malloc(sizeof(node));
	if (!s)
	{
		cout << "申请失败" << endl;
		return 0;
	}
	else
	{
		s->data = x;
		s->next = NULL; //生成新的结点
		q.rear->next = s; 
		//插入尾部,刚开始第一步时,因为头尾指针相同,所以头指针被赋予了第一个元素的地址
		q.rear = s; //修改队尾指针
		return 1;
	}
}

Pop-up element

When the pop-up is necessary to determine whether the queue is empty? Whether it is the first element of pop? If the first element is the need to let the pop-up head and tail pointers are equal again.

int pop_queue(linked_queue &q, datatype& e)//用e返回其值
{
	queueptr p;
	if (q.front == q.rear)
	{
		cout << "队列为空" << endl;
		return 0;
	}
	p = q.front->next; //暂存头元素指针
	e = p->data; 
	q.front = p->next;
	if (q.rear = p) //若队头是队尾,删除后队尾指向队头
		q.rear = q.front;
	free(p);
	return 1;
}

As for the display, calculation and the same number of elements in the queue of the linked list.

Second, cyclic queue

First, let's look at a problem, we assume that a length of the array 5, the initial state of the queue is empty, front and rear pointers to point to location 0 of the subscript. Then enqueue a1, a2, a3, a4, front pointer still points to the next location designated as 0, rear 4 points to the location.

Dequeue a1, a2, then the front pointer points to the position labeled 2, rear unchanged reentry team a5, this time constant pointer front, rear pointer moves to the outside of the array. At this point we can be in position 0,1 or spare, a phenomenon called a false overflow.

So the solution to this problem is to back is full, and then start from scratch.我们把队列的这种头尾相接的顺序存储结构称为循环队列。

When writing code, we need to consider the following questions.

When the queue is empty it? Obviously when the front == rear when, in order to prevent not know when the two are equal is empty or full. We change the rules when the queue is full, there is an array of a spare unit.

The queue full: We define the maximum size of the queue maximum bit queuesize, that queue full condition is (rear+1)%queuesize==front.

The queue length: When rear <When front, the length of rear-front + queuesize, but rear> When the front, the length of rear-front, so the general formula (rear-front+queuesize)%queuesize

Definition and basic operation

typedef int datatype;
#define maxsize 1024
typedef struct circular_queue
{
	datatype data[maxsize];
	int front;
	int rear;
}queue;
int initqueue(queue *q) //初始化
{
	q->front = q->rear = 0;
	return 1;
}
int length(queue* q) //求长度
{
	return (q->rear - q->front + maxsize) % maxsize;
}

insert

int insert_queue(queue* q, datatype e)
{
	if ((q->rear + 1) % maxsize == q->front)
	{
		cout << "循环队列为满" << endl;
		return 0;
	}
	q->data[q->rear] = e;
	q->rear = (q->rear + 1) % maxsize;
	return 1;
}

delete

int pop_queue(queue* q, datatype* x)//弹出元素放到地址x中
{
	if (q->front == q->rear)
	{
		cout << "循环队列为空" << endl;
		return 0;
	}
	*x = q->data[q->front];
	q->front = (q->front + 1) % maxsize;
	return 1;
}

All elements

int show_queue(queue* q)
{
	if (q->front == q->rear)
	{
		cout << "循环队列为空" << endl;
		return 0;
	}
	int i = q->front;
	while ( i% maxsize != q->rear) //从头到尾
	{
		cout << q->data[i] << endl;
		i++;
	}
}

三、STL中的queue

The first is the basic queue, the operation is very simple, after all, the wheel was finished.

#include<queue>//头文件
queue<typename>name;
back()返回最后一个元素
empty()如果队列空则返回真
front()返回第一个元素
pop()删除第一个元素
push()在末尾加入一个元素
size()返回队列中元素的个数

The basic operation of the priority queue

In the priority queue, the element is given priority. When accessing the element, the element with the highest priority is the first to be deleted. Similar to the function and operation.

definition:priority_queue<Type, Container, Functional>

Type is the data type, the type of container is Container (Container container arrays must be implemented, such as vector, deque and the like, but can not use the default is used inside list.STL Vector), is a comparison of the Functional embodiment. When only need to use custom data types need to pass these three parameters, using basic data types, only incoming data types, the default is the top big heap. priority_queue <> supportable container must be implemented in an array of containers

 priority_queue<int> a; 
//等同于 priority_queue<int, vector<int>, less<int> > a;
priority_queue<int, vector<int>, greater<int> > b;  
//这样就是小顶堆
堆:通俗来讲利用完全二叉树的结构来维护的一维数组

Guess you like

Origin www.cnblogs.com/lonely-ok/p/12656775.html