数据结构—循环队列操作及实现(CC++)

数据结构—循环队列操作及实现(C/C++)

文章目录

  • 数据结构—循环队列操作及实现(C/C++)
      • 1.0初始化定义
      • 1.1 循环队列的结构体定义
      • 1.2循环队列的初始化
      • 1.3求循环队列的长度
      • 1.4循环队列的入队操作
      • 1.5循环队列的出队操作
      • 1.6取循环队列对头元素
      • 1.7输出循环队列函数
      • 1.8 C 主函数代码
      • 1.9 C++主函数代码
      • 2.0 C完整代码
      • 2.1 C++完整代码
      • 2.2运行截图
      • 2.3参考书籍
      • 2.4总结

1.0初始化定义

#include
#include
#define MAXSIZE 1000    //定义数组的最大长度
#define OK 1
#define OVERFLOW -1
#define ERROR -1
typedef int QElemType;    // 自己定义的类型
typedef int status;      //  上同 
using namespace std;

1.1 循环队列的结构体定义

typedef struct
{
	QElemType* base; //存储空间的基地址
	int front; //头指针
	int rear; //尾指针
}SqQueue;

1.2循环队列的初始化

status InitQueue(SqQueue *Q)
{
	Q->base = new QElemType[MAXSIZE]; //为队列分配一个容量为MAXSIZE的数组空间
	if(!Q->base)                      //判断分配空间失败情况
	exit(OVERFLOW);
	Q->front = Q->rear=0;  //将头指针和尾指针置空,队列为空
	return OK;
}

1.3求循环队列的长度

status QueueLength(SqQueue *Q)
{
	cout<rear - Q->front + MAXSIZE) % MAXSIZE;
	//求循环队列的长度公式
}

1.4循环队列的入队操作

status EnQueue(SqQueue*Q, QElemType e) 
{
	if((Q->rear+1)%MAXSIZE==(Q->front))//判断队尾指针是否等于队头指针
	return ERROR;
 	Q->base[Q->rear]=e;  //讲元素进入队尾
	Q->rear=(Q->rear+1)%MAXSIZE; //队尾指针+1
	return OK;
}

1.5循环队列的出队操作

status DeQueue(SqQueue *Q,QElemType *e)
{
	if(Q->front==Q->rear) //判断队尾头是否等于队尾
	return ERROR;
	*e=Q->base[Q->front];   //保存出队的元素值
	Q->front=(Q->front+1)%MAXSIZE;  //队头指针加1
	return OK;  //返回出队元素
}

1.6取循环队列对头元素

status GetHead(SqQueue *Q,QElemType *e) //个人习惯用e值带回
{
	cout<front==Q->rear)    
	{
		return ERROR;
	}
	else
	{
	return *e=Q->base[Q->front];
	}
}

1.7输出循环队列函数

status PrintQueue(SqQueue *Q) 
{
	cout<front; i < Q->rear; ++i) //从队头打印到队尾
         cout<<"\40"<base[i];
     return OK;
}

1.8 C 主函数代码

int main()
{
	int n,m,t;
	QElemType e;
	SqQueue Q;
	InitQueue(&Q);
	printf("请输入需要入队的次数:");
	scanf("%d",&n);
	for(int i=1 ;i<=n;i++)
	{
		printf("请输入第%d个元素的值:",i);
		scanf("%d",&m);
		EnQueue(&Q, m);
	}
	PrintQueue(&Q);
	printf("%d",GetHead(&Q,&e));
	printf("%d",QueueLength(&Q));
	printf("\n");
	printf("\n");
	printf("请输入需要出队的次数:");
	scanf("%d",&t);
	for(int i=1;i<=t;i++)
	{
		DeQueue(&Q,&e);
	}
	PrintQueue(&Q);
	printf("%d",QueueLength(&Q));
	return 0;
}

1.9 C++主函数代码

