Knowledge summary and code implementation of sequence team

Keywords: data structure queue

catalogue

Storage mode of sequential queue

Characteristics of sequential queue storage

Definition of empty and full teams

Methods to solve false overflow

Sequential queue function

Code implementation of acyclic sequential queue

Cyclic sequential queue

Judgment method of solving queue full by cyclic sequential queue

Cyclic sequential queue code implementation

Storage mode of sequential queue

     The sequential storage structure is exactly the same as that of the general linear table. A group of storage units with continuous addresses are used to store the data elements from the head of the team to the tail of the team in turn.

Characteristics of sequential queue storage

         Simple, convenient, but easy to overflow (fixed array size)

Definition of empty and full teams

        front == rear   It's a sign that the team is empty

          rear == MAXSIZE   It's a sign that the team is full.

Methods to solve false overflow

1. Move the elements in the team to the team head in turn.
2. Imagine the queue space as a circular table, that is, the m storage units allocated to the queue can be recycled. When the rear is MAXSIZE,   If the beginning of the Q.front vector is empty, you can use the empty space from scratch. When front is MAXSIZE  , The same is true.

         The above two methods can solve the problem of false overflow, but through comparison, it can be seen that the disadvantage of the first method is a waste of time. Every time you move, the elements in the team will move, so you generally choose the second method.

Sequential queue function

bool InitQueue(SqQueue &Q);                                 // 1. Team initialization

bool QueueEmpty(SqQueue Q);                              // 2. Judge whether it is empty

int QueueLength(SqQueue Q);                                // 3. Find the length of the team

bool DestroyQueue(SqQueue &Q);                         // 4. Destruction team

void ClearQueue(SqQueue &Q);                             // 5. Empty the sequence team

bool EnQueue(SqQueue &Q,ElemType e);              // 6. Join the team

bool DeQueue(SqQueue &Q,ElemType &e);           // 7. Out of the team

bool GetHead(SqQueue Q,ElemType &e);              // 8. Get team leader element  

Code implementation of acyclic sequential queue

#include<bits/stdc++.h>
using namespace std;
#define MAXSIZE 100
typedef int ElemType;
typedef struct SqQueue {
	ElemType *base;
	int front;
	int rear;
}SqQueue;

//Initialize sequence queue 
bool InitQueue(SqQueue &Q) {
	Q.front = Q.rear = 0;
	Q.base = new ElemType[MAXSIZE];
	if(!Q.base)  //Storage allocation failed 
		exit(-1);
	return true;
}

//Determine whether the queue is empty 
bool QueueEmpty(SqQueue Q) {
	if(Q.front == Q.rear)
		return true;
	return false;
}

//Destroy queue
bool DestroyQueue(SqQueue &Q) {
	if(Q.base) {
		delete Q.base;
		Q.front = Q.rear = NULL;
	}
} 

//Empty queue
bool ClearQueue(SqQueue &Q) {
	if(Q.base) {
		Q.rear = Q.front;
	}
} 

//Find queue length 
int QueueLength(SqQueue Q) {
	return Q.rear - Q.front;
}

//Join the team 
bool EnQueue(SqQueue &Q,ElemType e) {
	if(Q.rear == MAXSIZE)
		return false;
	Q.base[Q.rear++] = e;
	return true;
} 

//Out of the team 
bool DeQueue(SqQueue &Q,ElemType &e) {
	if(Q.rear == Q.front)
		return false;
	e = Q.base[Q.front++];
	return true;
} 

//Get queue header element 
bool GetHead(SqQueue Q,ElemType &e) {
	if(Q.rear == Q.front)
		return false;
	e = Q.base[Q.front];
	return true;
} 
 
int main() {
	int n;
	ElemType e;
	SqQueue que;
	InitQueue(que);
	cout << "The queue has been initialized" << endl;
	cout << "Please enter the number of elements in the queue:" << endl;
	cin >> n;
	cout << "Please enter the elements in the queue in turn:" << endl; 
	for(int i = 0; i < n; i++) {	
		cin >> e;
		EnQueue(que, e); 
	} 
	cout << "At this time, the length of the queue is:" << QueueLength(que) << endl;
	cout << "Take out all elements in the queue:"; 
	for(int i = 0; i < n; i++) {
		DeQueue(que, e);
		cout << e << " ";
	}
	cout << endl;
	cout << "At this time, the length of the queue is:" << QueueLength(que) << endl;
	cout << "Add an element to the queue:" << endl;
	cin >> e;
	EnQueue(que, e);
	cout << "At this time, the team head element is:" << e << endl;
	GetHead(que, e);
	cout << "At this time, the length of the queue is:" << QueueLength(que) << endl;
	cout << "Empty queue" << endl;
	ClearQueue(que);
	cout << "At this time, the length of the queue is:" << QueueLength(que) << endl;
	DestroyQueue(que);
	return 0;
}

