一、h文件:my_que.h

#ifndef  _MY_QUE_H_
#define _MY_QUE_H_
struct QueRecord;
typedef struct QueRecord* queue; typedef int element_type; int IsEmpty(queue q);
int IsFull(queue q);
queue creat_que(int max_element);
void make_empty(queue q);
void enqueue(element_type x,queue q);
element_type front_que(queue q);
void dequeue(queue q);
element_type front_deque(queue q);
void dispose_que(queue q); #define mini_que 5 struct QueRecord
{
int capacity;
int size;
int front;
int rear;
element_type *array;
}; #endif

二、c文件:my_que.c

hangma@ubuntu:~/test/test/protest/que_test$ cat my_que.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "my_que.h" int IsEmpty(queue q)
{
return q->size == 0;
} int IsFull(queue q)
{
return q->size == q->capacity;
} queue creat_que(int max_element)
{
queue q; if(max_element < mini_que)
{
printf("the size of que is too small\n");
exit(-2);
} q = (queue)malloc(sizeof(struct QueRecord));
if(q == NULL)
{
printf("can't alloca memory\n");
exit(-1);
} q->array = (element_type *)malloc(max_element * sizeof(element_type));
if(q->array == NULL)
{
printf("can't alloca the mem\n");
exit(-1);
}
q->capacity = max_element; make_empty(q);
return q;
} void make_empty(queue q)
{
if(q != NULL)
{
q->size = 0;
q->front = 1;
q->rear = 0;
}
} int IsQueEnd(int value,queue q)
{
if( ++value == q->capacity)
return 0;
else
return value;
} void enqueue(element_type x,queue q)
{
if(q == NULL)
{
printf("the que is not exsit\n");
exit(-2);
} if(IsFull(q))
{
printf("the que is full\n");
exit(-2);
} q->size++;
q->rear = IsQueEnd(q->rear,q);
q->array[q->rear] = x;
} element_type front_que(queue q)
{
if(IsEmpty(q))
{
printf("the que is empty\n");
exit(-3);
} return q->array[q->front];
} void dequeue(queue q)
{
if(IsEmpty(q))
{
printf("the que is empty\n");
exit(-4);
} q->size--;
q->front = IsQueEnd(q->front,q);
} element_type front_deque(queue q)
{
if(IsEmpty(q))
{
printf("the que is empty");
exit(-5);
} q->size--;
int front = q->front;
q->front = IsQueEnd(q->front,q);
return q->array[front];
} void dispose_que(queue q)
{
if(q)
{
if(q->array)
{
free(q->array);
}
free(q);
}
} int main(int argc ,char *argv[])
{
element_type val;
int i = 0;
queue q; q = creat_que(10);
while( ++i <= 10 )
{
printf("now ,please input the value:\n");
scanf("%d",&val);
printf("the val is %d\n",val);
enqueue(val,q);
printf("the q size is %d\n",q->size);
} while(q->size)
{
val = front_deque(q);
printf("the val is %d\n",val);
sleep(1);
} dispose_que(q);
return 0;
}

三、打印输出:

hangma@ubuntu:~/test/test/protest/que_test$ ./my_que
now ,please input the value:
1
the val is 1
the q size is 1
now ,please input the value:
2
the val is 2
the q size is 2
now ,please input the value:
3
the val is 3
the q size is 3
now ,please input the value:
4
the val is 4
the q size is 4
now ,please input the value:
5
the val is 5
the q size is 5
now ,please input the value:
6
the val is 6
the q size is 6
now ,please input the value:
7
the val is 7
the q size is 7
now ,please input the value:
8
the val is 8
the q size is 8
now ,please input the value:
9
the val is 9
the q size is 9
now ,please input the value:
10
the val is 10
the q size is 10
the val is 1
the val is 2
the val is 3
the val is 4
the val is 5
the val is 6
the val is 7
the val is 8
the val is 9
the val is 10

最新文章

  1. 安卓贴图源码---&gt;单点触控.多点触控.类似in/百度魔图
  2. Javascript编程风格
  3. Sqlsever
  4. spring3.0注解
  5. Redis事务的分析及改进
  6. idea编辑器HttpServlet httpServlet = ServletActionContext.getServletContext().getRealPath();方法无法使用
  7. Uploadify 3.2 参数属性、事件、方法函数详解
  8. 一个灵巧的Delphi多播实事件现方案
  9. 从 mian 函数开始一步一步分析 nginx 执行流程(四)
  10. Tree 使用方式
  11. 20.C++- &quot;&amp;&amp;&quot;,&quot;||&quot;逻辑重载操作符的缺陷、&quot;,&quot;逗号重载操作符的分析
  12. 冲刺No.3
  13. 我的python渗透测试工具箱之自制netcat
  14. getString与optString的区别
  15. C++整形转化成string类型---路径拼接在批处理程序中的应用
  16. Revit API创建标注NewTag
  17. python中configparser模块的使用
  18. plsql 创建表空间、用户、赋予权限
  19. 微信小程序公共组件的引用与控制
  20. Storm相关笔记(包括Kafka和HBase)

热门文章

  1. APACHE的伪静态设置
  2. Python之路:Python 函数
  3. ubuntu下vpn无反应的解决办法
  4. Qt的信号槽,一个老MFC的经验
  5. apache .htaccess文件详解和配置技巧总结
  6. UVA 270 Lining Up 共线点 暴力
  7. 探索Android中的Parcel机制(上)
  8. EntityFramework经典的left join语法
  9. android 实现蓝牙自动配对连接
  10. AsyncQueryHandler处理数据