queue.cpp

来自「自己写的数据结构代码」· C++ 代码 · 共 66 行

CPP
66
字号
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#include <math.h>
#include <process.h>

#define OK 1
#define ERROR 0
typedef int Status;
typedef int QElemType;
#define MAXQSIZE 10000

struct SqQueue
{
	QElemType *base;
	int frount;
	int rear;
};

Status InitQueue(SqQueue &Q)
{
	Q.base=(QElemType *)malloc(MAXQSIZE*sizeof(QElemType));
	Q.frount=Q.rear=0;
	return OK;
}

Status EnQueue(SqQueue &Q,QElemType e)
{
	if((Q.rear+1)%MAXQSIZE==Q.frount)  return ERROR;
	Q.base[Q.rear]=e;
	Q.rear=(Q.rear+1)%MAXQSIZE;
	return OK;
}

int QueueTraverse(SqQueue Q)
{
	int i;
	i=Q.frount;
	if((Q.rear+1)%MAXQSIZE==Q.frount)  return ERROR;
	while(i!=Q.rear)
	{
		printf("%d ",(*(Q.base+i)));
		i=(i+1)%MAXQSIZE;
	}
	printf("\n");
	return OK;
}

int main()
{
	int i=0;
	QElemType d;
	SqQueue Q;
	InitQueue(Q);
	do
	{
		scanf("%d",&d);
		if(d==-1)  break;
		i++;
		EnQueue(Q,d);
	}while(i<MAXQSIZE-1);
	QueueTraverse(Q);
	return 0;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?