1、创建circle_queue.h 文件

#ifndef CIRCLE_QUEUE_H_

#define CIRCLE_QUEUE_H_

#define SUCCESS 1

#define FAILED -1

typedef int QElemType;// 队列的顺序存储结构(可用于循环队列和非循环队列)

#define MAXSIZE 1000 // 最大队列长度(对于循环队列,最大队列长度要减1)

typedef struct circle_queue circle_queue;

struct circle_queue

{

QElemType *base;// 初始化的动态分配存储空间,相当于一个数组头

int front;// 头指针,若队列不空,指向队列头元素,相当于一个下标

int rear; // 尾指针,若队列不空,指向队列尾元素的下一个位置,相当于一个下标

int count; // 元素个数

};

int init_queue(circle_queue *this); //初始化队列

int destroy_queue(circle_queue *this); //销毁队列

int queue_length(circle_queue *this); //获取队列长度

int insert_last(circle_queue *this,QElemType value); //插入队列尾部

int remove_first(circle_queue *this); //从队列头部移出

int is_empty_queue(circle_queue *this); //判断队列是否为空

int get_head(circle_queue *this,QElemType value); //若队列不空,获得队列的头元素

#endif

2、创建对应的

circle_queue.c文件

#include

#include

#include

#include "circle_queue.h"

//构造一个空队列Q,初始化队列

int init_queue(circle_queue *this)

{

//给队头队尾指针分配空间,并置空

this->base=malloc(MAXSIZE*sizeof(QElemType)); //this.base相当于数组头

if(!this->base) // 存储分配失败

exit(0);

this->front=this->rear=0;//下标初始化为0

return SUCCESS;

}

// 销毁队列queue

int destroy_queue(circle_queue *this)

{

if(this->base)

free(this->base);

this->base=NULL;

this->front=this->rear=0;//空队列的标志是队头队尾指针都相同,且为0

return SUCCESS;

}

//获取队列长度

int queue_length(circle_queue *this)

{

return(this->rear-this->front+MAXSIZE)%MAXSIZE;

}

//插入队列尾部,入队

int insert_last(circle_queue *this,QElemType value)

{

if(this->count == MAXSIZE) //队列满

{

return FAILED;

}

this->base[this->rear]=value;

this->rear=(this->rear+1)%MAXSIZE;

this->count++;

return SUCCESS;

}

//从队列头部移出,出队

int remove_first(circle_queue *this)

{

int value;

if (this->count==0) //队列空

{

return FAILED;

}

value=this->base[this->front];

this->front=(this->front+1)%MAXSIZE;

this->count--;

return value;

}

//判断队列是否为空

int is_empty_queue(circle_queue *this)

{

if(this == NULL)

{

return FAILED;

}

if (this->count==0)

{

return FAILED;

}

else

{

return SUCCESS;

}

}

//若队列不空,获得队列的头元素

int get_head(circle_queue *this,QElemType value)

{

if(this->front==this->rear)//队列空

{

return FAILED;

}

value=this->base[this->front];

return value;//获取队头元素值

}3、创建

circle_queue_test.c测试文件

#include

#include

#include

#include "circle_queue.h"

int main(int argc, char *argv[])

