六.队列顺序结构的实现程序

说明:

(1)pubuse.h 是几乎所有实验中都涉及到的,包含了一些常量定义,系统函数原型声明和类型

(Status)重定义,结果状态代码等。

(2) 数据结构定义:以Def.h 为文件名;

(3) 基本操作和算法:以Algo.h 为文件名;

(4) 调用基本操作的主程序:以Use.cpp 为文件名。

所以以上四个文件完成一个程序。推荐这种方法,因为这种方法对以后较大的程序起到便利的作用。

(5)为了方便用户特地写了一个CPP1.cpp,为单文件可运行的程序。

/*pubuse.h*/
#include<string>
#include<ctype.h>
#include<malloc.h> /* malloc()等*/
#include<limits.h> /* INT_MAX 等*/
#include <stdio.h> /* EOF(=^Z 或F6),NULL */
#include <stdlib.h> /* atoi() */
#include<io.h> /* eof() */
#include<math.h> /* floor(),ceil(),abs() */
#include<process.h> /* exit() */
/* 函数结果状态代码*/
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
/* #define OVERFLOW -2 因为在math. h 中已定义OVERFLOW 的值为3,故去掉此行*/
typedef int Status; /* Status 是函数的类型,其值是函数结果状态代码,如OK 等*/
typedef int Boolean;/* Boolean 是布尔类型,其值是TRUE 或FALSE */
/*Def.h*/
#define MAXQSIZE 5 /* 最大队列长度(对于循环队列,最大队列长度要减1) */
 typedef struct
 {
   QElemType *base; /* 初始化的动态分配存储空间 */
   int front; /* 头指针,若队列不空,指向队列头元素 */
   int rear; /* 尾指针,若队列不空,指向队列尾元素的下一个位置 */
 }SqQueue;
/*Algo.h*/
Status InitQueue(SqQueue &Q)
 { /* 构造一个空队列Q */
   Q.base=(QElemType *)malloc(MAXQSIZE*sizeof(QElemType));
   if(!Q.base) /* 存储分配失败 */
     exit(OVERFLOW);
   Q.front=Q.rear=0;
   return OK;
 }
 Status DestroyQueue(SqQueue &Q)
 { /* 销毁队列Q,Q不再存在 */
   if(Q.base)
     free(Q.base);
   Q.base=NULL;
   Q.front=Q.rear=0;
   return OK;
 }
 Status ClearQueue(SqQueue &Q)
 { /* 将Q清为空队列 */
   Q.front=Q.rear=0;
   return OK;
 }
 Status QueueEmpty(SqQueue Q)
 { /* 若队列Q为空队列,则返回TRUE,否则返回FALSE */
   if(Q.front==Q.rear) /* 队列空的标志 */
     return TRUE;
   else
     return FALSE;
 }
 int QueueLength(SqQueue Q)
 { /* 返回Q的元素个数,即队列的长度 */
   return(Q.rear-Q.front);
 }

 Status GetHead(SqQueue Q,QElemType *e)
 { /* 若队列不空,则用e返回Q的队头元素,并返回OK,否则返回ERROR */
   if(Q.front==Q.rear) /* 队列空 */
     return ERROR;
   *e=*(Q.base+Q.front);
   return OK;
 }
 Status EnQueue(SqQueue &Q,QElemType e)
 { /* 插入元素e为Q的新的队尾元素 */
   if(Q.rear>=MAXQSIZE)
   { /* 队列满,增加1个存储单元 */
     Q.base=(QElemType *)realloc(Q.base,(Q.rear+1)*sizeof(QElemType));
     if(!Q.base) /* 增加单元失败 */
       return ERROR;
   }
   *(Q.base+Q.rear)=e;
   Q.rear++;
   return OK;
 }

 Status DeQueue(SqQueue &Q,QElemType &e)
 { /* 若队列不空,则删除Q的队头元素,用e返回其值,并返回OK,否则返回ERROR */
   if(Q.front==Q.rear) /* 队列空 */
     return ERROR;
   e=Q.base[Q.front];
   Q.front=Q.front+1;
   return OK;
 }

 Status QueueTraverse(SqQueue Q,void(*vi)(QElemType))
 { /* 从队头到队尾依次对队列Q中每个元素调用函数vi()。一旦vi失败,则操作失败 */
   int i;
   i=Q.front;
   while(i!=Q.rear)
   {
     vi(*(Q.base+i));
     i++;
   }
   printf("\n");
   return OK;
 }

/*Use.cpp*/
/* main3-4.c 顺序队列(非循环),检验bo3-4.c的主程序 */
 #include"pubuse.h"
 typedef int QElemType;
 #include"def.h"
 #include"algo.h"
 void visit(QElemType i)
 {
   printf("%d ",i);
 }
 void main()
 {
   Status j;
   int i,n;
   QElemType d;
   SqQueue Q;
   InitQueue(Q);
   printf("初始化队列后,队列空否?%u(1:空 0:否)\n",QueueEmpty(Q));
   printf("队列长度为:%d\n",QueueLength(Q));
   printf("请输入队列元素个数n: ");
   scanf("%d",&n);
   printf("请输入%d个整型队列元素:\n",n);
   for(i=0;i<n;i++)
   {
     scanf("%d",&d);
     EnQueue(Q,d);
   }
   printf("队列长度为:%d\n",QueueLength(Q));
   printf("现在队列空否?%u(1:空 0:否)\n",QueueEmpty(Q));
   printf("现在队列中的元素为: \n");
   QueueTraverse(Q,visit);
   DeQueue(Q,d);
   printf("删除队头元素%d\n",d);
   printf("队列中的元素为: \n");
   QueueTraverse(Q,visit);
   j=GetHead(Q,&d);
   if(j)
     printf("队头元素为: %d\n",d);
   else
     printf("无队头元素(空队列)\n");
   ClearQueue(Q);
   printf("清空队列后, 队列空否?%u(1:空 0:否)\n",QueueEmpty(Q));
   j=GetHead(Q,&d);
   if(j)
     printf("队头元素为: %d\n",d);
   else
     printf("无队头元素(空队列)\n");
   DestroyQueue(Q);
 }

