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" " ...
随机推荐
- centos8安装RabbitMQ
一.安装erlang # 添加仓库 curl -s https://packagecloud.io/install/repositories/rabbitmq/erlang/script.rpm.sh ...
- centos8安装java jdk 13
一,查看本地centos的版本 [root@localhost lib]# cat /etc/redhat-release CentOS Linux release 8.1.1911 (Core) 说 ...
- OSI七层网络
7-应用层 各种应用软件 6-表示层 转换.加密.解密压缩 5-会话层 建立会话,保证会话,终止会话 4-传输层 TCP/UDP 3-网络层 路由 2-数据链路层 交换 1-物理层
- 趣谈多线程(Python版)
温馨提示:本文篇幅较长,建议读者耐心阅读,本文中的代码经过笔者精心构思,可以复制过去运行一下,观察输出结果,所有代码在python3.5.0中测试通过. 文章目录 What is 多线程? Why w ...
- 在 Istio 中实现 Redis 集群的数据分片、读写分离和流量镜像
Redis 是一个高性能的 key-value 存储系统,被广泛用于微服务架构中.如果我们想要使用 Redis 集群模式提供的高级特性,则需要对客户端代码进行改动,这带来了应用升级和维护的一些困难.利 ...
- Redis实现缓存与分布式锁
缓存与分布式锁 哪些数据适合放入缓存 即时性.数据一致性要求不高的 访问量大且更新频率不高的数据 选择redis做为缓存中间件 <dependency> <groupId>or ...
- etc/river.toml
# MySQL address, user and password # user must have replication privilege in MySQL. my_addr = " ...
- 【转】Setting up SDL 2 on Code::Blocks 12.11
FROM: http://lazyfoo.net/tutorials/SDL/01_hello_SDL/windows/codeblocks/index.php Setting up SDL 2 on ...
- mysql分组函数与查询
Ⅰ.分组函数的分类: max():最大值 min():最小值 sum():和 avg():平均值 count():计算非空的个数 这些都是通用的,sqlserver.oracle.mysql都是一样的 ...
- C语言基础-C简介
C语言简介 C 语言是一种通用的高级语言,最初是由丹尼斯·里奇在贝尔实验室为开发 UNIX 操作系统而设计的.C 语言最开始是于 1972 年在 DEC PDP-11 计算机上被首次实现. 在 19 ...