The basic operation and simple implementation circular queue

First, the introduction of

FIG implement the queues and

Order the queue represents - a one-dimensional array base [MAXQSIZE]

#define MAXQSIZE 100	//最大队列长
typedef struct{
Qelem *base;		//初始化的动态分配存储空间
int front;		//头指针,不是指针变量,若队列不空,只想队头元素
int rear;		//尾指针,不是指针变量,若队列不空,指向队尾元素的下一个位置
}SqQueue;

initial:

front=rear=0;//队空

Here Insert Picture Description
Into the team:

base[Q.rear]=e;//
rear++;	//指向下一空间

Here Insert Picture Description
The team:

e=base[Q.front];//出队
front++;
front=rear;//队空标志

Here Insert Picture Description
When the rear == MAXQSIZE, overflow
Here Insert Picture Description

  • If the front = 0, rear = MAXQSIZE, re-entry team - really overflow;
  • ! If the front = 0, rear = when MAXQSZIE, reentry team -False overflow

How to solve the problem of false overflow?

  1. The team moved to the head element, the disadvantage is a waste of time, the team must move elements
  2. The team space conceived as a circular table, that is allocated to the m memory cells can be recycled queues, whenrear = =MAXQSIZEWhen, if the starting end of the empty vector, but also re-use of empty space. whenfront= =MAXQSIZEWhen, it is the same.

Namely the introduction ofCircular queue
base [0] After the contact base [MAXQSIZE-1], if the rear + 1MAXQSIZE, so that the rear = 0;
Method: using
Mode (mod, i.e.%) == remainder operation.
Insert elements:

Q.base[Q.rear]=e;	//把值赋给尾指针所指空间
Q.rear=(Q.rear+1)%MAXQSIZE;//尾指针更新,+1%MAX=0

Removing elements:

e=Q.base[Q.front];//把头指针所指元素赋值给某个变量e
Q.front=(Q.front+1)%MAXQSIZE;//头指针位置的变化

Circular queue is not a circle, is made of imagination.
Therefore, circular queue: recycling queue allocated storage space.

Air Force team full judgment: less space element

  • Team empty conditions:Q.front = Q.rear(Also full team, but I can not distinguish, so with a little element of space)
  • Team full of conditions:(rear+1)%MAXQSIZE = Q.front
    Here Insert Picture Description

Second, the basic operation of the cyclic queue

Circular queue initialization

Status InitQueue(SqQueue &Q){
Q.base=new Qelemtype[MAXQSIZE]	//分配数组空间,base是首元素,是数组的地址,所以前面定义为指针变量,用C++语法简单
//C语法:用malloc函数
if(Q.base)
	exit(OVERFLOW);		//存储分配失败
Q.front=Q.rear=0;		//头尾指针置为0,队列为空
return OK;
}

Circular queue operation - request queue length

int Qlength(SqQueue Q){
return ((Q.rear-Q.front+MAXQSIZE)%MAXQSIZE);

Here Insert Picture DescriptionCycling team included Team

Status EnQueue(SqQueue &Q,int e){
if((Q.rear+1)%MAXQSIZE==Q.front)//队满
	return ERROR;
Q.base[Q.rear]=e; //把值赋给尾指针所指空间
Q.rear=(Q.rear+1)%MAXQSIZE;//尾指针更新,+1%MAX=0
return OK;

Team cycling team list

Status DeQueue(SqQueue &Q,int &e){
if(Q.front==Q.rear)	//队空
	return ERROR;
e=Q.base[Q.front];	//保存队头元素	
Q.front=(Q.front+1)%MAXQSIZE;	//队头指针+1
return OK;
}

Circular queues take the head elements

int GetHead(SqQueue Q){
if(Q.front!=Q.rear)	//队列不为空
	return Q.base[Q.front];	//返回队头指针元素的值,队头指针不变

Third, to achieve simple circular queue

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

bool InitQueue(SqQueue &Q){
 Q.base=new int[MAXQSIZE];
 if(!Q.base)
  exit(false);
 Q.front=Q.rear=0;
 return true;
}

int Qlength(SqQueue Q){
return ((Q.rear-Q.front+MAXQSIZE)%MAXQSIZE);
}

bool EnQueue(SqQueue &Q,int e){
if((Q.rear+1)%MAXQSIZE==Q.front)//队满
 return false;
Q.base[Q.rear]=e; //把值赋给尾指针所指空间
Q.rear=(Q.rear+1)%MAXQSIZE;//尾指针更新,+1%MAX=0
return true;
}

bool DeQueue(SqQueue &Q,int &e){
if(Q.front==Q.rear) //队空
 return false;
e=Q.base[Q.front]; //保存队头元素 
Q.front=(Q.front+1)%MAXQSIZE; //队头指针+1
return true;
}

int GetHead(SqQueue Q){
if(Q.front!=Q.rear) //队列不为空
 return Q.base[Q.front]; //返回队头指针元素的值,队头指针不变
}

int main()
{
 int n;
 SqQueue Q;
 cout<<"循环队列基本操作:"
	   <<"1、循环队列初始化\n"
	   <<"2、入队\n" 
	   <<"3、求队列长度\n"
	   <<"4、取队头元素\n"
	   <<"5、出队\n" ;
 while(1){
	
	  cout<<"输入操作序号:";
	  cin>>n; 
	  switch(n)
	  {
	   case 1: if(InitQueue (Q))
	      cout<<"循环队列初始化成功"<<endl;
	     else 
	      cout<<"初始化失败";
	     break; 
	   case 2:
	     for(int i=0;i<=10;i++)
	       EnQueue(Q,i);
	     cout<<"入队成功"<<endl; break;      
	   case 3:
	      cout<<"队列长度为"<<Qlength(Q)<<endl; break;//输出是9,因为队尾指针一开始变为1
	   case 4:
	     cout<<" 队头元素为:"<<GetHead(Q)<<endl;break; 
	   case 5:
	     
	     cout<<"出队,队列元素为:";
	     for(int i=0;i<10;i++)
	     {
	      DeQueue(Q,i);
	      cout<<i<<" ";
	  }
	     cout<<endl;break;  
	}
 }
}

Content Reference:
"data structure" Yan Wei Min
Qingdao University - Wang Zhuo

Published 34 original articles · won praise 85 · views 4616

Guess you like

Origin blog.csdn.net/weixin_45895026/article/details/103970214