队列实例程序(C语言)
/* queue.h */ #ifndef _QUEUE_H
#define _QUEUE_H struct queue_record;
typedef struct queue_record *queue; int is_empty( queue q );
int is_full( queue q );
queue create_queue( int max_elements );
void dispose_queue( queue q );
void make_empty( queue q );
void enqueue( int x, queue q );
int front( queue q );
void dequeue( queue q );
int front_and_dequeue( queue q ); #endif
/* queue.c */ #include "queue.h"
#include <stdio.h>
#include <stdlib.h> #define MIN_QUEUE_SIZE 5 struct queue_record
{
int capacity;
int front;
int rear;
int size;
int *array;
}; void
make_empty( queue q )
{
q->size = 0;
q->front = 1;
q->rear = 0;
} int
is_empty( queue q )
{
return q->size == 0;
} int
is_full( queue q )
{
return q->size == q->capacity;
} queue
create_queue( int max_elements )
{
queue q; if( max_elements < MIN_QUEUE_SIZE )
{
printf("Queue size is too small!\n");
exit(0);
} q = malloc( sizeof(struct queue_record) );
if(q == NULL)
{
printf("Out of space!\n");
exit(1);
}
q->array = malloc(sizeof(int) * max_elements);
if(q->array == NULL)
{
printf("Out of space!\n");
exit(1);
}
q->capacity = max_elements; make_empty(q); return q;
} static int
succ( int value, queue q )
{
if( ++value == q->capacity )
value = 0;
return value;
} void
enqueue( int x, queue q )
{
if( is_full( q ) )
{
printf("full queue!\n");
exit(0);
}
else
{
q->size++;
q->rear = succ( q->rear, q );
q->array[q->rear] = x;
}
} void
dequeue( queue q )
{
if( is_empty( q ) )
{
printf("empty queue!\n");
exit(0);
}
else
{
q->size--;
q->front = succ( q->front, q );
}
} int
front( queue q )
{ if( is_empty( q ) )
{
printf("empty queue!\n");
exit(0);
}
else
return q->array[q->front];
} int
front_and_dequeue( queue q )
{
int tmp; if( is_empty( q ) )
{
printf("empty queue!\n");
exit(0);
}
else
{
tmp = q->array[q->front];
q->size--;
q->front = succ( q->front, q );
return tmp;
} } void
dispose_queue( queue q )
{
if( q != NULL )
{
free(q->array);
free(q);
}
}
/* queue_test.c */ #include "queue.h"
#include <stdio.h> int
main(void)
{
queue q;
int tmp;
int i; q = create_queue(10);
printf("1 enqueue\n");
enqueue(1, q);
printf("2 enqueue\n");
enqueue(2, q);
printf("3 enqueue\n");
enqueue(3, q);
printf("4 enqueue\n");
enqueue(4, q);
printf("5 enqueue\n");
enqueue(5, q); printf("\n");
for(i=0; i<5; i++)
{
printf("dequeue %d\n", front_and_dequeue( q )); } }
测试结果:

