20200120 circular queue array implementation of data structures and algorithms of

An array of circular queue for use at small amount of data that case, you can quickly achieve elements into the team and the team. The team and the team follow the principle of first in first out (FIFO) is. Structure is composed as follows:

typedef int datatype;
typedef struct _loopQueue {
	datatype* data;
	int front;
	int rear;
}loopQueue;

FIG wherein basic operations are as follows:
Here Insert Picture Descriptioncycle team included team, the team after the end of a circular shift lq-> rear = (lq-> rear + 1)% MAX_SIZE
cycle teams listed team after the team cyclic shift head lq-> front = (lq -> front + 1) & MAX_SIZE
circular queue empty team lq-> front = lq-> rear = 0; // initializes point to the same position when the
circular queue is full (lq-> rear + 1)% MAX_SIZE = lq-> front
of the queue length calculation, two cases: If lq-> rear> lq-> front
queue length lq-> rear-lq-> front; if lq-> rearfront, the queue length is MAX_SZIE-lq-> front-lq-> rear), i.e. lq-> rear-lq-> front + MAX_SIZE;
uniform length of the above two cases (lq-> rear-lq-> front + MAX_SIZE)% MAX_SIZE
complete code to achieve the following:

#include<Windows.h>
#include<iostream>
using namespace std;
#define MAX_SZIE 5
typedef int datatype;
typedef struct _loopQueue {
	datatype* data;
	int front;
	int rear;
}loopQueue;
bool initLoopQueue(loopQueue* &lq);
bool isFull(loopQueue* &lq);
int getLength(loopQueue* &lq);
void loopQueuePrint(loopQueue*& lq);
bool loopQueueEnter(loopQueue*& lq, datatype data);
bool loopQueueDelete(loopQueue*& lq, datatype &data);

int main() {
	loopQueue* lq;
	if(!initLoopQueue(lq)) {
		cout << "初始化失败!" << endl;
		exit(1);
	}

	for (int i = 0; i < 10; i++) {
		if (loopQueueEnter(lq, i)) {
			cout << "入队成功:"<<i<<endl;
		}
		else {
			cout << "入队失败:" << i << endl;
		}
	}
	cout << "队列的长度为:" << getLength(lq)<<endl;

	int data;
	for (int i = 0; i < 2; i++) {
		if (loopQueueDelete(lq, data)) {
			cout << "出队成功:" << data << endl;
		}
		else {
			cout << "出队失败:"  << endl;
		}
	}

	for (int i = 10; i < 15; i++) {
		if (loopQueueEnter(lq, i)) {
			cout << "入队成功:" << i << endl;
		}
		else {
			cout << "入队失败:" << i << endl;
		}
	}
	cout << "队列的长度为:" << getLength(lq) << endl;
	loopQueuePrint(lq);
	system("pause");
	return 0;
}
bool initLoopQueue(loopQueue* &lq) {
	lq = new loopQueue;
	if (!lq) return false;
	lq->data = new datatype[MAX_SZIE];
	memset(lq->data, 0, MAX_SZIE);
	lq->front = lq->rear = 0;
	return true;
}
bool isFull(loopQueue*& lq) {
	if (!lq) return false;
	if ((lq->rear + 1) % MAX_SZIE == lq->front)
		return true;
	else 
		return false;
}
int getLength(loopQueue*& lq) {
	if (!lq) return false;
	return (lq->rear - lq->front + MAX_SZIE) % MAX_SZIE;
}
void loopQueuePrint(loopQueue* &lq) {
	if (!lq || lq->front == lq->rear) return;
	int tmp = lq->front;
	while (1) {		
		datatype num = lq->data[tmp];
		cout << num << " ";
		tmp=(tmp+1)% MAX_SZIE;
		if (tmp == lq->rear) break;
	}
}
bool loopQueueEnter(loopQueue*& lq, datatype data) {
	if (!lq || isFull(lq)) return false;
	lq->data[lq->rear] = data;
	lq -> rear = (lq->rear + 1) % MAX_SZIE;
	return true;
}
bool loopQueueDelete(loopQueue*& lq, datatype& data) {
	if (!lq || lq->front == lq->rear) return false;
	data = lq->data[lq->front];
	lq->front = (lq->front + 1) % MAX_SZIE;
	return true;

}
Published 53 original articles · won praise 0 · Views 2094

Guess you like

Origin blog.csdn.net/weixin_40071289/article/details/104050208