第七周项目1- 建立顺序环形队列算法库

来源:互联网 发布:孙侨潞淘宝店叫什么 编辑:程序博客网 时间:2024/06/06 16:30
问题描述及代码:[cpp] view plain copy1./*    2.*烟台大学计控学院     3.*作    者:朱建豪    4.*完成日期:2016年10月14日 5.*问题描述:定义顺序环形队列存储结构,实现其基本运算,并完成测试。  6.要求:  7.  1、头文件sqqueue.h中定义数据结构并声明用于完成基本运算的函数。对应基本运算的函数包括: 8. 9.void InitQueue(SqQueue *&q);  //初始化顺序环形队列 10.void DestroyQueue(SqQueue *&q); //销毁顺序环形队列 11.bool QueueEmpty(SqQueue *q);  //判断顺序环形队列是否为空 12.int QueueLength(SqQueue *q);   //返回队列中元素个数,也称队列长度 13.bool enQueue(SqQueue *&q,ElemType e);   //进队 14.bool deQueue(SqQueue *&q,ElemType &e);  //出队1 15.  2、在sqqueue.cpp中实现这些函数  16.  3、在main函数中完成测试,包括如下内容: 17. 18.(1)初始化队列q  19.(2)依次进队列元素a,b,c  20.(3)判断队列是否为空  21.(4)出队一个元素  22.(5)输出队列中元素个数  23.(6)依次进队列元素d,e,f  24.(7)输出队列中元素个数  25.(8)将队列中所有元素删除,并输出序列  26.(9)释放队列 27.*/  (1)sqqueue.h[cpp] view plain copy1.#ifndef SQQUEUE_H_INCLUDED  2.#define SQQUEUE_H_INCLUDED  3.#define MaxSize 5  4.typedef char ElemType;  5.typedef struct  6.{  7.    ElemType data[MaxSize];  8.    int front,rear;     /*队首和队尾指针*/  9.} SqQueue;  10.  11.  12.void InitQueue(SqQueue *&q);  //初始化顺序环形队列  13.void DestroyQueue(SqQueue *&q); //销毁顺序环形队列  14.bool QueueEmpty(SqQueue *q);  //判断顺序环形队列是否为空  15.int QueueLength(SqQueue *q);   //返回队列中元素个数,也称队列长度  16.bool enQueue(SqQueue *&q,ElemType e);   //进队  17.bool deQueue(SqQueue *&q,ElemType &e);  //出队  18.  19.  20.  21.  22.#endif // SQQUEUE_H_INCLUDED  (2)sqqueue.cpp[cpp] view plain copy1.#include"sqqueue.h"  2.#include<stdio.h>  3.#include<malloc.h>  4.void InitQueue(SqQueue *&q)  //初始化顺序环形队列  5.{  6.    q=(SqQueue *)malloc (sizeof(SqQueue));  7.    q->front=q->rear=0;  8.}  9.void DestroyQueue(SqQueue *&q) //销毁顺序环形队列  10.{  11.    free(q);  12.}  13.bool QueueEmpty(SqQueue *q)  //判断顺序环形队列是否为空  14.{  15.    return(q->front==q->rear);  16.}  17.  18.  19.int QueueLength(SqQueue *q)   //返回队列中元素个数,也称队列长度  20.{  21.    return (q->rear-q->front+MaxSize)%MaxSize;  22.}  23.  24.bool enQueue(SqQueue *&q,ElemType e)   //进队  25.{  26.    if ((q->rear+1)%MaxSize==q->front)  //队满上溢出  27.        return false;  28.    q->rear=(q->rear+1)%MaxSize;  29.    q->data[q->rear]=e;  30.    return true;  31.}  32.bool deQueue(SqQueue *&q,ElemType &e)  //出队  33.{  34.    if (q->front==q->rear)      //队空下溢出  35.        return false;  36.    q->front=(q->front+1)%MaxSize;  37.    e=q->data[q->front];  38.    return true;  39.}  (3)main.cpp[cpp] view plain copy1.#include"sqqueue.h"  2.#include<stdio.h>  3.int main()  4.{  5.    ElemType e;  6.    SqQueue *q;  7.    printf("(1)初始化队列q\n");  8.    InitQueue(q);  9.    printf("(2)依次进队列元素a,b,c\n");  10.    if (enQueue(q,'a')==0) printf("队满,不能进队\n");  11.    if (enQueue(q,'b')==0) printf("队满,不能进队\n");  12.    if (enQueue(q,'c')==0) printf("队满,不能进队\n");  13.    printf("(3)队列为%s\n",(QueueEmpty(q)?"空":"非空"));  14.    if (deQueue(q,e)==0)  15.        printf("队空,不能出队\n");  16.    else  17.        printf("(4)出队一个元素%c\n",e);  18.    printf("(5)队列q的元素个数:%d\n",QueueLength(q));  19.    printf("(6)依次进队列元素d,e,f\n");  20.    if (enQueue(q,'d')==0) printf("队满,不能进队\n");  21.    if (enQueue(q,'e')==0) printf("队满,不能进队\n");  22.    if (enQueue(q,'f')==0) printf("队满,不能进队\n");  23.    printf("(7)队列q的元素个数:%d\n",QueueLength(q));  24.    printf("(8)出队列序列:");  25.    while (!QueueEmpty(q))  26.    {  27.        deQueue(q,e);  28.        printf("%c ",e);  29.    }  30.    printf("\n");  31.    printf("(9)释放队列\n");  32.    DestroyQueue(q);  33.    return 0;  34.}  运行结果:
<img src="http://img.blog.csdn.net/20161014101759649?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />知识点总结:顺序环形队列的初始化、销毁、判断是否为空、进队列、出队列、求队列的长度学习心得:基本掌握了顺序环形队列的基本运算

0 0
原创粉丝点击