#include <stdio.h>
#define MAXSIZE 6 //设置队列的最大长度为100
typedef int ElemType ;
typedef struct{
ElemType elem[MAXSIZE];
/*定义一个一维数组存放队列中各元素,元素类型ElemType根据实际需要确定*/
int front; /*头指针,指向队头元素*/
int rear; /*尾指针,指向队尾元素的下一个位置*/
int len; /*存放队中元素个数,即队列的实际长度*/
}SqQueue;
void InitQueue (SqQueue *q)
{q->front=q->rear=0; q->len=0;}
int QueueEmpty (SqQueue q)
{if (q.len==0) return 1;
else return 0;
}
void EnQueue (SqQueue *q, ElemType e)
{if (q->len==MAXSIZE) //判队满
printf ("Queue is full/n");
else
{q->elem [q->rear]=e;
q->rear=(q->rear+1)%MAXSIZE;
q->len++;}
}
void DeQueue (SqQueue *q, ElemType *e)
{if (q->len==0) //判队空
printf ("Queue is empty/n");
else
{*e=q->elem [q->front];
q->front=(q->front+1)%MAXSIZE;
q->len--;}
}
void GetHead (SqQueue q, ElemType *e)
{if (q.len==0) //判队空
printf ("Queue is empty/n");
else
*e=q.elem [q.front];
}
int main(int argc, char *argv[])
{
SqQueue q;
ElemType x;
InitQueue(&q);
EnQueue(&q,1);
EnQueue(&q,2);
EnQueue(&q,3);
EnQueue(&q,4);
DeQueue(&q,&x);
printf("%d ",x);
DeQueue(&q,&x);
printf("%d ",x);
EnQueue(&q,5);
EnQueue(&q,6);
EnQueue(&q,7);
EnQueue(&q,8);
EnQueue(&q,9);
EnQueue(&q,10);
return 0;
}
#define MAXSIZE 6 //设置队列的最大长度为100
typedef int ElemType ;
typedef struct{
ElemType elem[MAXSIZE];
/*定义一个一维数组存放队列中各元素,元素类型ElemType根据实际需要确定*/
int front; /*头指针,指向队头元素*/
int rear; /*尾指针,指向队尾元素的下一个位置*/
int len; /*存放队中元素个数,即队列的实际长度*/
}SqQueue;
void InitQueue (SqQueue *q)
{q->front=q->rear=0; q->len=0;}
int QueueEmpty (SqQueue q)
{if (q.len==0) return 1;
else return 0;
}
void EnQueue (SqQueue *q, ElemType e)
{if (q->len==MAXSIZE) //判队满
printf ("Queue is full/n");
else
{q->elem [q->rear]=e;
q->rear=(q->rear+1)%MAXSIZE;
q->len++;}
}
void DeQueue (SqQueue *q, ElemType *e)
{if (q->len==0) //判队空
printf ("Queue is empty/n");
else
{*e=q->elem [q->front];
q->front=(q->front+1)%MAXSIZE;
q->len--;}
}
void GetHead (SqQueue q, ElemType *e)
{if (q.len==0) //判队空
printf ("Queue is empty/n");
else
*e=q.elem [q.front];
}
int main(int argc, char *argv[])
{
SqQueue q;
ElemType x;
InitQueue(&q);
EnQueue(&q,1);
EnQueue(&q,2);
EnQueue(&q,3);
EnQueue(&q,4);
DeQueue(&q,&x);
printf("%d ",x);
DeQueue(&q,&x);
printf("%d ",x);
EnQueue(&q,5);
EnQueue(&q,6);
EnQueue(&q,7);
EnQueue(&q,8);
EnQueue(&q,9);
EnQueue(&q,10);
return 0;
}