第七周自建算法库——顺序环形队列

  1. /* 
  2.  *Copyright (c) 2016,烟台大学计算机学院 
  3.  *All rights reserved. 
  4.  *文件名称:houzhuibiaodashi.cpp 
  5.  *作者:衣龙川 
  6.  *完成日期:2016年10月08日 
  7.  *版本号:vc++6.0 
  8.  * 
  9.  *问题描述:后缀表达式 
  10.  *输入描述:一个算数表达式 
  11.  *程序输出:加后缀的表达式 
  12. */  

sqqueue.h

#ifndef SQQUEUE_H_INCLUDED
#define SQQUEUE_H_INCLUDED

#define MaxSize 5
typedef char ElemType;
typedef struct
{
    ElemType data[MaxSize];
    int front,rear;     /*队首和队尾指针*/
} SqQueue;


void InitQueue(SqQueue *&q);  //初始化顺序环形队列
void DestroyQueue(SqQueue *&q); //销毁顺序环形队列
bool QueueEmpty(SqQueue *q);  //判断顺序环形队列是否为空
int QueueLength(SqQueue *q);   //返回队列中元素个数,也称队列长度
bool enQueue(SqQueue *&q,ElemType e);   //进队
bool deQueue(SqQueue *&q,ElemType &e);  //出队

#endif // SQQUEUE_H_INCLUDED

sqqueue.cpp

#include <stdio.h>
#include <malloc.h>
#include "sqqueue.h"

void InitQueue(SqQueue *&q)  //初始化顺序环形队列
{
    q=(SqQueue *)malloc (sizeof(SqQueue));
    q->front=q->rear=0;
}
void DestroyQueue(SqQueue *&q) //销毁顺序环形队列
{
    free(q);
}
bool QueueEmpty(SqQueue *q)  //判断顺序环形队列是否为空
{
    return(q->front==q->rear);
}


int QueueLength(SqQueue *q)   //返回队列中元素个数,也称队列长度
{
    return (q->rear-q->front+MaxSize)%MaxSize;
}

bool enQueue(SqQueue *&q,ElemType e)   //进队
{
    if ((q->rear+1)%MaxSize==q->front)  //队满上溢出
        return false;
    q->rear=(q->rear+1)%MaxSize;
    q->data[q->rear]=e;
    return true;
}
bool deQueue(SqQueue *&q,ElemType &e)  //出队
{
    if (q->front==q->rear)      //队空下溢出
        return false;
    q->front=(q->front+1)%MaxSize;
    e=q->data[q->front];
    return true;
}
main.cpp

#include <stdio.h>
#include "sqqueue.h"

int main()
{
    ElemType e;
    SqQueue *q;
    printf("(1)初始化队列q\n");
    InitQueue(q);
    printf("(2)依次进队列元素a,b,c\n");
    if (enQueue(q,'a')==0) printf("队满,不能进队\n");
    if (enQueue(q,'b')==0) printf("队满,不能进队\n");
    if (enQueue(q,'c')==0) printf("队满,不能进队\n");
    printf("(3)队列为%s\n",(QueueEmpty(q)?"空":"非空"));
    if (deQueue(q,e)==0)
        printf("队空,不能出队\n");
    else
        printf("(4)出队一个元素%c\n",e);
    printf("(5)队列q的元素个数:%d\n",QueueLength(q));
    printf("(6)依次进队列元素d,e,f\n");
    if (enQueue(q,'d')==0) printf("队满,不能进队\n");
    if (enQueue(q,'e')==0) printf("队满,不能进队\n");
    if (enQueue(q,'f')==0) printf("队满,不能进队\n");
    printf("(7)队列q的元素个数:%d\n",QueueLength(q));
    printf("(8)出队列序列:");
    while (!QueueEmpty(q))
    {
        deQueue(q,e);
        printf("%c ",e);
    }
    printf("\n");
    printf("(9)释放队列\n");
    DestroyQueue(q);
    return 0;
}

运行截图:


知识点总结:

环形队列首先知道最多保存MaxSize个元素,还有在进栈出栈时要考虑的队空(front=rear)和队满((rear+1)%MaxSize=front)的情况。

要编写自己的算法,你可以按照以下步骤进行: 1. 创建一个新的文件夹来存放你的算法,例如 "MyAlgorithms"。 2. 在该文件夹下创建一个头文件(.h 文件)和一个源文件(.cpp 文件),分别用于声明和定义你的算法函数。 3. 在头文件中声明你的算法函数,并提供必要的注释和文档说明。确保函数的参数和返回值都有明确的类型和含义。 4. 在源文件中实现你的算法函数。根据算法的逻辑,编写相应的代码,并确保代码正确性和性能。 5. 可选地,在源文件中添加一些辅助函数或工具函数,以支持你的算法实现。 6. 在需要使用你的算法的项目中,将你的头文件(.h 文件)包含进来,并使用其中的函数。 下面是一个简单的示例,展示了如何编写一个自定义的算法: **MyAlgorithms.h**: ```cpp #ifndef MYALGORITHMS_H #define MYALGORITHMS_H #include <vector> // 函数:计算向量中元素的总和 int sum(const std::vector<int>& vec); // 函数:在向量中查找特定元素,返回元素所在位置的索引 int find(const std::vector<int>& vec, int target); #endif ``` **MyAlgorithms.cpp**: ```cpp #include "MyAlgorithms.h" int sum(const std::vector<int>& vec) { int total = 0; for (int num : vec) { total += num; } return total; } int find(const std::vector<int>& vec, int target) { for (int i = 0; i < vec.size(); ++i) { if (vec[i] == target) { return i; } } return -1; } ``` 接下来,你可以在其他项目中使用你的自定义算法: ```cpp #include <iostream> #include "MyAlgorithms.h" int main() { std::vector<int> nums = {1, 2, 3, 4, 5}; int total = sum(nums); std::cout << "Total sum: " << total << std::endl; int index = find(nums, 3); if (index != -1) { std::cout << "Element found at index: " << index << std::endl; } else { std::cout << "Element not found" << std::endl; } return 0; } ``` 以上示例中,我们创建了一个名为 "MyAlgorithms" 的算法,并在头文件中声明了 `sum` 和 `find` 两个函数。在源文件中,我们实现了这两个函数的逻辑。然后,在使用该算法的项目中,我们包含了头文件,并使用其中的函数进行计算和查找操作。
评论 68
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值