Article directory
sequential queue
Structure definition
#define MAXSIZE 100
typedef int ElemType;
typedef struct{
ElemType data[MAXSIZE];
int front;//队头指针
int rear;//队尾指针
}SqQueue;
initialization
bool InitQueue(SqQueue &Q){
Q.front=Q.rear=0;//队头和队尾指针都指向0
}
Empty
bool IsEmpty(SqQueue &Q){
return (Q.front==Q.rear)?true:false;
}
enqueue
bool EnQueue(SqQueue &Q,ElemType x){
//队头指针跟队尾指针相遇,即代表队满
if((Q.rear+1)% MAXSIZE==Q.front){
cout<<"队满,入队失败!"<<endl;
return false;
}
//入队的时候,队头指针没有变,占据一个存储空间
Q.data[Q.rear]=x;//将元素x插入队尾
Q.rear=(Q.rear+1)% MAXSIZE;//队尾+1
//mod队长是为了让它从队尾在重新回到队头
return true;
}
dequeue
bool DeQueue(SqQueue &Q,ElemType &x){
if(IsEmpty(Q)){
cout<<"队空,出队失败!";
return false;
}
x=Q.data[Q.front];//出队
Q.front=(Q.front+1)% MAXSIZE;
return true;
}
Get the first element of the queue
bool GetHead(SqQueue &Q,ElemType &x) {
if(IsEmpty(Q)){
cout<<"队空,获取失败!";
return false;
}
x=Q.data[Q.front];//出队
return true;
}
Code
#include<iostream>
using namespace std;
#define MAXSIZE 100
typedef int ElemType;
typedef struct{
ElemType data[MAXSIZE];
int front;//队头指针
int rear;//队尾指针
}SqQueue;
/*初始化队列*/
bool InitQueue(SqQueue &Q){
Q.front=Q.rear=0;//队头和队尾指针都指向0
}
/*判断队列是否为空*/
bool IsEmpty(SqQueue &Q){
return (Q.front==Q.rear)?true:false;
}
/*入队*/
bool EnQueue(SqQueue &Q,ElemType x){
//队头指针跟队尾指针相遇,即代表队满
if((Q.rear+1)% MAXSIZE==Q.front){
cout<<"队满,入队失败!"<<endl;
return false;
}
//入队的时候,队头指针没有变,占据一个存储空间
Q.data[Q.rear]=x;//将元素x插入队尾
Q.rear=(Q.rear+1)% MAXSIZE;//队尾+1
//mod队长是为了让它从队尾在重新回到队头
return true;
}
/*出队*/
bool DeQueue(SqQueue &Q,ElemType &x){
if(IsEmpty(Q)){
cout<<"队空,出队失败!";
return false;
}
x=Q.data[Q.front];//出队
Q.front=(Q.front+1)% MAXSIZE;
return true;
}
/*获得队头元素*/
bool GetHead(SqQueue &Q,ElemType &x) {
if(IsEmpty(Q)){
cout<<"队空,获取失败!";
return false;
}
x=Q.data[Q.front];//出队
return true;
}
void PrintQueue(SqQueue &Q){
int js=1;
for(int i=Q.front;i<MAXSIZE;i++){
cout<<Q.data[i]<<" ";
if(!(js%5))cout<<endl;
js++;
}
cout<<endl;
}
int main(){
SqQueue Q;
InitQueue(Q);
if(IsEmpty(Q))cout<<"空队列"<<endl;
else cout<<"非空队列"<<endl;
for(int i=1;i<=100;i++)
EnQueue(Q,i);
cout<<"初始化后的队列为:"<<endl;
PrintQueue(Q);
cout<<"当前出队列的元素为:";
int x;
DeQueue(Q,x);
cout<<x<<endl;
cout<<x<<"出队列之后,当前的队首元素为:" ;
GetHead(Q,x);
cout<<x<<endl;
return 0;
}
operation result:
Chain queue (lead node)
Structure definition
typedef int ElemType;
typedef struct LinkNode{
//链队列的结点结构体
ElemType data;
struct LinkNode *next;
}LinkNode;
typedef struct{
//链队列结构体
LinkNode *front;//队头指针
LinkNode *rear;//队尾指针
}LinkQueue;
initialization
/*初始化链队列(带头结点)*/
void InitQueue(LinkQueue &Q) {
//初始化队头指针和队尾指针都指向头结点
Q.front=Q.rear=(LinkNode *)malloc(sizeof(LinkNode));
Q.front->next=NULL;//队头指针的下一个节点指向空
}
Judging that the team is empty
/*判断队列是否为空(带头结点)*/
bool isEmpty(LinkQueue &Q) {
return (Q.front==Q.rear)?true:false;
}
enqueue
/*入队(带头结点)*/
void EnQueue(LinkQueue &Q,ElemType x){
//1.申请结点
LinkNode *s=(LinkNode *)malloc(sizeof(LinkNode));
//2.装入数据
s->data=x;
//3.修改当前申请结点的指向
s->next=NULL;//保证队尾结点的指向为空
//4.修改队尾指针的指向
Q.rear->next=s;
//5.修改表尾指向
Q.rear=s;
}
dequeue
/*出队(带头结点),带回所删除的数据*/
bool DeQueue(LinkQueue &Q,ElemType &x){
//1.判断队列是否为空
if(isEmpty(Q)){
cout<<"空队列!"<<endl;
return false;
}
//2.申请结点指向头结点之后的数据
LinkNode *p=Q.front->next;
x=p->data;
//3.修改当前队头指向
Q.front->next=p->next;
//4.判断当前删除的结点是否为队列中的最后一个结点
if(Q.rear==p) {
Q.front=Q.rear;//修改队头队尾指针
}
free(p);
return true;
}
Get the first element of the queue
/*获得队首元素(带头结点)*/
bool GetHead(LinkQueue &Q,ElemType &x){
if(isEmpty(Q)){
cout<<"空队列!"<<endl;
return false;
}
LinkNode *p=Q.front->next;
x=p->data;
return true;
}
output queue element
/*输出队列的元素(带头结点)*/
void PrintQueue(LinkQueue &Q) {
LinkNode *p=Q.front->next;
while(p){
cout<<p->data<<" ";
p=p->next;
}
cout<<endl;
}
Chain queue (without leader node)
initialization
/*初始化队列(不带头结点)*/
void InitQueueN(LinkQueue &Q) {
//初始化 队头和队尾都指向NULL
Q.front=NULL;
Q.rear=NULL;
}
Judging that the team is empty
/*判断队列是否为空(不带头结点)*/
bool isEmptyN(LinkQueue &Q) {
return (Q.front==NULL)?true:false;
}
enqueue
/*入队(不带头结点)*/
void EnQueueN(LinkQueue &Q,ElemType x){
//1.申请结点
LinkNode *s=(LinkNode *)malloc(sizeof(LinkNode));
//2.装入数据
s->data=x;
//3.修改当前申请结点的指向
s->next=NULL;//保证队尾结点的指向为空
//由于没有头结点,所以需要单独一套逻辑处理第一个结点
if(Q.front==NULL){
//将头结点和尾结点的指向指向新申请的结点
Q.front=s;
Q.rear=s;
}else{
//4.修改队尾指针的指向
Q.rear->next=s;
//5.修改表尾指向
Q.rear=s;
}
}
dequeue
bool DeQueueN(LinkQueue &Q,ElemType &x){
//1.判断队列是否为空
if(isEmptyN(Q)){
cout<<"空队列!"<<endl;
return false;
}
//2.申请结点指向头结点
LinkNode *p=Q.front;
x=p->data;
//3.修改当前队头指向
Q.front=p->next;
//4.判断当前删除的结点是否为队列中的最后一个结点
if(Q.rear==p) {
//修改队头队尾指针
Q.front=NULL;
Q.rear=NULL;
}
free(p);
return true;
}
Get the first element of the queue
/*获得队首元素(不带头结点)*/
bool GetHeadN(LinkQueue &Q,ElemType &x){
if(isEmptyN(Q)){
cout<<"空队列!"<<endl;
return false;
}
LinkNode *p=Q.front;
x=p->data;
return true;
}
output queue element
/*输出队列的元素(不带头结点)*/
void PrintQueueN(LinkQueue &Q) {
LinkNode *p=Q.front;
while(p){
cout<<p->data<<" ";
p=p->next;
}
cout<<endl;
}
Code
#include<iostream>
#include<stdlib.h>
using namespace std;
typedef int ElemType;
typedef struct LinkNode{
//链队列的结点结构体
ElemType data;
struct LinkNode *next;
}LinkNode;
typedef struct{
//链队列结构体
LinkNode *front;//队头指针
LinkNode *rear;//队尾指针
}LinkQueue;
/*初始化链队列(带头结点)*/
void InitQueue(LinkQueue &Q) {
//初始化队头指针和队尾指针都指向头结点
Q.front=Q.rear=(LinkNode *)malloc(sizeof(LinkNode));
Q.front->next=NULL;//队头指针的下一个节点指向空
}
/*判断队列是否为空(带头结点)*/
bool isEmpty(LinkQueue &Q) {
return (Q.front==Q.rear)?true:false;
}
/*初始化队列(不带头结点)*/
void InitQueueN(LinkQueue &Q) {
//初始化 队头和队尾都指向NULL
Q.front=NULL;
Q.rear=NULL;
}
/*判断队列是否为空(不带头结点)*/
bool isEmptyN(LinkQueue &Q) {
return (Q.front==NULL)?true:false;
}
/*入队(带头结点)*/
void EnQueue(LinkQueue &Q,ElemType x){
//1.申请结点
LinkNode *s=(LinkNode *)malloc(sizeof(LinkNode));
//2.装入数据
s->data=x;
//3.修改当前申请结点的指向
s->next=NULL;//保证队尾结点的指向为空
//4.修改队尾指针的指向
Q.rear->next=s;
//5.修改表尾指向
Q.rear=s;
}
/*入队(不带头结点)*/
void EnQueueN(LinkQueue &Q,ElemType x){
//1.申请结点
LinkNode *s=(LinkNode *)malloc(sizeof(LinkNode));
//2.装入数据
s->data=x;
//3.修改当前申请结点的指向
s->next=NULL;//保证队尾结点的指向为空
//由于没有头结点,所以需要单独一套逻辑处理第一个结点
if(Q.front==NULL){
//将头结点和尾结点的指向指向新申请的结点
Q.front=s;
Q.rear=s;
}else{
//4.修改队尾指针的指向
Q.rear->next=s;
//5.修改表尾指向
Q.rear=s;
}
}
/*出队(带头结点),带回所删除的数据*/
bool DeQueue(LinkQueue &Q,ElemType &x){
//1.判断队列是否为空
if(isEmpty(Q)){
cout<<"空队列!"<<endl;
return false;
}
//2.申请结点指向头结点之后的数据
LinkNode *p=Q.front->next;
x=p->data;
//3.修改当前队头指向
Q.front->next=p->next;
//4.判断当前删除的结点是否为队列中的最后一个结点
if(Q.rear==p) {
Q.front=Q.rear;//修改队头队尾指针
}
free(p);
return true;
}
/*出队(不带头结点),带回所删除的数据*/
bool DeQueueN(LinkQueue &Q,ElemType &x){
//1.判断队列是否为空
if(isEmptyN(Q)){
cout<<"空队列!"<<endl;
return false;
}
//2.申请结点指向头结点
LinkNode *p=Q.front;
x=p->data;
//3.修改当前队头指向
Q.front=p->next;
//4.判断当前删除的结点是否为队列中的最后一个结点
if(Q.rear==p) {
//修改队头队尾指针
Q.front=NULL;
Q.rear=NULL;
}
free(p);
return true;
}
/*获得队首元素(带头结点)*/
bool GetHead(LinkQueue &Q,ElemType &x){
if(isEmpty(Q)){
cout<<"空队列!"<<endl;
return false;
}
LinkNode *p=Q.front->next;
x=p->data;
return true;
}
/*获得队首元素(不带头结点)*/
bool GetHeadN(LinkQueue &Q,ElemType &x){
if(isEmptyN(Q)){
cout<<"空队列!"<<endl;
return false;
}
LinkNode *p=Q.front;
x=p->data;
return true;
}
/*输出队列的元素(带头结点)*/
void PrintQueue(LinkQueue &Q) {
LinkNode *p=Q.front->next;
while(p){
cout<<p->data<<" ";
p=p->next;
}
cout<<endl;
}
/*输出队列的元素(不带头结点)*/
void PrintQueueN(LinkQueue &Q) {
LinkNode *p=Q.front;
while(p){
cout<<p->data<<" ";
p=p->next;
}
cout<<endl;
}
int main(){
LinkQueue Q1;
cout<<"带头结点的队列基本操作:"<<endl;
InitQueue(Q1);
if(isEmpty(Q1)) cout<<"队空!"<<endl;
else cout<<"非队空!"<<endl;
for(int i=0;i<=20;i+=2){
EnQueue(Q1,i);
}
cout<<"队列初始化后的数据为:";
PrintQueue(Q1);
int x;
DeQueue(Q1,x);
cout<<"队头元素x出队为:" <<x<<endl;
cout<<"出队后的队列为:" ;
PrintQueue(Q1);
GetHead(Q1,x);
cout<<"当前的队首元素为:"<<x<<endl;
cout<<"=============================="<<endl;
LinkQueue Q2;
cout<<"不带头结点的队列基本操作:"<<endl;
InitQueueN(Q2);
if(isEmpty(Q2)) cout<<"队空!"<<endl;
else cout<<"非队空!"<<endl;
for(int i=1;i<22;i+=2){
EnQueueN(Q2,i);
}
cout<<"队列初始化后的数据为:";
PrintQueueN(Q2);
DeQueueN(Q2,x);
cout<<"队头元素x出队为:" <<x<<endl;
cout<<"出队后的队列为:" ;
PrintQueueN(Q2);
GetHeadN(Q2,x);
cout<<"当前的队首元素为:"<<x<<endl;
cout<<"=============================="<<endl;
}
operation result: