// 队列的数组实现
#include <cstdio>
#include <cstdlib>
//#define _OJ_
#define maxsize 5
typedef struct Lnode
{
int front;
int rear;
int *elem;
} deque, *Deque;
void
creat_deque(Deque q)
{
q->elem = (int*) malloc (maxsize * sizeof(int));
q->front = q->rear = 0;
}
int
isfull(Deque q)
{
if((q->rear + 1) % maxsize == q->front)
return 1;
else
return 0;
}
int
isempty(Deque q)
{
if(q->front == q->rear)
return 1;
else
return 0;
}
int
deque_size(Deque q)
{
return (q->front - q->rear + maxsize) % maxsize;
}
void
endeque(Deque q)
{
int x;
scanf("%d", &x);
q->elem[q->rear] = x;printf("%d
", q->rear);
q->rear = (q->rear + 1) % maxsize;
}
int
del_deque(Deque q)
{
int e;
e = q->elem[q->front];
q->front = (q->front + 1) % maxsize;
return e;
}
int main(int argc, char const *argv[]) {
#ifndef _OJ_ //ONLINE_JUDGE
freopen("input.txt", "r", stdin);
#endif
Deque q;
creat_deque(q);
printf("isempty == %d
", isempty(q));
printf("size == %d
", deque_size(q));
printf("full == %d
", isfull(q));
int n, i;
n = 3;
do
{
endeque(q);
}
while (!isfull(q)) ;
while (!isempty(q)) {
printf("del == %d
", del_deque(q));
}
//值得注意的是此数组只可以存入 maxsize -1 个数据
return 0;
}