#ifndef QUEUE_H
#define QUEUE_H #define QUEUE_SIZE 10 typedef struct queue
{
int buffer[QUEUE_SIZE];
int head;
int size;
int tail; int (*isFull)(struct queue* const me);
int (*isEmpty)(struct queue* const me);
int (*getSize)(struct queue* const me);
void (*insert)(struct queue* const me, int k);
int (*remove)(struct queue* const me);
}QUEUE; void Queue_Init(QUEUE* me, int (*isFullFunction)(QUEUE* const me),
int (*isEmptyFunction)(QUEUE* const me),
int (*getSizeFunction)(QUEUE* const me),
void (*insertFunction)(QUEUE* const me, int k),
int (*removeFunction)(QUEUE* const me));
void Queue_Cleanup(QUEUE* const me); int Queue_isFull(QUEUE* const me);
int Queue_isEmpty(QUEUE* const me);
int Queue_getSize(QUEUE* const me);
void Queue_insert(QUEUE* const me, int k);
int Queue_remove(QUEUE* const me); QUEUE* Queue_Create(void);
void Queue_Destroy(QUEUE* const me); #endif
#include <stdio.h>
#include <stdlib.h>
#include "queue.h" void Queue_Init(QUEUE* me, int (*isFullFunction)(QUEUE* const me),
int (*isEmptyFunction)(QUEUE* const me),
int (*getSizeFunction)(QUEUE* const me),
void (*insertFunction)(QUEUE* const me, int k),
int (*removeFunction)(QUEUE* const me)){ me->head=0;
me->tail=0;
me->size=0; me->isFull=isFullFunction;
me->isEmpty=isEmptyFunction;
me->getSize=getSizeFunction;
me->insert=insertFunction;
me->remove=removeFunction;
} void Queue_Cleanup(QUEUE* const me){ } int Queue_isFull(QUEUE* const me){ return (me->head+1)%QUEUE_SIZE==me->tail;
} int Queue_isEmpty(QUEUE* const me){ return me->head==me->tail;
} int Queue_getSize(QUEUE* const me){ return me->size;
} void Queue_insert(QUEUE* const me, int k){ if (!me->isFull(me))
{
me->buffer[me->head]=k;
me->head=(me->head+1)%QUEUE_SIZE;
++me->size;
}
} int Queue_remove(QUEUE* const me){ int value=-9999; if(!me->isEmpty(me))
{
value=me->buffer[me->tail];
me->tail=(me->tail+1)%QUEUE_SIZE;
--me->size;
} return value;
} QUEUE* Queue_Create(void){ QUEUE* me=(QUEUE*)malloc(sizeof(QUEUE)); if (me!=NULL)
{
Queue_Init(me,Queue_isFull,Queue_isEmpty,Queue_getSize,
Queue_insert,Queue_remove);
} return me;
} void Queue_Destroy(QUEUE* const me){ if (me!=NULL)
{
Queue_Cleanup(me);
} free(me);
}
#include "queue.h"
#include <stdlib.h>
#include <stdio.h> int main(void)
{
int j,k,h,t; QUEUE* myQ;
myQ=Queue_Create();
k=1000; for (j = 0; j<QUEUE_SIZE; j++)
{
h=myQ->head;
myQ->insert(myQ, k);
printf("inserting %d at position %d, size=%d\n", k--,h,myQ->getSize(myQ));
}
printf("Iserted %d elements\n", myQ->getSize(myQ)); for (j = 0; j<QUEUE_SIZE; j++)
{
t=myQ->tail;
k=myQ->remove(myQ);
printf("Removing %d at position %d, size=%d\n", k, t, myQ->getSize(myQ));
}
printf("Last item removed = %d\n", k); printf("Current queue size %d\n", myQ->getSize(myQ));
puts("Queue test program"); return 0;
}

