#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#define N 6
//定义的是静态循环队列
typedef struct Queue
{
int *pBase; //数组链表的首地址
int front; //数组元素的下标(队首,含有有效数据)
int rear; //数组元素的下标(队尾,不包含有效数据)
}QUEUE;
//初始化队列
void init_queue(QUEUE*);
//入队
bool en_queue(QUEUE*,int);
//出队
bool out_queue(QUEUE*,int &);
//遍历
void traverse_queue(QUEUE*);
//判断队列是否已满
bool full_queue(QUEUE*);
//判断队列是否为空
bool empty_queue(QUEUE*);
int main(void)
{
QUEUE Q; //定义一个队列变量Q
int data; //存储出队的数据
init_queue(&Q); //初始化队列
en_queue(&Q,1); //入队
en_queue(&Q,2); //定义的最大动态数组长度为6个,为了好判别队列是空还是满,故将第6个元素(a[5])空下来不作存储数据用
en_queue(&Q,3);
en_queue(&Q,4); //如果front == rear 队列则为空,如果 (rear+1) %N == front 队列则满
en_queue(&Q,5);
en_queue(&Q,6); //队列已满,入队失败
en_queue(&Q,7); //队列已满,入队失败
traverse_queue(&Q); //遍历队列
out_queue(&Q,data); //出队
printf("out of data is %d \n",data);
traverse_queue(&Q);
return 0;
}
void init_queue(QUEUE *pq)
{
pq->pBase = (int*)malloc(sizeof(int)*N);
pq->front = 0;
pq->rear = 0;
}
bool en_queue(QUEUE* pq,int val)
{
if(full_queue(pq))
return false;
else
{
pq->pBase[pq->rear] = val;
pq->rear = (pq->rear+1)%N;
return true;
}
}
bool full_queue(QUEUE* pq)
{
if((pq->rear+1)%6 == pq->front)
return true;
return false;
}
void traverse_queue(QUEUE* pq)
{
int i = pq->front;
while(i != pq->rear)
{
printf("%d ",pq->pBase[i]);
i = (i+1) % 6;
}
printf("\n");
}
bool out_queue(QUEUE* pq, int &val)
{
if(empty_queue(pq))
return false;
else
{
val = pq->pBase[pq->front];
pq->front = (pq->front+1)%N;
}
return true;
}
bool empty_queue(QUEUE* pq)
{
if(pq->front == pq->rear)
return true;
return false;
}
/*======================vc6.0++运行结果==========================
1 2 3 4 5
out of data is 1
2 3 4 5
Press any key to continue
======================vc6.0++运行结果==========================*/
#include <malloc.h>
#include <stdlib.h>
#define N 6
//定义的是静态循环队列
typedef struct Queue
{
int *pBase; //数组链表的首地址
int front; //数组元素的下标(队首,含有有效数据)
int rear; //数组元素的下标(队尾,不包含有效数据)
}QUEUE;
//初始化队列
void init_queue(QUEUE*);
//入队
bool en_queue(QUEUE*,int);
//出队
bool out_queue(QUEUE*,int &);
//遍历
void traverse_queue(QUEUE*);
//判断队列是否已满
bool full_queue(QUEUE*);
//判断队列是否为空
bool empty_queue(QUEUE*);
int main(void)
{
QUEUE Q; //定义一个队列变量Q
int data; //存储出队的数据
init_queue(&Q); //初始化队列
en_queue(&Q,1); //入队
en_queue(&Q,2); //定义的最大动态数组长度为6个,为了好判别队列是空还是满,故将第6个元素(a[5])空下来不作存储数据用
en_queue(&Q,3);
en_queue(&Q,4); //如果front == rear 队列则为空,如果 (rear+1) %N == front 队列则满
en_queue(&Q,5);
en_queue(&Q,6); //队列已满,入队失败
en_queue(&Q,7); //队列已满,入队失败
traverse_queue(&Q); //遍历队列
out_queue(&Q,data); //出队
printf("out of data is %d \n",data);
traverse_queue(&Q);
return 0;
}
void init_queue(QUEUE *pq)
{
pq->pBase = (int*)malloc(sizeof(int)*N);
pq->front = 0;
pq->rear = 0;
}
bool en_queue(QUEUE* pq,int val)
{
if(full_queue(pq))
return false;
else
{
pq->pBase[pq->rear] = val;
pq->rear = (pq->rear+1)%N;
return true;
}
}
bool full_queue(QUEUE* pq)
{
if((pq->rear+1)%6 == pq->front)
return true;
return false;
}
void traverse_queue(QUEUE* pq)
{
int i = pq->front;
while(i != pq->rear)
{
printf("%d ",pq->pBase[i]);
i = (i+1) % 6;
}
printf("\n");
}
bool out_queue(QUEUE* pq, int &val)
{
if(empty_queue(pq))
return false;
else
{
val = pq->pBase[pq->front];
pq->front = (pq->front+1)%N;
}
return true;
}
bool empty_queue(QUEUE* pq)
{
if(pq->front == pq->rear)
return true;
return false;
}
/*======================vc6.0++运行结果==========================
1 2 3 4 5
out of data is 1
2 3 4 5
Press any key to continue
======================vc6.0++运行结果==========================*/