数据结构之循环队列

来源:互联网 发布:向日葵之夜 知乎 编辑:程序博客网 时间:2024/05/22 13:45
/*****************************\*数据结构之队列(循环队列)*IDE:VS2010\*****************************//**  队列操作:*初始化、入队(插入队尾)、出队(即取队头)、判断队列是否非空/满*/#include <iostream>using namespace std;#define MAX_LEN 100//定义节点类型typedef struct queue{int data[MAX_LEN];int front;int rear;}queue;//初始化void initQueue(queue* q){q->front = q->rear = 0;}//是否为空bool isEmpty(queue* q){return (q->front == q->rear) ? true : false;}//是否为满bool isFull(queue* q){return ((q->rear+1)%MAX_LEN == q->front) ? true : false;}//入队bool push(queue* q, int x){if(!isFull(q)){q->data[q->rear] = x;q->rear = (q->rear+1)%MAX_LEN;return true;}return false;}//出队bool pop(queue* q, int* x){if(!isEmpty(q)){*x = q->data[q->front];q->front = (q->front+1)%MAX_LEN;return true;}return false;}

0 0
原创粉丝点击