《数据结构》循环队列基本操作与实现

#include "stdio.h"
#include  "stdlib.h"
#define TRUE 1
#define ERROR 0
#define OK 1
#define FALSE 0
#define OVERFLOW -1

typedef int QElemType;
typedef int Status;
#define MAXQSIZE 100
typedef struct  
{
	QElemType *base;
	int front;
	int rear;
}SqQueue;

//建一个空循环队列
Status InitSqQueue(SqQueue *Q){
	Q->base=(QElemType*)malloc(MAXQSIZE*sizeof(QElemType));
	if(!Q->base) exit (OVERFLOW);
	Q->front=Q->rear=0;
	return OK;
}

// 求队列长
int LengthSqQueue(SqQueue Q)
{
	return (Q.rear-Q.front+MAXQSIZE)%MAXQSIZE;
}

// 入队列操作(新增队尾元素)
Status EnSqQueue(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 DeSqQueue(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 EmptySqQueue(SqQueue Q)
{ 
	if(Q.front==Q.rear) 
		return TRUE;
	else 
		return FALSE;
}

// 取队头元素操作(取出循环队列e的队头元素值)
Status Getfront(SqQueue *Q,QElemType *e)
{ 
	if(Q->front==Q->rear)
		return ERROR;
	else
	*e=Q->base[Q->front];              
		return OK;
}

//队列销毁
Status DestroySqQueue(SqQueue Q)
{
	while ( Q.front!= Q.rear)
	{
	  free(&Q.base[Q.front]);
	  Q.front=(Q.front+1)%MAXQSIZE;
	}
	return OK;
}

//清空循环队列
Status  ClearSqQueue(SqQueue Q)
{  
	int i=Q.front;
	while (i != Q.rear)
	   {
	   i=0;
	   i++;
	   }
	   Q.front=Q.rear=0;
}

//输出
void PrintSqQueue (SqQueue Q)
{ 
	int i;
	i=Q.front;
	while(i!=Q.rear)
	  { 
		printf("%d\t",Q.base[i]);
		i=(i+1)%MAXQSIZE;
	  }
	  printf("\n");
}

void main()
{
	SqQueue Q;
	int choice=1;
	if(InitSqQueue(&Q)==OK)
		printf("InitSqQueue success! ");
	else
		printf("InitSqQueue fail!");
	while(choice!=0)
	{
		system("cls");
		printf("*********************************************************\n");
		printf("   1.SqQueueEmpty  2.SqQueueLength  3.SqQueueClear  4.DeQueue\n");
		printf("   5.SqQueuePrint  6.EnQueue       7.SqQueueDestroy      8.Getfront\n");
		printf("   0.Exit\n");
		printf("*********************************************************\n");
		printf("please input your choice:");
		scanf("%d",&choice);
		switch(choice)
		{
			case 0:
			system("cls");
			printf("Bye Bye!\n");
			break;
		case 1:
			if(EmptySqQueue(Q)==TRUE)
				printf("The Queue is empty!\n");
			else
				printf("The Queue is not empty!\n");
			break;
		case 2:
			printf("The QueueLength is %d\n",LengthSqQueue(Q));
			break;
		case 3:
			if(ClearSqQueue(Q)==OK)
				printf("The Queue is Cleared!");
			else
				printf("The Queue is not Cleared!");
			break;
		case 4:
			{
				QElemType e;
				if(DeSqQueue(&Q,&e)==OK)
				{
					printf("删除成功!\n");
					printf("删除队头元素后的序列:\n");
					PrintSqQueue(Q);
				}
				break;
				}
		case 5:
			PrintSqQueue(Q);
			break;
		case 6:
			{
				QElemType e;
				printf("请输入新的队尾元素\n");
				scanf("%d",&e);
				if(EnSqQueue(&Q,e)==OK)
				{
					printf("增加成功!\n");
					printf("增加新的队尾元素后的序列:\n");
					PrintSqQueue(Q);
				}
				break;
				}
		case 7:
			if(DestroySqQueue(Q)==TRUE)
				printf("The Queue is Destroyed !\n");
			else
				printf("The Queue is not Destroyed!\n");
			break;
		case 8:
			{
			QElemType e;
			if(Getfront(&Q,&e)==OK)
			printf("该队列头元素为:%d\n",e);
			break;
			}				
		default:
			break;

		}
		system("pause");
	}
	



}

 

你可能感兴趣的:(数据结构C语言版,代码实现,项目)