#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. spring boot:基于profile的多环境配置(spring boot 2.3.4)

    一,为什么要进行多环境配置? 1,没有人会在生产环境中进行开发和测试, 所以通常会有多个环境的划分: 工程师本地的开发环境 进行测试的测试环境 最终上线的生产环境 每个环境对应不同的数据库/缓存等数据 ...

  2. Linux-京西百花山

    百花山有三个收票的入口,分别在门头沟(G109).房山(G108)和河北 108有两个方向上百花山,史家营和四马台.只有史家营方向能开车到山顶. 四马台那边,不住,要坐景区车才行 尽头是1900多米的 ...

  3. JS实现将二维数组生成到页面上

    前言 之前没说过数组,现在来写一下数组 CSS span { border:2px solid skyblue; width:30px; height: 30px; display: inline-b ...

  4. django—Form组件校验方法(is_valid)执行流程

    1.从is_valid方法入手 def is_valid(self): """Return True if the form has no errors, or Fals ...

  5. RPM与YUM使用

    1.RPM 1.1RPM简介 RPM全名RedHat Package Manager 优点: 1. 由于已经编译完成并且打包完毕,所以软件传输与安装上很方便 (不需要再重新编译): 2. 由于软件的信 ...

  6. Docker学习笔记之-部署.Net Core 3.1项目到Docker容器,并使用Nginx反向代理(CentOS7)(一)

    上一节演示如何安装Docker,链接:Docker学习笔记之-在CentOS中安装Docker 本节演示 将.net core 3.1 部署到docker容器当中,并使用 Nginx反向代理,部署平台 ...

  7. h5 语义话标签的意义

    使用语义话标签的意义 语义类标签对开发者更为友好,使用语义类标签增强了可读性,即便是在没有 CSS 的时 候,开发者也能够清晰地看出网页的结构,也更为便于团队的开发和维护. 除了对人类友好之外,语义类 ...

  8. B. Two Fairs 解析(思維、DFS、組合)

    Codeforce 1276 B. Two Fairs 解析(思維.DFS.組合) 今天我們來看看CF1276B 題目連結 題目 給一個連通圖,並給兩個點(\(a,b\)),求有多少點對使得:任一路徑 ...

  9. Java反射:new一个宝可梦吧

    最近写Spring Boot的测试用例会发现经常会有用到@RunWith(SpringRunner.class)的设置注解,关于SpringRunner.class的理解也有点似是而非.其实这种写法是 ...

  10. eclipse之SSH配置spring【二】

    第一篇中配置struts完成(http://www.cnblogs.com/dev2007/p/6475074.html),在此基础上,继续配置spring. web.xml中增加listener,依 ...