队列实例程序(C语言)的更多相关文章
- tesseract ocr文字识别Android实例程序和训练工具全部源代码
tesseract ocr是一个开源的文字识别引擎,Android系统中也可以使用.可以识别50多种语言,通过自己训练识别库的方式,可以大大提高识别的准确率. 为了节省大家的学习时间,现将自己近期的学 ...
- Delphi XE5教程3:实例程序
内容源自Delphi XE5 UPDATE 2官方帮助<Delphi Reference>,本人水平有限,欢迎各位高人修正相关错误! 也欢迎各位加入到Delphi学习资料汉化中来,有兴趣者 ...
- MSMQ-发送消息到远程专用队列 实例
目录 一:MSMQ的一些理论上的知识 二:队列类型(Queue Type) 三:安装消息队列 四:在C#中Messagequeue class 五:MSMQ-发送消息到远程专用队列 六:例子 一. ...
- C#网络编程TCP通信实例程序简单设计
C#网络编程TCP通信实例程序简单设计 采用自带 TcpClient和TcpListener设计一个Tcp通信的例子 只实现了TCP通信 通信程序截图: 压力测试服务端截图: 俩个客户端链接服务端测试 ...
- C# 实现单实例程序
在我们经常使用的软件中,当我们已经打开后,再次打开时,有的软件不会出现两个.例如有道词典,会将上次的界面显示出来,或者提示我们“该程序已经运行...”.我通过一个简单的C# WPF例子来说明. 首先我 ...
- Android L Camera2 API 使用实例程序汇总
在网上发现几个使用Camera API2开发的实例程序,总结一下方便后续参考: 1.Camera2 Basic : https://github.com/googlesamples/android-C ...
- Node.js入门实例程序
在使用Node.js创建实际“Hello, World!”应用程序之前,让我们看看Node.js的应用程序的部分.Node.js应用程序由以下三个重要组成部分: 导入需要模块: 我们使用require ...
- 主成分分析、实例及R语言原理实现
欢迎批评指正! 主成分分析(principal component analysis,PCA) 一.几何的角度理解PCA -- 举例:将原来的三维空间投影到方差最大且线性无关的两个方向(二维空间). ...
- 微信小程序开发语言的选择
微信使用的开发语言和文件很「特殊」. 小程序所使用的程序文件类型大致分为以下几种: ①WXML(WeiXin Mark Language,微信标记语言) ②WXSS(WeiXin Style Shee ...
随机推荐
- spring---aop(3)---Spring AOP的拦截器链
写在前面 时间断断续续,这次写一点关于spring aop拦截器链的记载.至于如何获取spring的拦截器,前一篇博客已经写的很清楚(spring---aop(2)---Spring AOP的JDK动 ...
- spring---transaction(3)---源代码分析(事务的管理器PlatformTransactionManager)
写在前面 由于实现事务功能的方式各不相同,Spring进行了统一的抽象,形成了PlatformTransactionManager事务管理器顶级接口(平台事务管理器),事务的提交.回滚等操作全部交给它 ...
- Pyhton 编程风格
博文概要:1.Python语句和语法2.Python标识符3.基本编程风格 一.Python语句和语法 1.可以从一行的任何地方开始2.续行 \: ''':闭合操作符,单一语句跨多行 3.代码组 .缩 ...
- Inspect a new tab · cyrus-and/chrome-remote-interface Wiki
Inspect a new tab · cyrus-and/chrome-remote-interface Wiki Runtime.evaluate
- 吐槽Windows 8,就没见过这么烂的平板操作系统
本文带有严重个人情感色彩,反感者慎入. CSDN 博文大赛得了个奖品,联想的平板电脑, MIIX2 8 .系统是 Windows 8 . 今天媳妇再次使用它,惹得我再次吐槽. 一句话.Windows ...
- web office apps 在线预览实践
摘要 在一些项目中需要在线预览office文档,包括word,excel,ppt等.达到预览文档的目的有很多方法,可以看我之前总结,在线预览的n种方案: [Asp.net]常见word,excel,p ...
- [Apache] Apache 從 2.2 換至 2.4 httpd.conf 的調整筆記 (windows 環境)
原文地址: http://www.dotblogs.com.tw/maplenote/archive/2012/07/20/apache24_httpd_conf.aspx 整理一下 Windows ...
- IE中Ext的comboBox跑到页面左上角
{ xtype:'combo', width:100, //id:'exTypeCom', name:'exType', hiddenName:'exType', displayField:'text ...
- 解决sdk manager下载非常慢或者下载失败
有了sdk manager,打开它,想下载一些须要的东西总是会发现非常慢,然后就仅仅好慢慢等待,等待许久之后最后是失败了,这样就会非常麻烦,以下我总结总结,怎样解决这些问题,让你在分分钟下载好这些东西 ...
- 10.2.2移动产品离线功能等具体解释----暨4月8日移动《在离线一体化》公开课Q&A
4月8日<离,或者不离,ArcGIS移动的"在离线一体化"就在那里!>移动公开课已经结束,针对公开课上粉丝们重点关注的问题,本博客进行了具体的解答.答疑主要环绕最新的R ...