/*CPP1.cpp*/
#include<string>
#include<ctype.h>
#include<malloc.h> /* malloc()等*/
#include<limits.h> /* INT_MAX 等*/
#include <stdio.h> /* EOF(=^Z 或F6),NULL */
#include <stdlib.h> /* atoi() */
#include<io.h> /* eof() */
#include<math.h> /* floor(),ceil(),abs() */
#include<process.h> /* exit() */
/* 函数结果状态代码*/
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
/* #define OVERFLOW -2 因为在math. h 中已定义OVERFLOW 的值为3,故去掉此行*/
typedef int Status; /* Status 是函数的类型,其值是函数结果状态代码,如OK 等*/
typedef int Boolean;/* Boolean 是布尔类型,其值是TRUE 或FALSE */
 typedef int QElemType;

 #define MAXQSIZE 5 /* 最大队列长度(对于循环队列,最大队列长度要减1) */
 typedef struct
 {
   QElemType *base; /* 初始化的动态分配存储空间 */
   int front; /* 头指针,若队列不空,指向队列头元素 */
   int rear; /* 尾指针,若队列不空,指向队列尾元素的下一个位置 */
 }SqQueue;

 Status InitQueue(SqQueue &Q)
 { /* 构造一个空队列Q */
   Q.base=(QElemType *)malloc(MAXQSIZE*sizeof(QElemType));
   if(!Q.base) /* 存储分配失败 */
     exit(OVERFLOW);
   Q.front=Q.rear=0;
   return OK;
 }
 Status DestroyQueue(SqQueue &Q)
 { /* 销毁队列Q,Q不再存在 */
   if(Q.base)
     free(Q.base);
   Q.base=NULL;
   Q.front=Q.rear=0;
   return OK;
 }
 Status ClearQueue(SqQueue &Q)
 { /* 将Q清为空队列 */
   Q.front=Q.rear=0;
   return OK;
 }
 Status QueueEmpty(SqQueue Q)
 { /* 若队列Q为空队列,则返回TRUE,否则返回FALSE */
   if(Q.front==Q.rear) /* 队列空的标志 */
     return TRUE;
   else
     return FALSE;
 }
 int QueueLength(SqQueue Q)
 { /* 返回Q的元素个数,即队列的长度 */
   return(Q.rear-Q.front);
 }
 Status GetHead(SqQueue Q,QElemType *e)
 { /* 若队列不空,则用e返回Q的队头元素,并返回OK,否则返回ERROR */
   if(Q.front==Q.rear) /* 队列空 */
     return ERROR;
   *e=*(Q.base+Q.front);
   return OK;
 }
 Status EnQueue(SqQueue &Q,QElemType e)
 { /* 插入元素e为Q的新的队尾元素 */
   if(Q.rear>=MAXQSIZE)
   { /* 队列满,增加1个存储单元 */
     Q.base=(QElemType *)realloc(Q.base,(Q.rear+1)*sizeof(QElemType));
     if(!Q.base) /* 增加单元失败 */
       return ERROR;
   }
   *(Q.base+Q.rear)=e;
   Q.rear++;
   return OK;
 }

 Status DeQueue(SqQueue &Q,QElemType &e)
 { /* 若队列不空,则删除Q的队头元素,用e返回其值,并返回OK,否则返回ERROR */
   if(Q.front==Q.rear) /* 队列空 */
     return ERROR;
   e=Q.base[Q.front];
   Q.front=Q.front+1;
   return OK;
 }

 Status QueueTraverse(SqQueue Q,void(*vi)(QElemType))
 { /* 从队头到队尾依次对队列Q中每个元素调用函数vi()。一旦vi失败,则操作失败 */
   int i;
   i=Q.front;
   while(i!=Q.rear)
   {
     vi(*(Q.base+i));
     i++;
   }
   printf("\n");
   return OK;
 }

 void visit(QElemType i)
 {
   printf("%d ",i);
 }

 void main()
 {
   Status j;
   int i,n;
   QElemType d;
   SqQueue Q;
   InitQueue(Q);
   printf("初始化队列后,队列空否?%u(1:空 0:否)\n",QueueEmpty(Q));
   printf("队列长度为:%d\n",QueueLength(Q));
   printf("请输入队列元素个数n: ");
   scanf("%d",&n);
   printf("请输入%d个整型队列元素:\n",n);
   for(i=0;i<n;i++)
   {
     scanf("%d",&d);
     EnQueue(Q,d);
   }
   printf("队列长度为:%d\n",QueueLength(Q));
   printf("现在队列空否?%u(1:空 0:否)\n",QueueEmpty(Q));
   printf("现在队列中的元素为: \n");
   QueueTraverse(Q,visit);
   DeQueue(Q,d);
   printf("删除队头元素%d\n",d);
   printf("队列中的元素为: \n");
   QueueTraverse(Q,visit);
   j=GetHead(Q,&d);
   if(j)
     printf("队头元素为: %d\n",d);
   else
     printf("无队头元素(空队列)\n");
   ClearQueue(Q);
   printf("清空队列后, 队列空否?%u(1:空 0:否)\n",QueueEmpty(Q));
   j=GetHead(Q,&d);
   if(j)
     printf("队头元素为: %d\n",d);
   else
     printf("无队头元素(空队列)\n");
   DestroyQueue(Q);
 }


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值