/***************利用数组模拟循环队列***************/
#include "stdio.h"
#define MAXSIZE 10
typedef struct
{
int data[MAXSIZE];//数据的存储区
int front,rear;//队头对尾指针
}c_SeQueue;//循环队
void Init_SeQueue(c_SeQueue *q)//初始化队列
{
q->front=q->rear=0;
}
int In_SeQueue(c_SeQueue *q,int x)//入队列
{
if((q->rear+1)%MAXSIZE==q->front)
{
printf("队满");
return 0;
}
else
{
q->rear=(q->rear+1)%MAXSIZE;
q->data[q->rear]=x;
return 1;//入队完成
}
}
int Out_SeQueue(c_SeQueue *q,int *x)//出队列
{
if(q->rear==q->front)
{
printf("队空");
return 0;//队空不能出队
}
else
{
q->front=(q->front+1)%MAXSIZE;
*x=q->data[q->front];//读出队头元素
return 1;//出队完成
}
}
int Empty_SeQueue(c_SeQueue *q)//判队空
{
if(q->rear==q->front)
return 1;
else
return 0;
}
int Full_SeQueue(c_SeQueue *q)//判队满
{
if((q->rear+1)%MAXSIZE==q->front)
return 1;
else
return 0;
}
int main()
{
c_SeQueue q,* cq=&q;
int select,i;
int y,z;
Init_SeQueue(cq);
printf("\n(1)Input data");
printf("\n(2)Output data");
printf("\n(3)Exit");
printf("\nPlease select ont=>");
scanf("%d",&select);
do
{
switch(select)
{
case 1:printf("\nPlease input the data=>");
scanf("%d",&y);
In_SeQueue(cq,y);
printf("\nthe elements are:\n");
for(i=(cq->front+1)%MAXSIZE;i!=(cq->rear+1)%MAXSIZE;i=(i+1)%MAXSIZE)
printf("%d ",cq->data[i]);//输出队列元素
break;
case 2:Out_SeQueue(cq,&z);
printf("\nthe elments are:\n");
for(i=(cq->front+1)%MAXSIZE;i!=(cq->rear+1)%MAXSIZE;i=(i+1)%MAXSIZE)
printf("%d ",cq->data[i]);//输出队列元素
break;
}
printf("\n(1)Input data");
printf("\n(2)Output data");
printf("\n(3)Exit");
printf("\nPlease select ont=>");
scanf("%d",&select);
printf("\n");
}
while(select!=3);
}
/***************利用链表模拟链队列***************/
#include "stdio.h"
#include<stdlib.h>
#define MAXSIZE 10
typedef struct node
{
int data;
struct node *next;
}QNode;//链队结点的类型
typedef struct
{
QNode *front,*rear;
}LQueue;//将头尾指针封装在一起的链队
void Init_LQueue(LQueue *q)
{
q->front=(QNode *)malloc(sizeof(QNode));//申请链队头结点
q->front->next=NULL;
q->rear=q->front;
}
void In_LQueue(LQueue *q,int x)
{
QNode *p;
p=(QNode *)malloc(sizeof(QNode));//申请新结点
p->data=x;
p->next=NULL;
q->rear->next=p;
q->rear=p;
}
int Empty_LQueue(LQueue *q)
{
if(q->front==q->rear)
return 1;
else
return 0;
}
int Out_LQueue(LQueue *q,int *x)
{
QNode *p;
if(Empty_LQueue(q))
{
printf("队空");
return 0;
}//队空,出队失败
else
{
p=q->front->next;
q->front->next=p->next;
*x=p->data;//队头元素放x中
free(p);
if(q->front->next==NULL)
q->rear=q->front;//只有一个元素时,出队后队空,此时要修改队尾指针
return 1;
}
}
int main()
{
LQueue q,*lq=&q;
QNode *p;
int select;
int y,z;
Init_LQueue(lq);
printf("\n(1)Input data");
printf("\n(2)Output data");
printf("\n(3)Exit");
printf("\nPlease select one=>");
scanf("%d",&select);
do
{
switch(select)
{
case 1:printf("\nPlease input the data=>");
scanf("%d",&y);
In_LQueue(lq, y);
printf("\nthe elements are:\n");
p=lq->front->next;
while(p!=NULL)
{
printf("%d",p->data);
p=p->next;
}
printf("\n");
break;
case 2:Out_LQueue(lq,&z);
printf("\nthe elements are:\n");
p=lq->front->next;
while(p!=NULL)
{
printf("%d",p->data);
p=p->next;
}
printf("\n");
break;
}
printf("\n(1)Input data");
printf("\n(2)Output data");
printf("\n(3)Exit");
printf("\nPlease select one=>");
scanf("%d",&select);
printf("\n");
}
while (select!=3);
}