C语言设计模式(应用)的更多相关文章

  1. Go语言设计模式之函数式选项模式

    Go语言设计模式之函数式选项模式 本文主要介绍了Go语言中函数式选项模式及该设计模式在实际编程中的应用. 为什么需要函数式选项模式? 最近看go-micro/options.go源码的时候,发现了一段 ...

  2. C语言设计模式-封装-继承-多态

    快过年了,手头的工作慢慢也就少了,所以,研究技术的时间就多了很多时间,前些天在CSDN一博客看到有大牛在讨论C的设计模式,正好看到了,我也有兴趣转发,修改,研究一下. 记得读大学的时候,老师就告诉我们 ...

  3. Go语言设计模式实践:迭代器(Iterator)

    关于本系列 决定开个新坑. 这个系列首先是关于Go语言实践的.在项目中实际使用Go语言也有段时间了,一个体会就是不论是官方文档.图书还是网络资料,关于Go语言惯用法(idiom)的介绍都比较少,基本只 ...

  4. Go语言设计模式实践:组合(Composite)

    关于本系列 这个系列首先是关于Go语言实践的.在项目中实际使用Go语言也有段时间了,一个体会就是不论是官方文档.图书还是网络资料,关于Go语言惯用法(idiom)的介绍都比较少,基本只能靠看标准库源代 ...

  5. Go语言设计模式汇总

    目录 设计模式背景和起源 设计模式是什么 Go语言模式分类 个人观点 Go语言从面世就受到了业界的普遍关注,随着区块链的火热Go语言的地位也急速蹿升,为了让读者对设计模式在Go语言中有一个初步的了解和 ...

  6. C语言设计模式

    一 .C语言和设计模式(继承.封装.多态) C++有三个最重要的特点,即继承.封装.多态.我发现其实C语言也是可以面向对象的,也是可以应用设计模式的,关键就在于如何实现面向对象语言的三个重要属性. ( ...

  7. go语言设计模式之Concurrency workers pool

    worker.go package main import ( "fmt" "strings" ) type WorkerLauncher interface ...

  8. go语言设计模式之Concurrency pipeline

    pipeline.go package pipeline func LaunchPipeline(amount int) int { firstCh := generator(amount) seco ...

  9. go语言设计模式之Concurrency future

    future.go package future type SuccessFunc func(string) type FailFunc func(error) type ExecuteStringF ...

  10. go语言设计模式之Concurrency barrier

    barrier.go package barrier import ( "fmt" "io/ioutil" "net/http" " ...

随机推荐

  1. 原生js实现一个自定义下拉单选选择框

    浏览器自带的原生下拉框不太美观,而且各个浏览器表现也不一致,UI一般给的下拉框也是和原生的下拉框差别比较大的,这就需要自己写一个基本功能的下拉菜单/下拉选择框了.最近,把项目中用到的下拉框组件重新封装 ...

  2. 【Flutter 混合开发】嵌入原生View-iOS

    Flutter 混合开发系列 包含如下: 嵌入原生View-Android 嵌入原生View-iOS 与原生通信-MethodChannel 与原生通信-BasicMessageChannel 与原生 ...

  3. vbox挂载共享文件夹

      版权 挂载共享文件夹很简单,有2种方法,1是自动挂载,2是手动挂载. 一.自动挂载步骤: 1,把想共享的文件夹设置为共享. 2,在virtualbox界面对虚拟机设置共享文件夹,如下图.

  4. virtualbox 网络地址转换(NAT)

    网络地址转换 虚拟机可以访问主机 通过主机请求外网 但是主机不能请求虚拟机 所以要配置端口转发才行 host-only模式下 不同网段的不同虚拟机也可以互相ping通  比如 192.168.33.1 ...

  5. UEditor 自定义图片视频尺寸校验

    UEditor支持单图.多图以及视频上传,编辑器配置项支持文件格式.文件大小校验,对于文件宽高尺寸校验暂不支持.这里记录一下自定义图片.视频尺寸校验过程,内容核心主要是扩展校验逻辑和增加自定义提示文本 ...

  6. 通过命令行上传ipa到appstore

    搞持续集成自动化打包上传到appstore遇到这个问题,记录一下. 其实主要就一条到命令: xcrun altool --upload-app -f xxxx.ipa -u "yanqizh ...

  7. 两分钟搞定VS下第三方库的配置(以GNU Regex Library库为例)

    写C的朋友大概知道导入一个库的痛苦,特别是在宇宙第一IDE--VS下更是无从下手,生怕一不小心就把VS搞崩了,而VS的卸载过程又是一个十分头疼的过程.所以,这里特此开了一篇如何在VS下配置第三方库的博 ...

  8. 利用数据库拿shell的一些姿势

    0x01.利用MySQL命令导出getshell 利用条件: 1.拥有网站的写入权限 2.Secure_file_priv参数为空或者为指定路径 3.知道网站的绝对路径 方法: 通过into outf ...

  9. day76:luffy:项目前端环境搭建&轮播图的实现

    目录 1.项目前端环境搭建 1.创建项目目录 2.前端初始化全局变量和全局方法 3.跨域CORS 4.axios配置 2.轮播图功能的实现 1.安装依赖模块 2.上传文件相关配置 3.注册home子应 ...

  10. vue 404

    问题描述:前端同事使用Vue.js框架,利用vue-route结合webpack编写了一个单页路由项目,运维协助在服务器端配置nginx.部署完成后,访问首页没问题,从首页里打开二级页面没问题,但是所 ...