《数据结构》实验报告
实验序号 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");}
totyun,新公司,主要运作香港vps、日本vps业务,接入cn2网络,不限制流量!VPS基于KVM虚拟,采用系统盘和数据盘分离,从4G内存开始支持Windows系统...大家注意下,网络分“Premium China”、“Global”,由于站长尚未测试,所以也还不清楚情况,有喜欢吃螃蟹的尝试过不妨告诉下站长。官方网站:https://totyun.com一次性5折优惠码:X4QTYVNB3P...
RAKsmart 商家估摸着前段时间服务器囤货较多,这两个月的促销活动好像有点针对独立服务器。前面才整理到七月份的服务器活动在有一些配置上比上个月折扣力度是大很多,而且今天看到再来部分的服务器首月半价,一般这样的促销有可能是商家库存充裕。比如近期有一些服务商挖矿服务器销售不好,也都会采用这些策略,就好比电脑硬件最近也有下降。不管如何,我们选择服务器或者VPS主机要本着符合自己需求,如果业务不需要,...
zji怎么样?zji最近新上韩国BGP+CN2线路服务器,国内三网访问速度优秀,适用8折优惠码zji,优惠后韩国服务器最低每月440元起。zji主机支持安装Linux或者Windows操作系统,会员中心集成电源管理功能,8折优惠码为终身折扣,续费同价,全场适用。ZJI是原Wordpress圈知名主机商:维翔主机,成立于2011年,2018年9月启用新域名ZJI,提供中国香港、台湾、日本、美国独立服...