#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
#include<time.h>
#define ElemType char
#define SIZE 30//队列的最大长度
typedef struct QNode
{
ElemType *base;//初始化的分配存储空间
int front;//头指针针,若队列不空,则指向队头元素
int rear;//尾指针,若队列不空,则指向队尾元素的下一个元素
}SqQueue;
int InitQueue(SqQueue &Q)//初始化一个空队列
{
Q.base=(ElemType *)malloc(SIZE*sizeof(ElemType));//队列基址指针
if(Q.base == NULL)//若分配内存失败
{
printf("存储分配失败!\n\n");return -1;
}
Q.front=Q.rear=0;//初始化队头和队尾指针
return 1;
}
int Assignment(SqQueue &Q)//为队列中的元素赋值
{
srand((unsigned)time(NULL));
for(int i=0;i<10;i++)//往队列中添加10个数据
{
if((Q.rear+1)%SIZE == Q.front)break;
Q.base[Q.rear]=(char)(rand()%26+'a');
Q.rear=(Q.rear+1)%SIZE;//队尾指针前进一位
}
return 1;
}
int DisplayQueue(SqQueue Q)//从队头到队尾依次打印队列中的元素
{
if(Q.rear == Q.front)
{
printf("当前队列为空!\n\n
建立数据元素值为字符型的顺序队列
最新推荐文章于 2021-10-09 21:32:16 发布