《数据结构》实验报告
实验序号 5 实验项目名称 队列的操作
附源程序清单
1
#inc lude<stdlib.h>
#inc lude<stdio.h>
#define ERROR 1
#define OK 0
#define OVERFLOW 1typedef int QElemType;typedef int Status;
#define MAXQSIZE 100 //最大队列长度typedef struc t {
QElemType*bas e; //动态分配存储空间int front; //头指针若队列不空指向队列头元素int rear; //尾指针若队列不空 //指向队列尾元素的下一个位置
}SqQueue;
//构造一个空队列Q
Status InitQueue(SqQueue&Q)
{
Q.bas e=(QElemType*)malloc (MAXQSIZE*sizeof(QElemType));if (!Q.base) exit (OVERFLOW);//存储分配失败
Q.front=Q.rear=0;return OK;
}
//插入元素e为Q的新的队尾元素
Status EnQueue(SqQueue&Q,QElemType e)
{if((Q.rear+1)%MAXQSIZE==Q.front)return ERROR; //队列满
Q.base[Q.rear]=e;
Q.rear=(Q.rear+1)%MAXQSIZE;return OK;
}
//若队列不空则删除Q的队头元素用e返回其值并返回O K; 否则返回ERRO RStatus DeQueue(SqQueue&Q,QElemType&e)
{if(Q.front==Q.r ear) r eturn ERROR;e=Q.base[Q.front];
Q.front=(Q.front+1)%MAXQ SIZE;return OK;
}
void main()
{char i;int n=0;
QElemType j=0;
SqQueue S;
InitQueue(S); //初始化队列printf("元素入队列,回车结束\n");while((i=getchar())!='\n')
{
EnQueue(S,i); //元素入队列n++;
}printf("元素出队列\n");for(i=0; i<n; i++)
{
DeQueue(S,j); //元素出队列printf("%c",j);
}printf("\n");
}
2
#inc lude<stdlib.h>
#inc lude<stdio.h>
#define ERROR 1
#define OK 0
#define OVERFLOW 1typedef int QElemType;typedef int Status;int tag 1=0;int tag2=1;
#define MAXQSIZE 5 //最大队列长度typedef struc t {
QElemType*bas e; //动态分配存储空间int front; //头指针若队列不空指向队列头元素int rear; //尾指针若队列不空 //指向队列尾元素的下一个位置}SqQueue;
//构造一个空队列Q
Status InitQueue(SqQueue&Q)
{
Q.bas e=(QElemType*)malloc (MAXQSIZE*sizeof(QElemType));if (!Q.base) exit (OVERFLOW);//存储分配失败
Q.front=Q.rear=0;return OK;
}
//插入元素e为Q的新的队尾元素
Status EnQueue(SqQueue&Q,QElemType e)
{if((Q.rear+1)%MAXQSIZE==Q.front)
{if((((Q.rear+1) /MAXQSIZE))==tag 1)
{printf("队列为空\n");
}if((((Q.rear+1) /MAXQSIZE))==tag2)
{printf("队列为满\n");
}
}//return ERROR; //队列满
Q.base[Q.rear]=e;
Q.rear=(Q.rear+1)%MAXQSIZE;return OK;
}
//若队列不空则删除Q的队头元素用e返回其值并返回O K; 否则返回ERRO RStatus DeQueue(SqQueue&Q,QElemType&e)
{if(Q.front==Q.r ear) r eturn ERROR;e=Q.base[Q.front];
Q.front=(Q.front+1)%MAXQ S IZE;return OK;
}void main()
{char i;int n=0;
QElemType j=0;
SqQueue S;
InitQueue(S); //初始化队列printf("元素入队列,回车结束\n");while((i=getchar())!='\n')
{
EnQueue(S,i); //元素入队列
n++;
}printf("元素出队列\n");for(i=0;i<n;i++)
{
DeQueue(S,j); //元素出队列printf("%c",j);
}printf("\n");
}
2
#inc lude<stdlib.h>
#inc lude<stdio.h>
#define ERROR 1
#define OK 0
#define OVERFLOW 1typedef int QElemType;typedef int Status;
#define MAXQSIZE 5//最大队列长度static int tag=1;static int k=0;typedef struc t
{
QElemType*bas e; //动态分配存储空间int front; //头指针若队列不空指向队列头元素int rear; //尾指针若队列不空 //指向队列尾元素的下一个位置
}SqQueue;
//构造一个空队列Q
Status InitQueue(SqQueue&Q)
{
Q.bas e=(QElemType*)malloc (MAXQSIZE*sizeof(QElemType));if (!Q.base) exit (OVERFLOW);//存储分配失败
Q.front=Q.rear=0;return OK;
}
//插入元素e为Q的新的队尾元素
Status EnQueue(SqQueue&Q,QElemType e)
{
Q.base[Q.rear]=e;
Q.rear=(Q.rear+1)%MAXQSIZE;return OK;
}
//若队列不空则删除Q的队头元素用e返回其值并返回O K; 否则返回ERRO RStatus DeQueue(SqQueue&Q,QElemType&e)
{e=Q.base[Q.front];
Q.front=(Q.front+1)%MAXQ SIZE;
//if((((Q.front)%MAXQSIZE))==Q.front)
{tag=2;
}k++;return OK;
}void main()
{char i;int n=0;
QElemType j=0;
SqQueue S;
InitQueue(S); //初始化队列printf("元素入队列,回车结束\n");while((i=getchar())!='\n')
{
EnQueue(S,i); //元素入队列n++;//4
}printf("元素出队列\n");for(i=0; i<n; i++)
{
DeQueue(S,j); //元素出队列printf("%c",j);
}
// printf("%d",k);if((k/MAXQSIZE)==1)
{printf("\n头尾相连 队列为满");
}if(tag==1)
{printf("\n头尾相连 队列为空");
}printf("\n");}
轻云互联怎么样?轻云互联,广州轻云网络科技有限公司旗下品牌,2018年5月成立以来,轻云互联以性价比的价格一直为提供个人,中大小型企业/团队云上解决方案。本次轻云互联送上的是美国圣何塞cn2 vps(免费50G集群防御)及香港沙田cn2 vps(免费10G集群防御)促销活动,促销产品均为cn2直连中国大陆线路、采用kvm虚拟技术架构及静态内存。目前,轻云互联推出美国硅谷、圣何塞CN2GIA云服务器...
物语云计算(MonogatariCloud)是一家成立于2016年的老牌国人商家,主营国内游戏高防独服业务,拥有多家机房资源,产品质量过硬,颇有一定口碑。本次带来的是美国圣何塞 Equinix 机房的高性能I9-10980XE大带宽VPS,去程CN2GIA回程AS9929,美国原生IP,支持解锁奈飞等应用,支持免费安装Windows系统。值得注意的是,物语云采用的虚拟化技术为Hyper-V,资源全...
特网云为您提供高速、稳定、安全、弹性的云计算服务计算、存储、监控、安全,完善的云产品满足您的一切所需,深耕云计算领域10余年;我们拥有前沿的核心技术,始终致力于为政府机构、企业组织和个人开发者提供稳定、安全、可靠、高性价比的云计算产品与服务。官方网站:https://www.56dr.com/ 10年老品牌 值得信赖 有需要的请联系======================特网云推出多IP云主机...