C语言设计模式(应用)
#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语言设计模式(应用)的更多相关文章
- Go语言设计模式之函数式选项模式
Go语言设计模式之函数式选项模式 本文主要介绍了Go语言中函数式选项模式及该设计模式在实际编程中的应用. 为什么需要函数式选项模式? 最近看go-micro/options.go源码的时候,发现了一段 ...
- C语言设计模式-封装-继承-多态
快过年了,手头的工作慢慢也就少了,所以,研究技术的时间就多了很多时间,前些天在CSDN一博客看到有大牛在讨论C的设计模式,正好看到了,我也有兴趣转发,修改,研究一下. 记得读大学的时候,老师就告诉我们 ...
- Go语言设计模式实践:迭代器(Iterator)
关于本系列 决定开个新坑. 这个系列首先是关于Go语言实践的.在项目中实际使用Go语言也有段时间了,一个体会就是不论是官方文档.图书还是网络资料,关于Go语言惯用法(idiom)的介绍都比较少,基本只 ...
- Go语言设计模式实践:组合(Composite)
关于本系列 这个系列首先是关于Go语言实践的.在项目中实际使用Go语言也有段时间了,一个体会就是不论是官方文档.图书还是网络资料,关于Go语言惯用法(idiom)的介绍都比较少,基本只能靠看标准库源代 ...
- Go语言设计模式汇总
目录 设计模式背景和起源 设计模式是什么 Go语言模式分类 个人观点 Go语言从面世就受到了业界的普遍关注,随着区块链的火热Go语言的地位也急速蹿升,为了让读者对设计模式在Go语言中有一个初步的了解和 ...
- C语言设计模式
一 .C语言和设计模式(继承.封装.多态) C++有三个最重要的特点,即继承.封装.多态.我发现其实C语言也是可以面向对象的,也是可以应用设计模式的,关键就在于如何实现面向对象语言的三个重要属性. ( ...
- go语言设计模式之Concurrency workers pool
worker.go package main import ( "fmt" "strings" ) type WorkerLauncher interface ...
- go语言设计模式之Concurrency pipeline
pipeline.go package pipeline func LaunchPipeline(amount int) int { firstCh := generator(amount) seco ...
- go语言设计模式之Concurrency future
future.go package future type SuccessFunc func(string) type FailFunc func(error) type ExecuteStringF ...
- go语言设计模式之Concurrency barrier
barrier.go package barrier import ( "fmt" "io/ioutil" "net/http" " ...
随机推荐
- Python之数据类型总结
1.字符串 2.数字 3.列表 4.元组 5.字典 可变 or 不可变 1:可变:列表.字典 2:不可变:字符串,数字,元组 访问顺序 1.直接访问:数字 2.顺序访问:字符串,列表,元组 3.映射访 ...
- Windows环境下vscode Live Server插件如何开启https
0x01 vscode http插件 Live Server如何开启https 在本机端的开发环境下,如果要测试一些需要HTTPS的功能可以使用mkcert给自己颁发凭证 0x02 安装步骤如下: 1 ...
- 趣谈多线程(Python版)
温馨提示:本文篇幅较长,建议读者耐心阅读,本文中的代码经过笔者精心构思,可以复制过去运行一下,观察输出结果,所有代码在python3.5.0中测试通过. 文章目录 What is 多线程? Why w ...
- 2020年的UWP(2)——In Process App Service
最早的时候App Service被定义为一种后台服务,类似于极简版的Windows Service.App Service作为Background Task在宿主UWP APP中运行,向其他UWP A ...
- 解决Django本地接口不能跨域访问的问题
1.安装django-cors-headers模块: pip install django-cors-headers 2.插入Django的APP配置中: # 修改settings.py中的INSTA ...
- ubuntu 搭建samba服务器&挂载(mount)代码到本地
一.搭建samba服务器 1.下载: sudo apt-get install samba samba-common 2.创建共享文件夹MyShare: mkdir /home/user/MyShar ...
- 前端基础——HTML(一)
HTML html超文本标记语言 前端三层 HTML结构层 css样式层 JavaScript行为层 其他多媒体内容(图片,音频等等) 互联网运行过程 客 --http请求--> 服 户 htt ...
- list.add方法参数详解
- Redis【二】 set|get那些事
redis4.0.9 SET\GET方法 从哪里开始 server.c里面有每个redis命令对应的执行方法 如 struct redisCommand redisCommandTable[] = { ...
- 浅析Java Web框架技术
一.Java Web框架技术的概念 所谓的Java框架,简单理解是一个可复用的设计构件,它规定了应用的体系结构,阐明了整个设计.协作构件之间的依赖关系.责任分配和控制流程,表现为一组抽象类以及其实例之 ...