队列的实现
2015-08-16 11:28
375 查看
#include <stdio.h> #include <stdlib.h> #include <time.h> #include <malloc.h> #include <ctype.h> #define element int #define max__queue 10 typedef struct queue { element *array; int capacity;//队列预分配大小 int front;//队头--删除 int rear;//队尾----插入 int size;//当前队列大小 } queue, *queue_point; int isempty(const queue_point s)//判断队列是否空闲 { return s->size == 0; } int isfull(const queue_point s)//判断队列是否满 { return s->capacity <= s->size; } void makeempty(queue_point s)//使队列为空 { s->size = 0; s->capacity = 0; s->front = -1; s->rear = -1; } void circle_queue(queue_point s)//队列循环 { s->front = -1; } queue_point create_queue(const int max_queue)//创建一个队列 并返回 { queue_point a = calloc(1, sizeof(queue)); if (a == NULL) { printf("calloc error"); } a->array = (element*)calloc(max_queue, sizeof(element)); if (a->array == NULL) { printf("calloc error"); } makeempty(a); a->capacity = max_queue; return a; } void enqueue(element data, queue_point s)//尾部 进队 { if (isfull(s)) { printf("queue is full"); } else if (s == NULL) { s = create_queue(max__queue); } if (s->rear + 1 == s->capacity) { s->rear == -1; } s->rear++; s->size += 1; s->array[s->rear] = data; } element dequeue(queue_point s)//头部 出队 { if (s->front + 1 == s->capacity) { s->rear == -1; } s->front++; s->size -= 1; return s->array[s->front]; } void display_queue(const queue_point s)//输出队列的数据 { int b = s->size; while (b) { printf("%d ", s->array[s->rear - s->size + b]); b--; } }
#include "20150524.c" int main() { queue_point duilie = create_queue(10); srand(time(NULL)); enqueue(rand() % 100 + 1, duilie); enqueue(rand() % 100 + 1, duilie); enqueue(rand() % 100 + 1, duilie); enqueue(rand() % 100 + 1, duilie); enqueue(rand() % 100 + 1, duilie); //printf("%d\n", duilie->rear); display_queue(duilie); dequeue(duilie); display_queue(duilie); return 0; }
相关文章推荐
- Spring4.x整合Quartz2.x 动态修改定时器时间(修正多定时器SpringBean异常)
- linux学习之路(1)
- scala学习之:List的map、flatMap、foreach、filter的操作代码实战
- oracle中的赋权
- Android启动页
- eclipse:No resource found that matches the given name 'Theme.AppCompat.Light'
- 砝码称重
- C语言实现字符串的左旋转
- [clone]Java中的深拷贝和浅拷贝 实例解析
- 快速判断一个数字是否是素数
- BGP相关资料
- xcode 各种项目设置
- 二叉搜索树的后序遍历序列(算法)
- POJ 2559 Largest Rectangle in a Histogram
- CentOS yum 源的配置与使用
- 关情纸尾-----UIKit基础-超级猜图
- 从输入整数问题
- arm中断控制led
- LayoutInflater.inflate源码分析
- Andriod Studio科普文章——3.大约gradle常见问题插头