编程实现顺序队列的入队操作和出队操作,并将出队结果输出

头文件:函数的声明

#include 
#include 
#include 
#define QUEUESIZE 100
typedef char ElemType;
typedef struct
{
	ElemType queue[QUEUESIZE];
	int front;
	int rear;
}SeqQueue;

void InitQueue(SeqQueue *SQ);
int QueueEmpty(SeqQueue SQ);
int EnQueue(SeqQueue *SQ,ElemType e);
int DeQueue(SeqQueue *SQ,ElemType *e);
int Gethead(SeqQueue SQ,ElemType *e);
void ClearQueue(SeqQueue *SQ);


函数的定义

#include "顺序队.h"

void InitQueue(SeqQueue *SQ)
{
	SQ->front = SQ->rear  = 0;
}

int QueueEmpty(SeqQueue SQ)
{
	if(SQ.front == SQ.rear)
	{
		return 1;
	}
	else
	{
		return 0;
	}
}
int EnQueue(SeqQueue *SQ,ElemType e)
{
	if(SQ->rear == QUEUESIZE)
	{
		return 0;
	}
	else
	{
		SQ->queue[SQ->rear] = e;
		SQ->rear = SQ->rear + 1;
		return 1;
	}
}
int DeQueue(SeqQueue *SQ,ElemType *e)
{
	if(SQ->front == SQ->rear)
	{
		return 0;
	}
	else
	{
		*e = SQ->queue[SQ->front];
		SQ->front = SQ->front + 1;
		return 1;
	}
}

int Gethead(SeqQueue SQ,ElemType *e)
{
	if(SQ.front == SQ.rear)
	{
		return 0;
	}
	else
	{
		*e = SQ.queue[SQ.front];
		return 1;
	}
}
void ClearQueue(SeqQueue *SQ)
{
	SQ->front = SQ->rear  = 0;
}



函数的应用

#include "顺序队.h"

int main(void)
{
	SeqQueue Q;
	char str[] = "ABCDEFGH";
	int i;
	char x;
	InitQueue(&Q);
	for(i = 0;i < sizeof(str)/sizeof(str[0]);i++)
	{
		EnQueue(&Q,str[i]);
	}
	DeQueue(&Q,&x);
	printf("出队列的元素是:%c\n",x);
	printf("顺序队列中的元素是:");
	if(!QueueEmpty(Q))
	{

		for(i = Q.front ;i < Q.rear ;i++)
		{
			printf("%c",Q.queue[i]);
		}
	}
	printf("\n");
	return 0;
}


你可能感兴趣的:(C语言)