{

int i;

int count;

int value;

int empty;

circle_queue *queue=malloc(sizeof(circle_queue));//声明一个循环队列queue;

init_queue(queue);//循环队列queue初始化

empty = is_empty_queue(queue);//队列判空

if(empty > 0)

{

printf("队列不为空!\n");

}

else

{

printf("队列为空!\n");

}

printf("请输入元素的个数:\n");

scanf("%d",&count);

printf("请输入%d个入队元素:\n",count);

for(i=0;i

{

printf("第%d个:",i+1);

scanf("%d",&value);

insert_last(queue,value); //入队

}

empty = is_empty_queue(queue);//队列判空

if(empty > 0)

{

printf("队列不为空!\n");

}

else

{

printf("队列为空!\n");

}

count=queue_length(queue);

printf("队列的长度为:%d\n",count);

printf("队列中的元素为:");

for(i=0;i

{

printf("%d",queue->base[queue->front+i]);

if(i < (count-1))

{

printf("

}

}

printf("\n");

value=get_head(queue,value);

printf("队头元素为:%d\n",value);

printf("队列开始出队:\n");

for(i=0;i

{

printf("出队的元素为:第%d个:%d\n",i+1,remove_first(queue));

}

empty = is_empty_queue(queue);//队列判空

if(empty > 0)

{

printf("队列不为空!\n");

}

else

{

printf("队列为空!\n");

}

if(queue)

{

printf("队列开始销毁...\n");

}

destroy_queue(queue);//销毁循环队列queue

if(!(queue->base))

{

printf("队列销毁成功!\n");

}

return;

}

4、编译

gcc -o test circle_queue_test.c circle_queue.c

5、运行

./test

6、演示

linux编译c文件for循环,Linux C 循环队列的实现相关推荐

  1. linux编译cpp文件命令,Jsoncpp Linux 下编译为 .a 文件

    1 下载 jsoncpp 路径如下: https://github.com/open-source-parsers/jsoncpp 2. 解压文件 unzip jsoncpp-master.zip 3 ...

  2. linux 穿件文件_关于Linux的25件事

    linux 穿件文件 今天是Linux 25年的历史,这是有史以来最成功的软件. 在本周的LinuxCon上,Linux基金会的Jim Zemlin在主题演讲中表达了钦佩,赞美和激动,他说:" ...

  3. linux 几个文件夹作用,linux下每一个文件夹的作用.docx

    目录结构及主要内容 "/"根冃录部分有以F子冃录: /usr 口录包含所有的命令.程序库.文档和英它文件.这些文件在正常操作中不会被改变的.这个目录也包含你的Linux 发行版本的 ...

  4. 四、linux编译规则文件Makefile

    makefile脚本语法规则具体看: 1.编译器路径设置 linux内核源码根目录下makefile文件修改编译器路径.如下图: 2. 强制编译 源码开发者强制要求编译进内核的文件 ,类似下面的写法, ...

  5. centos linux编译c,紧急提醒!Linux是如何编译C语言程序文件的?CentOS 8的gcc使用方法介绍...

    一句话告诉你gcc怎么编译C文件 执行命令 gcc Tristone.c  -o Tristone 解释:"Tristone.C"Tristone可执行文件编译,编译完成后&quo ...

  6. linux编译.o文件,使用-O0编译Linux内核

    文章目录 1. 编译内核 1.1. 修改gcc优化等级 1.2. 防止`modpost: Section mismatches detected.`错误 1.3. 根据需要编译内核 1.4. 修改子目 ...

  7. linux编译lnx文件命令_linux命令dd

    dd 使用dd这个linux命令可以创建一定大小文件. linux创建文件命令:dd命令 把指定的输入文件拷贝到指定的输出文件中,并且在拷贝的过程中可以进行格式转换.语法: CODE:[Copy to ...

  8. 树莓派linux编译不了动态库,linux系统下的树莓派与Qt 5.12.3源码的交叉编译

    {写在前面:按照这个方法,基本可以成功在linux系统下交叉编译Qt5.12.3,其他版本的源码也编译} 我的环境:Linux Mint 19.1;树莓派 3;Qt源码5.12.3 当两个系统全部安装 ...

  9. linux编译内核实验,实验六 Linux内核编译实验.doc

    实验六 Linux内核编译 讲师:杨行 [实验目的] 1.掌握Linux内核编译 2.了解Linux内核Makefile 3.了解Linux内核Kbuild系统 [实验原理] 网站可以下载标准内核文件 ...

  10. Linux编译soci库,Soci库linux下的编译方法

    Soci库的linux编译方法 1.下载soci库源码 2.在目标机器上配置数据库环境 以oracle为例:(其他数据库只需要简单安装客户端即可) A)下载oracle客户端安装包 oracle-in ...

最新文章

  1. R语言使用ggplot2包使用geom_boxplot函数绘制基础分组箱图(不同分组配置不同的箱体填充色+灰度尺度图)实战
  2. 使用mss2sql将SqlServer转换为Mysql
  3. 【NLP】一行Python代码中自动化文本处理
  4. 使用Java程序通过http post访问Application server
  5. oracle用户密码复杂度查询,11gR2 Database用户密码复杂度验证
  6. 不用空格怎么打两个空格_和平精英:苹果手机怎么打空格 空白名使用教程
  7. ac ap原理、_AP面板是什么?家庭AC+AP的组网方式,真的适合所有人吗?
  8. UI线框图模板素材实际应用好帮手
  9. Visual Stdio下安装WTL的向导Widzard
  10. sonar-runner命令模式运行sonar
  11. Python命令行参数选择
  12. 常见的资源记录类型及应用示例
  13. 西门子 SinuTrain 840Dsl OPC UA 模拟
  14. PDF文档无法注释或高亮的解决办法
  15. 线元法输入曲线要素_交点法、线元法
  16. 【操作系统】设备管理
  17. windows10 20H2版本微软账户登录不上解决方法
  18. 使用OneNote多人分布式共享
  19. 英语流利说l4u1p2_L4-U1-P2-3 Vocabulary : Science 英语流利说 懂你英语
  20. windows cmd设置ip\dns地址

热门文章

  1. java 生日 计算_java根据生日计算当前年龄,精确到月
  2. python中读取文本文件_利用Python读取文本文件?
  3. cdr 表格自动填充文字_操作基础知识Word文字编辑
  4. 渐进式Web应用程序居然是Web开发的未来
  5. php全词查询,php 关键词查询的实现方法
  6. java肝癌晚期_生信分析43.肿瘤浸润免疫与肝癌(HCCDB+oncomine)
  7. 计算机二级b5纸是多大尺寸,两张b5纸是多大
  8. linux下查询汇编指令手册,Linux命令大全
  9. android 多个模块,Android 模块化中两个模块之间传递model
  10. 有多个正整数存放在数组中,编写一个函数要求偶数在左边由小到大顺序放置,奇数在右边,也是由小到大顺序放置,Java实现...