队列的创建,入队,出队,遍历
2015-03-05 15:02
387 查看
#include <stdio.h> #include <malloc.h> typedef struct queue{ int * pBase; int front; int rear; }QUEUE; void init(QUEUE *); bool enQueue(QUEUE *,int); bool IsFullQueue(QUEUE *); bool traverseQueue(QUEUE *); bool IsEmptyQueue(QUEUE *); bool outQueue(QUEUE *,int *); int main(void){ QUEUE Q; int qVal; init(&Q); enQueue(&Q,1); enQueue(&Q,2); enQueue(&Q,3); enQueue(&Q,4); enQueue(&Q,5); traverseQueue(&Q); if ( outQueue(&Q,&qVal) ){ printf("出队成功,出队的元素是:%d\n",qVal); } else printf("出队失败!"); traverseQueue(&Q); } void init(QUEUE *qQ){ qQ->pBase = (int *)malloc(sizeof(int)*6); qQ->front = qQ->rear = 0; return; } bool IsFullQueue(QUEUE *qQ){ if ( (qQ->rear+1)%6 == qQ->front ){ //front和rear紧挨着就能判断队列已经满了 return true; } else return false; } bool enQueue(QUEUE *qQ,int pVal){ if (IsFullQueue(qQ)){ return false; } else{ qQ->pBase[qQ->rear] = pVal; qQ->rear = (qQ->rear+1)%6; return true; } } bool traverseQueue(QUEUE *qQ){ if (IsEmptyQueue(qQ)){ return false; } else { int i = qQ->front; //int i = 0;这样写就错了,出队以后还是从第一个开始 while(i != qQ->rear){ printf("%d\n",qQ->pBase[i]); i = (i+1)%6; } } return true; } bool IsEmptyQueue(QUEUE *qQ){ if (qQ->front == qQ->rear){ //注意判断是否满的条件和是否为空的条件 return true; } else return false; } bool outQueue(QUEUE *qQ,int *qVal){ if (IsEmptyQueue(qQ)){ return false; } else{ *qVal = qQ->pBase[qQ->front]; qQ->front = (qQ->front+1)%6; //后移一个 return true; } }
相关文章推荐
- c之队列相关操作------初始化,入队,出队,队列清空,销毁,遍历
- 顺序队列的创建、初始化、判断空、入队、出队、读取队头元素、置空、求队长操作
- 队列的创建 入队出队 Java数据结构与算法
- 队列的创建、入队、出队、打印、统计队列长度
- 队的创建,出队,入队,遍历等操作
- 链队列和循环队列的表示与实现-初始化、判空、求长、入队、出队、遍历、求头、求尾、清队、毁队
- 顺序队列的入队,出队,遍历队列计算队列的长度
- 队列中入队,出队,遍历算法代码演示
- 顺序队列的创建、入队、出队
- 子函数实现链式队列 的 操作,创建、入队、出队、退出。
- 编程实现队列的入队、出队、测长、打印
- (10.15)[转]C++学习(队列操作,入队,出队)
- 2个堆栈实现自定义队列的入队出队方法 - 调用者定义2个栈的容量
- 无锁环形队列在网口接收(双线程--一个入队,一个出队)的应用
- 编程实现队列入队出队操作
- queue队列容器入队,出队,读取队首元素,队尾元素,判断是否为空
- 队列的入队、出队操作实现
- 链队列的建立、判空、入队、出队、求长、访头、清空和销毁
- 队列之出队入队
- 链表初解(四)——队列的入队和出队