作者在 2010-06-20 16:51:07 发布以下内容
#include <stdio.h>
#include <malloc.h>
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
#define OVERFLOW -2
typedef int Status;
typedef int QElemType;
#define MAXQSIZE 6
typedef struct{
QElemType *base;
int front;
int rear;
}SqQueue;
#include <malloc.h>
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
#define OVERFLOW -2
typedef int Status;
typedef int QElemType;
#define MAXQSIZE 6
typedef struct{
QElemType *base;
int front;
int rear;
}SqQueue;
Status InitQueue(SqQueue &Q)
{
Q.base=(QElemType*)malloc(MAXQSIZE*sizeof(QElemType));
if(!Q.base)
return (OVERFLOW);
Q.front=Q.rear=0;
return OK;
}
{
Q.base=(QElemType*)malloc(MAXQSIZE*sizeof(QElemType));
if(!Q.base)
return (OVERFLOW);
Q.front=Q.rear=0;
return OK;
}
int QueueLength(SqQueue Q)
{
return (Q.rear-Q.front+MAXQSIZE)%MAXQSIZE;
}
{
return (Q.rear-Q.front+MAXQSIZE)%MAXQSIZE;
}
Status EnQueue(SqQueue &Q,QElemType e)
{
if((Q.rear+1)%MAXQSIZE==Q.front) return ERROR;
Q.base[Q.rear]=e;
Q.rear=(Q.rear+1)%MAXQSIZE;
return OK;
}
Status DeQueue(SqQueue &Q,QElemType &e){
if(Q.front==Q.rear) return ERROR;
e=Q.base[Q.front];
Q.front=(Q.front+1)%MAXQSIZE;
return OK;
}
Status QueueEmpty(SqQueue Q){
if(Q.front==Q.rear) return true;
else return false;
}
{
if((Q.rear+1)%MAXQSIZE==Q.front) return ERROR;
Q.base[Q.rear]=e;
Q.rear=(Q.rear+1)%MAXQSIZE;
return OK;
}
Status DeQueue(SqQueue &Q,QElemType &e){
if(Q.front==Q.rear) return ERROR;
e=Q.base[Q.front];
Q.front=(Q.front+1)%MAXQSIZE;
return OK;
}
Status QueueEmpty(SqQueue Q){
if(Q.front==Q.rear) return true;
else return false;
}
void main(){
SqQueue Q;
QElemType e;
InitQueue(Q);
printf("输入一个入队列的数(以-1表示结束):");
scanf("%d",&e);
while(e!=-1){
EnQueue(Q,e);
printf("\n输入下一个入队列的数(以-1表示结束):");
scanf("%d",&e);
}
if(QueueEmpty(Q)) printf("队列为空。\n");
else{
printf("\n队列不为空,其元素个数为:%d\n", QueueLength(Q));
printf("\n元素出队列的顺序为:\n");
while(Q.rear!=Q.front){
DeQueue(Q,e);
printf("%d ",e);
}
}
return;
}
SqQueue Q;
QElemType e;
InitQueue(Q);
printf("输入一个入队列的数(以-1表示结束):");
scanf("%d",&e);
while(e!=-1){
EnQueue(Q,e);
printf("\n输入下一个入队列的数(以-1表示结束):");
scanf("%d",&e);
}
if(QueueEmpty(Q)) printf("队列为空。\n");
else{
printf("\n队列不为空,其元素个数为:%d\n", QueueLength(Q));
printf("\n元素出队列的顺序为:\n");
while(Q.rear!=Q.front){
DeQueue(Q,e);
printf("%d ",e);
}
}
return;
}