Basic operation of queue (loop queue, chain queue)

Article directory

1, Abstract data type definition of queue

The order table can only be added at the end of the team and deleted at the head of the team

ADT Queue
{
	Data
		Same as linear table. The elements have the same type, and the adjacent elements have the relationship of precursor and successor.
	Operation
		Initqueue & queue initialization
		Destroyqueue & destroy queue if it exists
		Clearqueue & clear queue
		QueueEmpty(Q) returns true if the queue is empty, false otherwise
		Gethead (Q, & E) if the queue exists, use e to return to the queue head
		Enqueue (& Q, e) join the team, insert new element E to the end of the team
		Dequeue (& Q, * e), use e to return its value
		QueueLength(Q) number of returned queues		
}ADT Queue

2, Circular queue

The sequential storage structure representation of a queue is called a circular queue

False overflow

The simple sequential storage of queues can cause false overflow, as shown in the following figure:

Obviously, there is still storage space, but the algorithm is full of results.
In order to solve the false overflow, we can use the way of circular queue: the queue (array) is assumed to be a circular table connected at the beginning and the end, so that the useless space at the front of the array caused by the deletion operation can be reused as much as possible. Such a queue is called circular queue.
Through the "module" operation, Q.rear = (Q.rear+1)%n, there will be no "false overflow" problem
The following picture:

It can be seen that for circular queues, it is not possible to judge whether the queue space is "full" or "empty" by the same value of the head and tail pointers. In this case, the conditions for distinguishing the queue space from the queue space are as follows:

  1. One less space element
  2. Set another flag to distinguish whether the team is "full" or "empty".

The condition of team empty: Q.front == Q.rear
Conditions for team full: (Q.rear +1)%MAXQSIZE == Q.front

1. Sequential storage structure representation of queues

#define MAXQSIZE 10	//Maximum possible length of queue
    typedef struct {
	QElemType *base;//Base address of storage space
	int front;		//Head pointer
	int rear;		//Tail pointer
}SqQueue;

2. Initialization

Loop queue initialization is to dynamically allocate an array space with a predefined size of MAXQSIZE.

[algorithm steps]

  • Allocate an array space with the maximum capacity of MAXQSIZE to the queue, and base points to the first address of the array space.
  • The head and tail pointers are set to zero, indicating that the queue is empty.

[algorithm description]

Status InitQueue(SqQueue &Q) {			//Construct an empty queue
	Q.base = new QElemType[MAXQSIZE];	//Allocate an array space with the maximum capacity of MAXQSIZE to the queue
	if (!Q.base) exit(-2);				//Storage allocation failed
	Q.front = Q.rear = 0;				//The head end pointer is set to zero and the queue is empty
	return OK;
}

3. Find the queue length

Direct return direct return (Q.rear-Q.front+MAXQSIZE)% MAXQSIZE

int QueueLength(SqQueue Q){//Return the number of elements of Q, i.e. queue length
	return (Q.rear-Q.front+MAXQSIZE)%MAXQSIZE;
}

4. team entry

Insert a new element at the end of the team

[algorithm steps]

  • Judge whether the queue is full, and return ERROR if it is full
  • Insert new elements at the end of the team
  • End of team pointer plus 1

[algorithm description]

Status EnQueue(SqQueue &Q,QElemType e){//Insert a new team ending element with element e as Q
	if((Q.rear+1)%MAXQSIZE==Q.front)	//The tail pointer is equal to the head pointer after adding 1 in the circular sense, indicating that the team is full
		return ERROER;
	Q.base[Q.rear]=e;					//New element insert end of team
	Q.rear=(Q.rear+1)%MAXQSIZE;			//End of team pointer plus 1
	return OK; 
}

5. teams


[algorithm steps]

  • Judge whether the queue is empty. If it is empty, return ERROR
  • Save team header element
  • Team head pointer plus 1
    [algorithm description]
Status DeQueue(SqQueue &Q,QElemType &e){//Delete Q's team header element
	if(Q.front==Q.rear)	return ERROR;	//Team empty
	e=Q.base[Q.front];					//Save team header element
	Q.front=(Q.front+1)%MAXQSIZE;		//Team head pointer plus 1
	return OK;
}

6. Team leader element

Judge whether the queue is empty or not. If it is not empty, return the queue head element

[algorithm description]

SElemType GetHead(SqQueue Q){	//Returns the team head element of Q without modifying the team head pointer
	if(Q.front!=Q.rear)		 	//Queue is not empty
		return Q.base[Q.front];	//Returns the value of the team head element. The team head pointer does not change
}

Three. Chain team

The queue realized by chain storage structure is called chain queue

1. Chain storage structure representation of queue

typedef struct QNode {
	QelemType data;
	struct QNode *next;
}QNode,*QueuePtr;
typedef struct {
	QueuePtr fornt;		//Head pointer
	QueuePrt rear;		//Tail pointer
}LinkQueue;

2. Initialization

The initialization operation of chain team is to construct an empty team with only header nodes.

[algorithm steps]

  • A new node is generated as the head node, and the queue head and queue tail pointers point to this node.
  • Null pointer field of header node

[algorithm description]

Status IntitQueue(LinkQueue &Q){	//Construct an empty queue Q
	Q.front = Q.rear = new QNode;	//Generate a new node as the head node, and the queue head and tail pointers point to this node
	Q.front->next = NULL;			//Null head node pointer field
	return OK;
}

3. team entry

[algorithm steps]

  • Allocate node space for queued elements, and point to it with pointer p.
  • Set the new node data field to e.
  • Insert a new node to the end of the pair
  • Change the end of queue pointer to p

[algorithm description]

