《数据结构》实验报告
实验序号 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 s truc 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)%MAXQ SIZE==Q.front)return ERROR; //队列满
Q.base[Q.rear]=e;
Q.rear=(Q.rear+1)%MAXQSIZE;return OK;
}
//若队列不空则删除Q的队头元素用e返回其值并返回OK; 否则返回ERRORStatus DeQueue(SqQueue&Q,QElemType&e)
{if(Q.front==Q.rear) return 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 s truc t {
QElemType*bas e; //动态分配存储空间int front; //头指针若队列不空指向队列头元素int rear; //尾指针若队列不空 //指向队列尾元素的下一个位置
}SqQueue;
//构造一个空队列Q
Status InitQueue(SqQueue&Q)
{
Q.base=(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)%MAXQ SIZE==Q.front)
{if((((Q.rear+1) /MAXQSIZE))==tag 1)
{printf("队列为空\n");
}if((((Q.rear+1) /MAXQ S IZE))==tag2)
{printf("队列为满\n");
}
}//return ERROR; //队列满
Q.base[Q.rear]=e;
Q.rear=(Q.rear+1)%MAXQSIZE;return OK;
}
//若队列不空则删除Q的队头元素用e返回其值并返回OK; 否则返回ERRORStatus DeQueue(SqQueue&Q,QElemType&e)
{if(Q.front==Q.rear) 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;
#define MAXQSIZE 5//最大队列长度static int tag=1;static int k=0;typedef s truc t
{
QElemType*bas e; //动态分配存储空间int front; //头指针若队列不空指向队列头元素int rear; //尾指针若队列不空 //指向队列尾元素的下一个位置
}SqQueue;
//构造一个空队列Q
Status InitQueue(SqQueue&Q)
{
Q.base=(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返回其值并返回OK; 否则返回ERRORStatus DeQueue(SqQueue&Q,QElemType&e)
{e=Q.bas e[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");
Virtono最近推出了夏季促销活动,为月付、季付、半年付等提供9折优惠码,年付已直接5折,而且下单后在LET回复订单号还能获得双倍内存,不限制付款周期。这是一家成立于2014年的国外VPS主机商,提供VPS和服务器租用等产品,商家支持PayPal、信用卡、支付宝等国内外付款方式,可选数据中心包括罗马尼亚、美国洛杉矶、达拉斯、迈阿密、英国和德国等。下面列出几款VPS主机配置信息,请留意,下列配置中...
profitserver怎么样?profitserver是一家成立于2003的主机商家,是ITC控股的一个部门,主要经营的产品域名、SSL证书、虚拟主机、VPS和独立服务器,机房有俄罗斯、新加坡、荷兰、美国、保加利亚,VPS采用的是KVM虚拟架构,硬盘采用纯SSD,而且最大的优势是不限制流量,大公司运营,机器比较稳定,数据中心众多。此次ProfitServer正在对德国VPS(法兰克福)、西班牙v...
快快云怎么样?快快云是一家成立于2021年的主机服务商,致力于为用户提供高性价比稳定快速的主机托管服务,快快云目前提供有香港云服务器、美国云服务器、日本云服务器、香港独立服务器、美国独立服务器,日本独立服务器。快快云专注为个人开发者用户,中小型,大型企业用户提供一站式核心网络云端服务部署,促使用户云端部署化简为零,轻松快捷运用云计算!多年云计算领域服务经验,遍布亚太地区的海量节点为业务推进提供强大...