循环Queue(队列)
2012-09-22 23:25
260 查看
#include <stdio.h> #include <malloc.h> #define LEN 6 typedef struct queue{ int * pBase; //队列的数组. int front; //出队点. int rear; //入队点. } QUEUE, * PQUEUE; void init(PQUEUE); int en_queue(PQUEUE, int); int full_queue(PQUEUE); void print_queue(PQUEUE); int out_queue(PQUEUE, int *); int empty(PQUEUE pQ); int main(void) { QUEUE Q; int val; init(&Q); en_queue(&Q, 1); en_queue(&Q, 2); en_queue(&Q, 3); en_queue(&Q, 4); en_queue(&Q, 5); print_queue(&Q); if(out_queue(&Q, &val)){ printf("出队:%d\n", val); } if(out_queue(&Q, &val)){ printf("出队:%d\n", val); } en_queue(&Q, 7); en_queue(&Q, 8); print_queue(&Q); return 0; } //队列初始化方法. void init(PQUEUE pQ){ pQ->pBase = (int *)malloc(sizeof(int) * LEN); pQ->front = 0; pQ->rear = 0; } //判断队列是否满. //(pQ->rear+1) % LEN 可实现循环自加. //循环队列核心方法. int full_queue(PQUEUE pQ){ if((pQ->rear+1) % LEN == pQ->front){ return 1; } else { return 0; } } //入队方法. int en_queue(PQUEUE pQ, int val){ if(full_queue(pQ)){ return 0; } else { pQ->pBase[pQ->rear] = val; pQ->rear = (pQ->rear + 1) % LEN; return 1; } } //打印队列里的值. void print_queue(PQUEUE pQ){ int i = pQ->front; while(i != pQ->rear){ printf("%d\n", pQ->pBase[i]); i = (i + 1) % LEN; } } //判断队列是不是为空.如果为空.就不能出列. int empty(PQUEUE pQ){ if(pQ->front == pQ->rear){ return 1; } else { return 0; } } //出列方法. int out_queue(PQUEUE pQ, int * pVal){ if( empty(pQ) ){ return 0; } else { *pVal = pQ->pBase[pQ->front]; pQ->front = (pQ->front + 1) % LEN; return 1; } }
相关文章推荐
- 循环队列Queue–使用链式存储结构实现
- LQueue.h 链式队列,不循环
- SeqCQueue.h 顺序循环队列
- 队列(queue)的链表(list)实现及循环数组(circular array)实现 C++实现
- 循环队列CircularQueue with flag
- Data Structure-2 Queue 循环队列,用数组实现
- uva 133 The Dole Queue(循环队列)
- 事件循环(事件队列)和 Job Queue(作业队列)
- 循环队列_queue
- 队列(queue)的链表(list)实现及循环数组(circular array)实现 C++实现
- 循环队列的实现(Queue, C++版)
- CircleMsgQueue 用于存放 指定大小的,循环的,消息队列
- Queue(1)Queue的顺序实现之循环队列版本
- 循环队列Queue--使用顺序存储结构(数组)实现
- 【数据结构与算法基础】以数组实现的循环队列 / Circular Queue implemented by array
- 爬虫代码实现六-Queue队列实现循环抓取
- 数据结构基础-队列(循环队列、链式队列)以及STL中queue的使用
- priority_queue<int,vector<int>,greater<int>>优先队列 按照由小到大顺序
- Python 多线程|Queue队列|生产者消费者模式|
- 循环队列的基本操作