数据结构(C语言) 队列的表示和实现

队列

队列的存储结构

//----循环队列----
typedef struct {
	QElemType* base;//初始化的动态分配存储空间
	int front;//头指针,若队列不空,指向队列头元素
	int rear;//尾指针,若队列不空,指向队列尾元素的下一个位置
}SqQueue;

基本操作的函数原型说明

//----基本操作的函数原型说明----
Status InitQueue(SqQueue& Q);
//构造一个空队列Q
bool QueueEmpty(SqQueue Q);
//判断队列是否为空
int QueueLength(SqQueue Q);
//返回Q的元素个数,即队列的长度
Status EnQueue(SqQueue& Q, QElemType e);
//插入元素e为Q的新的队尾元素
Status DeQueue(SqQueue& Q, QElemType& e);
//若队列不空,则删除Q的队头元素,用e返回其值,并返回OK;否则返回ERROR
void PrintQueue(SqQueue Q);
//打印队列

基本操作的函数原型说明

//----基本操作的函数原型说明----
Status InitQueue(SqQueue& Q);
//构造一个空队列Q
int QueueLength(SqQueue Q);
//返回Q的元素个数,即队列的长度
Status EnQueue(SqQueue& Q, QElemType e);
//插入元素e为Q的新的队尾元素
Status DeQueue(SqQueue& Q, QElemType& e);
//若队列不空,则删除Q的队头元素,用e返回其值,并返回OK;否则返回ERROR
void PrintQueue(SqQueue Q);
//打印队列

//----基本操作的算法描述----
//----基本操作的算法描述----
Status InitQueue(SqQueue& Q)
{
	Q.base = (QElemType*)malloc(MAXQSIZE * sizeof(QElemType));
	if (!Q.base)
		exit(OVERFLOW);//存储分配失败
	Q.front = Q.rear = 0;
	return OK;
}
bool QueueEmpty(SqQueue Q)
{
	if (Q.front == Q.rear)
		return TRUE;
	else
		return FALSE;
}
int QueueLength(SqQueue Q)
{
	return (Q.rear - Q.front);
}
Status EnQueue(SqQueue& Q, QElemType e)
{
	if (Q.rear == MAXQSIZE )
		return ERROR;
	Q.base[Q.rear] = e;
	Q.rear = Q.rear + 1;
	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;
	return OK;
}
void PrintQueue(SqQueue Q)
{
	printf("队列为:");
	int n = Q.front;
	for (int i = 0; i < QueueLength(Q); i++)//循环次数
	{
		printf("%d ", Q.base[n++]);
	}
	printf("\n");
}

完整代码

#include<stdio.h>
#include<stdlib.h>
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
#define MAXQSIZE 10
//Status是函数的类型,其值是函数结果状态代码
typedef int Status;
typedef int QElemType;
//----循环队列----
typedef struct {
	QElemType* base;//初始化的动态分配存储空间
	int front;//头指针,若队列不空,指向队列头元素
	int rear;//尾指针,若队列不空,指向队列尾元素的下一个位置
}SqQueue;
//----基本操作的函数原型说明----
Status InitQueue(SqQueue& Q);
//构造一个空队列Q
bool QueueEmpty(SqQueue Q);
//判断队列是否为空
int QueueLength(SqQueue Q);
//返回Q的元素个数,即队列的长度
Status EnQueue(SqQueue& Q, QElemType e);
//插入元素e为Q的新的队尾元素
Status DeQueue(SqQueue& Q, QElemType& e);
//若队列不空,则删除Q的队头元素,用e返回其值,并返回OK;否则返回ERROR
void PrintQueue(SqQueue Q);
//打印队列

//----基本操作的算法描述----
Status InitQueue(SqQueue& Q)
{
	Q.base = (QElemType*)malloc(MAXQSIZE * sizeof(QElemType));
	if (!Q.base)
		exit(OVERFLOW);//存储分配失败
	Q.front = Q.rear = 0;
	return OK;
}
bool QueueEmpty(SqQueue Q)
{
	if (Q.front == Q.rear)
		return TRUE;
	else
		return FALSE;
}
int QueueLength(SqQueue Q)
{
	return (Q.rear - Q.front);
}
Status EnQueue(SqQueue& Q, QElemType e)
{
	if (Q.rear == MAXQSIZE )
		return ERROR;
	Q.base[Q.rear] = e;
	Q.rear = Q.rear + 1;
	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;
	return OK;
}
void PrintQueue(SqQueue Q)
{
	printf("队列为:");
	int n = Q.front;
	for (int i = 0; i < QueueLength(Q); i++)//循环次数
	{
		printf("%d ", Q.base[n++]);
	}
	printf("\n");
}
int main()
{
	SqQueue Q;
	InitQueue(Q);
	EnQueue(Q, 1);
	EnQueue(Q, 2);
	EnQueue(Q, 3);
	int n;
	DeQueue(Q, n);
	printf("Q.front=%d Q.rear=%d\n", Q.front, Q.rear);
	PrintQueue(Q);
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_44864262/article/details/107196768