Status EnQueue(LinkQueue &Q, QElemType e){//Insert a new team ending element with element e as Q
	p = new QNode;						  //Allocate node space for queue elements, and use pointer p to point to
	p->data = e;						  //Set the new node data field to e
	p->next = NULL; Q.rear->next = p;	  //Insert new node at the end of the team
	Q.rear = p;							  //Modify end of queue pointer
	return OK;
}

4. teams

It is necessary to judge whether the team is empty and release the space after leaving the team.

[algorithm steps]

  • Judge whether the team is empty. If it is empty, return ERROR.
  • Temporarily save the space of the team head element for release.
  • Modify the pointer field of the header node to point to the next node.
  • Determine whether the queue element is the last element. If so, reassign the queue end pointer to the head node.
  • Release the space of the original team head element.

[algorithm description]

Status DeQueue(LinkQueue &Q, QElemType &e) {//Delete the team header element of Q and return its value with e
	if (Q.front == Q.rear) return ERROR;	//ERROR if the queue is empty
	p = Q.front->next;						//p points to team head element
	e = p->data;							//e save the value of the team head element
	Q.front->next = p->next;				//Modify the pointer field of the header node
	if (Q.rear == p)	Q.rear = Q.front;	//The last element is deleted, and the end of the queue pointer points to the head node
	delete p;								//Release the space of the original team leader element
	return OK;
}

5. Take out the team leader element

Judge whether it is empty, return the team head element when it is not empty, and do not change the pointer

[algorithm description]

SElemType GetHead(LinkQueue Q) {	//Returns the team head element of Q without modifying the team head pointer
	if (Q.front != Q.rear)			//Queue is not empty
		return Q.front->next->data; //Returns the value of the queue head element, with the queue head pointer unchanged.
}

6. Implementation of basic operation of circular queue

#include<iostream>
using namespace std;
#define MAXQSIZE 10
typedef struct SqQueue {
	int *base;
	int front;
	int rear;
}SqQueue;
//Initialization
void InitQueue(SqQueue &Q) {
	Q.base = new int[MAXQSIZE];
	if (!Q.base) {
		cout << "initialization failed" << endl;
		exit(-2);
	}
	else {
		Q.front = Q.rear = 0;
		cout << "Initialization successful" << endl;
	}
}
//Join the team
void EnQueue(SqQueue &Q, int e)
{
	if ((Q.rear + 1) % MAXQSIZE == Q.front) {
		cout << "Team full, team failed!" << endl;
	}
	else {
		Q.base[Q.rear] = e;
		Q.rear = (Q.rear+ 1) % MAXQSIZE;
	}
}
//Team out
void DeQueue(SqQueue &Q) {
	if (Q.front == Q.rear) {
		cout << "Team empty, can't leave the team!" << endl;
	}
	else {
		cout << "The outgoing elements are:" << Q.base[Q.front] << endl;
		Q.front = (Q.front + 1) % MAXQSIZE;
		cout << "Team success" << endl;
	}
}
//Take out team leader element
void  GetHead(SqQueue Q)
{
	if (Q.front != Q.rear)
		cout << Q.base[Q.front] << endl;
}
int main() {
	SqQueue Q;
	int e;
	cout << "Initialization" << endl;
	InitQueue(Q);
	cout << "1 . Join the team" << endl;
	for (int i = 0; i < 9; i++) {
		cin >> e;
		EnQueue(Q, e);
	}
	cout << "2 . Team out" << endl;
	DeQueue(Q); 
		cout << "Take out team leader element" << endl;
	GetHead(Q);
	cout << "Remaining elements out of the team" << endl;
	for (int i = 0; i < 8; i++) {
		DeQueue(Q);
	}
	cout << " Team out" << endl;
	DeQueue(Q);
	return 0;
}

7. Realization of basic operation of chain team

#include<iostream>
using namespace std;
typedef  int  QElemType;
typedef struct QNode {
	QElemType data;
	struct QNode *next;
}QNode,*QueuePtr;
typedef struct {
	QueuePtr front;
	QueuePtr rear;
}LinkQueue;
//Initialization
void  IntitQueue(LinkQueue &Q){
	Q.front = Q.rear = new QNode;
	Q.front->next = NULL;
	cout << "Initialization successful" << endl;
}
//Join the team
void EnQueue(LinkQueue &Q, QElemType e) {
	struct QNode *p;
	p = new QNode;
	p->data = e;
	p->next = NULL; Q.rear->next = p;
	Q.rear = p;
}
//Team out
void DeQueue(LinkQueue &Q, QElemType &e) {
	if (Q.front == Q.rear) {
		cout << "Team empty" << endl;
		return;
	}
	struct QNode *p;
	p = Q.front->next;
	e = p->data;
	Q.front->next = p->next;
	if (Q.rear == p)	Q.rear = Q.front;
	delete p;
	cout << e << " ";
}
//Take out team leader element
void  GetHead(LinkQueue Q) {
	if (Q.front != Q.rear)
		cout << Q.front->next->data << endl;
}
int main() {
	LinkQueue Q;
	int e;
	cout << "Initialize queue" << endl;
	IntitQueue(Q);
	cout << "1 . Join the team" << endl;
	for (int i = 0; i < 10; i++) {
		cin >> e;
		EnQueue(Q, e);
	}
	cout << "2 .Team out" << endl;
	DeQueue(Q, e);
	cout << endl;
	cout << "3 . Take out team leader element" << endl;
	GetHead(Q);
	cout << "Take out the remaining elements" << endl;
	for (int i = 0; i < 9; i++) {
		DeQueue(Q, e);
	}
	cout << endl;
	cout << "Team out" << endl;
	DeQueue(Q, e);
	return 0;
}
14 original articles published, 34 praised, 2167 visited
Private letter follow

Tags: less

Posted on Thu, 16 Jan 2020 04:20:36 -0500 by kliopa10