Cyclic sequential queue

         In the above sequence teams, it is found that the space of sequence teams is sometimes not fully utilized, and there will be false overflow. In order to solve the false overflow, it is proposed to use circular sequence teams to prevent it.

As shown in the figure below, base[0] is connected to base[MAXQSIZE]  - 1] After that, if rear+   1==M, then make rear=0; A cyclic sequential queue is a cyclic use of the storage space allocated for the queue.

         The way to implement the cyclic sequence team is to use the module,   In C language:  %) Therefore, the difference between the circular sequence team and the sequence team in the operation of inserting and deleting elements is that it needs to perform one more modular operation:

  Insert element:  

        Q.base[Q.rear]=x;

        Q.rear=(Q.rear+ 1)% MAXQSIZE;

Delete element:

        x=Q.base[s.front];

        Q.front=(Q.front+ 1)% MAXQSIZE

Judgment method of solving queue full by cyclic sequential queue

The problem should be found in the above figure, that is, the judgment conditions of empty and full teams are front == rear. Obviously, this is not feasible, so there are three solutions, as follows:

         1. Another sign shall be set to distinguish between empty and full teams

         2. Set another variable to record the number of elements

         3. Use less one primeless space

Generally, the third method is adopted. Therefore, the third method is used to realize the cyclic sequence team. The diagram is as follows:

  At this time, the condition of empty queue is still front == rear

The condition of full queue is (rear + 1)% maxsize = = front

Cyclic sequential queue code implementation

#include<bits/stdc++.h>
using namespace std;
#define MAXSIZE 10
typedef int ElemType;
typedef struct SqQueue {
	ElemType *base;
	int front;
	int rear;
}SqQueue;

//Initialize cyclic sequence queue 
bool InitQueue(SqQueue &Q) {
	Q.front = Q.rear = 0;
	Q.base = new ElemType[MAXSIZE];
	if(!Q.base)  //Storage allocation failed 
		exit(-1);
	return true;
}

//Judge whether the loop sequence queue is empty 
bool QueueEmpty(SqQueue Q) {
	if(Q.front == Q.rear)
		return true;
	return false;
}

//Destroy queue
bool DestroyQueue(SqQueue &Q) {
	if(Q.base)
		delete Q.base;
	Q.base = NULL;
	Q.front = Q.rear = 0;
} 

//Empty queue
void ClearQueue(SqQueue &Q) {
	Q.rear = Q.front = 0;
} 

//Find queue length 
int QueueLength(SqQueue Q) {
	return (Q.rear - Q.front + MAXSIZE) % MAXSIZE;
}

//Join the team 
bool EnQueue(SqQueue &Q, ElemType e) {
	if((Q.rear + 1) % MAXSIZE == Q.front)
		return false;
	Q.base[Q.rear] = e;
	Q.rear = (Q.rear + 1) % MAXSIZE;
	return true;
} 

//Out of the team 
bool DeQueue(SqQueue &Q, ElemType &e) {
	if(Q.rear == Q.front)
		return false;
	e = Q.base[Q.front];
	Q.front = (Q.front + 1) % MAXSIZE;
	return true;
} 

//Get queue header element 
bool GetHead(SqQueue Q, ElemType &e) {
	if(Q.rear == Q.front)
		return false;
	e = Q.base[Q.front];
	return true;
} 
 
int main() {
	int n;
	ElemType e;
	SqQueue que;
	InitQueue(que);
	cout << "The queue has been initialized" << endl;
	cout << "Please enter the number of elements in the queue:" << endl;
	cin >> n;
	cout << "Please enter the elements in the queue in turn:" << endl; 
	for(int i = 0; i < n; i++) {	
		cin >> e;
		EnQueue(que, e); 
	} 
	cout << "At this time, the length of the queue is:" << QueueLength(que) << endl;
	cout << "Take out all elements in the queue:"; 
	for(int i = 0; i < n; i++) {
		DeQueue(que, e);
		cout << e << " ";
	}
	cout << endl;
	cout << "At this time, the length of the queue is:" << QueueLength(que) << endl;
	cout << "Add an element to the queue:" << endl;
	cin >> e;
	EnQueue(que, e);
	cout << "At this time, the team head element is:" << e << endl;
	GetHead(que, e);
	cout << "At this time, the length of the queue is:" << QueueLength(que) << endl;
	cout << "Empty queue" << endl;
	ClearQueue(que);
	cout << "At this time, the length of the queue is:" << QueueLength(que) << endl;
	DestroyQueue(que);
	return 0;
}

Posted by BryonS on Fri, 29 Oct 2021 23:52:17 -0700