《数据结构》实验报告
实验序号 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");}
digital-vm在日本东京机房当前提供1Gbps带宽、2Gbps带宽、10Gbps带宽接入的独立服务器,每个月自带10T免费流量,一个独立IPv4。支持额外购买流量:20T-$30/月、50T-$150/月、100T-$270美元/月;也支持额外购买IPv4,/29-$5/月、/28-$13/月。独立从下单开始一般24小时内可以上架。官方网站:https://digital-vm.com/de...
腾讯云双十一活动已于今天正式开启了,多重优惠享不停,首购服务器低至0.4折,比如1C2G5M轻量应用服务器仅48元/年起,2C4G8M也仅70元/年起;个人及企业用户还可以一键领取3500-7000元满减券,用于支付新购、续费、升级等各项账单;企业用户还可以以首年1年的价格注册.COM域名。活动页面:https://cloud.tencent.com/act/double11我们分享的信息仍然以秒...
企鹅小屋怎么样?企鹅小屋最近针对自己的美国cn2 gia套餐推出了2个优惠码:月付7折和年付6折,独享CPU,100%性能,三网回程CN2 GIA网络,100Mbps峰值带宽,用完优惠码1G内存套餐是年付240元,线路方面三网回程CN2 GIA。如果新购IP不能正常使用,请在开通时间60分钟内工单VPS技术部门更换正常IP;特价主机不支持退款。点击进入:企鹅小屋官网地址企鹅小屋优惠码:年付6折优惠...