int main()
{
	int n,m,t;
	QElemType e;
	SqQueue Q;
	InitQueue(&Q);
	cout<<"请输入需要入队的次数:";
	cin>>n;
	for(int i=1 ;i<=n;i++)
	{
	cout<<"请输入第"<>m;
	EnQueue(&Q, m);
	}
	PrintQueue(&Q);
	cout<>t;
	for(int i=1;i<=t;i++)
	{
		DeQueue(&Q,&e);
	}
	PrintQueue(&Q);
	cout<

2.0 C完整代码

#include
#include
#define MAXSIZE 1000    //定义数组的最大长度
#define OK 1
#define OVERFLOW -1
#define ERROR -1
typedef int QElemType;    // 自己定义的类型
typedef int status;      //  上同 
//队列的顺序存储结构
typedef struct
{
	QElemType* base; //存储空间的基地址
	int front; //头指针
	int rear; //尾指针
}SqQueue;

//循环队列的初始化
status InitQueue(SqQueue *Q)
{
	
	Q->base =(QElemType*)malloc(MAXSIZE*sizeof(QElemType)); //为队列分配一个容量为MAXSIZE的数组空间
	if(!Q->base)                      //判断分配空间失败情况
	exit(OVERFLOW);
	Q->front = Q->rear=0;  //将头指针和尾指针置空,队列为空
	return OK;
}

//求队列的长队
status QueueLength(SqQueue *Q)
{
	printf("\n");
	printf("当前队列长度:");
	return (Q->rear - Q->front + MAXSIZE) % MAXSIZE;
	//求循环队列的长度公式
}

//循环队列入队
status EnQueue(SqQueue*Q, QElemType e) 
{
	if((Q->rear+1)%MAXSIZE==(Q->front))//判断队尾指针是否等于队头指针
	return ERROR;
 	Q->base[Q->rear]=e;  //讲元素进入队尾
	Q->rear=(Q->rear+1)%MAXSIZE; //队尾指针+1
	return OK;
}
//循环队列的出队
status DeQueue(SqQueue *Q,QElemType *e)
{
	if(Q->front==Q->rear) //判断队尾头是否等于队尾
	return ERROR;
	*e=Q->base[Q->front];   //保存出队的元素值
	Q->front=(Q->front+1)%MAXSIZE;  //队头指针加1
	return OK;  //返回出队元素
}

//取队头元素
status GetHead(SqQueue *Q,QElemType *e) //个人习惯用e值带回
{
	printf("\n");
	printf("当前队头元素:");
	if(Q->front==Q->rear)    
	{
		return ERROR;
	}
	else
	{
	return *e=Q->base[Q->front];
	}
}

//输出队列元素
status PrintQueue(SqQueue *Q) 
{
	printf("\n");
	printf("当前队列为:");
     for (int i = Q->front; i < Q->rear; ++i) //从队头打印到队尾
         printf("%4d",Q->base[i]);
     return OK;
}

int main()
{
	int n,m,t;
	QElemType e;
	SqQueue Q;
	InitQueue(&Q);
	printf("请输入需要入队的次数:");
	scanf("%d",&n);
	for(int i=1 ;i<=n;i++)
	{
		printf("请输入第%d个元素的值:",i);
		scanf("%d",&m);
		EnQueue(&Q, m);
	}
	PrintQueue(&Q);
	printf("%d",GetHead(&Q,&e));
	printf("%d",QueueLength(&Q));
	printf("\n");
	printf("\n");
	printf("请输入需要出队的次数:");
	scanf("%d",&t);
	for(int i=1;i<=t;i++)
	{
		DeQueue(&Q,&e);
	}
	PrintQueue(&Q);
	printf("%d",GetHead(&Q,&e));
	printf("%d",QueueLength(&Q));
	return 0;
}

2.1 C++完整代码

#include
#include
#define MAXSIZE 1000    //定义数组的最大长度
#define OK 1
#define OVERFLOW -1
#define ERROR -1
typedef int QElemType;    // 自己定义的类型
typedef int status;      //  上同 
using namespace std;
//队列的顺序存储结构
typedef struct
{
	QElemType* base; //存储空间的基地址
	int front; //头指针
	int rear; //尾指针
}SqQueue;

//循环队列的初始化
status InitQueue(SqQueue *Q)
{
	Q->base = new QElemType[MAXSIZE]; //为队列分配一个容量为MAXSIZE的数组空间
	if(!Q->base)                      //判断分配空间失败情况
	exit(OVERFLOW);
	Q->front = Q->rear=0;  //将头指针和尾指针置空,队列为空
	return OK;
}

//求队列的长队
status QueueLength(SqQueue *Q)
{
	cout<rear - Q->front + MAXSIZE) % MAXSIZE;
	//求循环队列的长度公式
}

//循环队列入队
status EnQueue(SqQueue*Q, QElemType e) 
{
	if((Q->rear+1)%MAXSIZE==(Q->front))//判断队尾指针是否等于队头指针
	return ERROR;
 	Q->base[Q->rear]=e;  //讲元素进入队尾
	Q->rear=(Q->rear+1)%MAXSIZE; //队尾指针+1
	return OK;
}
//循环队列的出队
status DeQueue(SqQueue *Q,QElemType *e)
{
	if(Q->front==Q->rear) //判断队尾头是否等于队尾
	return ERROR;
	*e=Q->base[Q->front];   //保存出队的元素值
	Q->front=(Q->front+1)%MAXSIZE;  //队头指针加1
	return OK;  //返回出队元素
}

//取队头元素
status GetHead(SqQueue *Q,QElemType *e) //个人习惯用e值带回
{
	cout<front==Q->rear)    
	{
		return ERROR;
	}
	else
	{
	return *e=Q->base[Q->front];
	}
}

//输出队列元素
status PrintQueue(SqQueue *Q) 
{
	cout<front; i < Q->rear; ++i) //从队头打印到队尾
         cout<<"\40"<base[i];
     return OK;
}

int main()
{
	int n,m,t;
	QElemType e;
	SqQueue Q;
	InitQueue(&Q);
	cout<<"请输入需要入队的次数:";
	cin>>n;
	for(int i=1 ;i<=n;i++)
	{
	cout<<"请输入第"<>m;
	EnQueue(&Q, m);
	}
	PrintQueue(&Q);
	cout<>t;
	for(int i=1;i<=t;i++)
	{
		DeQueue(&Q,&e);
	}
	PrintQueue(&Q);
	cout<

2.2运行截图

数据结构—循环队列操作及实现(CC++)_第1张图片

2.3参考书籍

《数据结构》(C语言版)(第2版)—严蔚敏 李冬梅 吴伟民 编著

2.4总结

多多关注留言!

你可能感兴趣的:(循环队列操作,数据结构,队列,数据结构)