#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;
}
#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;
}