当前位置:   article > 正文

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

c++ for循环合并

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

1.0初始化定义

#include<iostream>
#include<cstdlib>
#define MAXSIZE 1000    //定义数组的最大长度
#define OK 1
#define OVERFLOW -1
#define ERROR -1
typedef int QElemType;    // 自己定义的类型
typedef int status;      //  上同 
using namespace std;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

1.1 循环队列的结构体定义

typedef struct
{
	QElemType* base; //存储空间的基地址
	int front; //头指针
	int rear; //尾指针
}SqQueue;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

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
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

1.3求循环队列的长度

status QueueLength(SqQueue *Q)
{
	cout<<endl;
	cout<<"当前队列长度:";
	return (Q->rear - Q->front + MAXSIZE) % MAXSIZE;
	//求循环队列的长度公式
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

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
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

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
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

1.6取循环队列对头元素

status GetHead(SqQueue *Q,QElemType *e) //个人习惯用e值带回
{
	cout<<endl;
	cout<<"当前队头元素:";
	if(Q->front==Q->rear)    
	{
		return ERROR;
	}
	else
	{
	return *e=Q->base[Q->front];
	}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

1.7输出循环队列函数

status PrintQueue(SqQueue *Q) 
{
	cout<<endl;
   	cout<<"当前队列为:";
     for (int i = Q->front; i < Q->rear; ++i) //从队头打印到队尾
         cout<<"\40"<<Q->base[i];
     return OK;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

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
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29

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<<"请输入第"<<i<<"个元素的值:";
	cin>>m;
	EnQueue(&Q, m);
	}
	PrintQueue(&Q);
	cout<<GetHead(&Q,&e);
	cout<<QueueLength(&Q);
	cout<<endl<<endl;
	cout<<"请输入需要出队的次数:";
	cin>>t;
	for(int i=1;i<=t;i++)
	{
		DeQueue(&Q,&e);
	}
	PrintQueue(&Q);
	cout<<QueueLength(&Q);
	return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28

2.0 C完整代码

#include<stdio.h>
#include<stdlib.h>
#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;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110

2.1 C++完整代码

#include<iostream>
#include<cstdlib>
#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<<endl;
	cout<<"当前队列长度:";
	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值带回
{
	cout<<endl;
	cout<<"当前队头元素:";
	if(Q->front==Q->rear)    
	{
		return ERROR;
	}
	else
	{
	return *e=Q->base[Q->front];
	}
}

//输出队列元素
status PrintQueue(SqQueue *Q) 
{
	cout<<endl;
   	cout<<"当前队列为:";
     for (int i = Q->front; i < Q->rear; ++i) //从队头打印到队尾
         cout<<"\40"<<Q->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<<"请输入第"<<i<<"个元素的值:";
	cin>>m;
	EnQueue(&Q, m);
	}
	PrintQueue(&Q);
	cout<<GetHead(&Q,&e);
	cout<<QueueLength(&Q);
	cout<<endl<<endl;
	cout<<"请输入需要出队的次数:";
	cin>>t;
	for(int i=1;i<=t;i++)
	{
		DeQueue(&Q,&e);
	}
	PrintQueue(&Q);
	cout<<QueueLength(&Q);
	return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108

2.2运行截图

在这里插入图片描述

2.3参考书籍

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

2.4总结

多多关注留言!

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/706780
推荐阅读
相关标签
  

闽ICP备14008679号