数据结构之循环队列
2014-09-23 15:05
471 查看
#include<iostream> using namespace std; #define QSIZE 8 typedef int ElemType; typedef struct { ElemType *pBase; int Front; int Rear; }Queue;//循环队列比顺序对要节省大量的空间,默认是不用顺序队的 void InitQueue(Queue &Q) { Q.pBase = new ElemType[QSIZE]; Q.Front = 0; Q.Rear = 0; } void DestroyQueue(Queue &Q) { delete[]Q.pBase; Q.Front = 0; Q.Rear = 0; return; } void ClearQueue(Queue &Q) { Q.Front = 0; Q.Rear = 0; return; } bool QueueFull(Queue &Q)//浪费一个单元为代价,当处理大量的数据时是非常值得的. { if ((Q.Rear + 1) % QSIZE == Q.Front) return true; else return false; } bool QueueEmpty(Queue &Q) { if (Q.Front == Q.Rear) return true; else return false; } int QueueLength(Queue &Q) { if (QueueEmpty(Q)) return 0; else return (Q.Rear - Q.Front + QSIZE) % QSIZE; } bool GetHead(Queue Q, ElemType &e) { if (QueueEmpty(Q)) return false; e = Q.pBase[Q.Front]; return true; } bool EnQueue(Queue &Q, ElemType e) { if (QueueFull(Q)) return false; Q.pBase[Q.Rear] = e; Q.Rear = (Q.Rear + 1) % QSIZE; return true; } bool DeQueue(Queue &Q, ElemType &e) { if (QueueEmpty(Q)) return false; e = Q.pBase[Q.Front]; Q.Front = (Q.Front + 1) % QSIZE; return true; } void Visit(ElemType e) { cout << e << " "; } void Traverse(Queue Q) { int i = Q.Front; while (i != Q.Rear) { Visit(Q.pBase[i]); i = (i + 1) % QSIZE; } cout << endl; return; } int main(void) { Queue Q; ElemType e = 0; InitQueue(Q); EnQueue(Q, 1); EnQueue(Q, 2); EnQueue(Q, 3); EnQueue(Q, 4); EnQueue(Q, 5); EnQueue(Q, 6); GetHead(Q, e); cout << e << endl; Traverse(Q); if (QueueEmpty(Q)) cout << "Indeed Empty!" << endl; else cout << "Not Empty!" << endl; cout << QueueLength(Q) << endl; EnQueue(Q, 7); //注意:7个数据则队便满,EnQueue(Q, 8)无效 if (QueueFull(Q)) cout << "Indeed Full!" << endl; else cout << "Not Full!" << endl; Traverse(Q); DeQueue(Q, e); cout << e << endl; Traverse(Q); return(0); }
相关文章推荐
- 五、数据结构基础之循环队列C语言实现
- 数据结构之循环队列
- 看数据结构写代码(16)顺序队列的实现(循环队列)
- 数据结构基础(7) --循环队列的设计与实现
- 【数据结构与算法基础】以数组实现的循环队列 / Circular Queue implemented by array
- 数据结构——循环队列操作
- 数据结构基础(7) --循环队列的设计与实现
- 数据结构之---C语言实现循环队列
- 数据结构基础(7) --循环队列的设计与实现
- 数据结构之循环队列
- 【数据结构】——顺序循环队列的相关操作
- 数据结构——队列及循环队列
- 数据结构之循环队列操作
- 再回首,数据结构——循环队列上的常见操作
- 数据结构(三)——队列及实现、循环队列实现
- 数据结构之循环队列
- 数据结构(C实现)------- 顺序队列(循环队列之少用一个存储空间实现) .
- 数据结构之队列11循环队列定义及实现
- 数据结构与算法之循环队列
- 数据结构之静态循环队列(所有操作)