C语言实现循环队列

#include "stdafx.h"
#include <stdlib.h>


#define m 5


typedef int datatype;


typedef struct Queue{
    datatype sequ[m];
int rear,front;
int quelen;
}QUEUE,*PQUEUE;


PQUEUE Create_queue()
{
    PQUEUE p;
p=(PQUEUE)malloc(sizeof(QUEUE));
if(!p){printf("Memory allocation failed");exit(0);}
else{
p->rear=0;
p->front=0;
p->quelen=0;
return p;
}
}


void Setnull_queue(PQUEUE p)
{
    PQUEUE p1=p;
p1->quelen=0;
p1->rear=0;
p1->front=0;
}


bool En_queue(PQUEUE p, datatype x)
{
PQUEUE p1=p;
if(p1->quelen==m){printf("Error!The queue will be overflow!\n");return 0;}
else{
p1->sequ[p1->rear]=x;
//printf("En number is %d\n",x);
p1->rear=(p1->rear+1)%m;
p1->quelen++;
//printf("EN : front:%d  rear:%d\n",p->front,p->rear);
    return 1;
}


}


datatype De_queue(PQUEUE p)
{
    PQUEUE p1=p;
datatype x;
if(p1->quelen==0){
printf("Error!The queue will be under flow!\n");
exit(0);
}
else{
p1->quelen--;
x=p1->sequ[p1->front];
p1->front=(p1->front+1)%m;
//printf("DE : front:%d  rear:%d\n",p->front,p->rear);
return x;
}
}


void Show_queue(PQUEUE p)
{   
int i;
PQUEUE p1=p;
if(p1->quelen==0){printf("The queue is null!\n");}
else{
if(p1->front == p1->rear){
i=p1->quelen;
while(i){
if(p1->front >= 5) p1->front=(p1->front)%m;
printf("%4d",p1->sequ[p1->front]);
i--;
p1->front=(p1->front+1)%m;
}
printf("\n");
}
else{
for(i=p1->front;i!=(p1->rear);i=(i+1)%m){
printf("%4d",p1->sequ[i]);
}
printf("\n");
}
}
}


int _tmain(int argc, _TCHAR* argv[])
{
PQUEUE p=Create_queue();
int i;
int x;
for(i=0;i<6;i++){
if(En_queue(p,i))
printf("入队成功!\n");
else
printf("入队失败!\n");
    }


Show_queue(p);

De_queue(p);
Show_queue(p);


En_queue(p,5);
Show_queue(p);

De_queue(p);
Show_queue(p);

En_queue(p,6);
Show_queue(p);

De_queue(p);
Show_queue(p);

En_queue(p,7);
Show_queue(p);


De_queue(p);
Show_queue(p);

En_queue(p,8);
Show_queue(p);


De_queue(p);
Show_queue(p);

En_queue(p,9);
Show_queue(p);
return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值