统一C语言风格定义各种数据结构及基本操作——顺序队列基本操作

/*sqqueue.h*/
#pragma once
#define _CRT_SECURE_NO_WARNINGS 
#include<stdio.h>
#include<stdlib.h>

#define QMAXSIZE 10
typedef int ElemType;
typedef struct SqQueue {
	ElemType* base;//动态存储区
	int front;//头指针
	int rear;//尾指针
}SqQueue,*pSqQueue;
//初始化构造一个空队列
int initQueue(pSqQueue psq)
{
	psq->base = (ElemType*)malloc(sizeof(ElemType)*QMAXSIZE);
	if (!psq)return 0;//存储分配失败
	psq->front = psq->rear = 0;
	return 1;
}
//销毁队列
void destroyQueue(pSqQueue psq)
{
	if (!psq->base)free(psq->base);//队列psq存在
	psq->base = NULL;
	psq->front = psq->rear = 0;
}
//清空队列
void clearQueue(pSqQueue psq)
{
	psq->front = psq->rear = 0;
}
//判断队列是否为空
int isEmptyQueue(pSqQueue psq)
{
	if (psq->front == psq->rear)return 1;//空
	return 0;
}
//返回队列头元素(用e接收)
int getHeadQueue(pSqQueue psq,ElemType& e)
{
	if (psq->front == psq->rear)return 0;
	e = psq->base[psq->front];
	return 1;
}
//返回队列元素个数
int lengthQueue(pSqQueue psq)
{
	return (psq->rear - psq->front);
}
//向队列队尾压入元素
int pushQueue(pSqQueue psq, ElemType e)
{
	if (psq->rear >= QMAXSIZE)return 0;//队已满
	psq->base[psq->rear] = e;//向队尾压入元素
	psq->rear += 1;//队尾指针永远指向下一个即将压入元素的位置
	return 1;
}
//删除队头元素(并用元素e接收队头元素)
int popQueue(pSqQueue psq,ElemType& e)
{
	if (psq->front == psq->rear)return 0;
	e = psq->base[psq->front];
	psq->front += 1;
	return 1;
}
//遍历队列(用回调函数接收队列中的元素)
void traverseQueue(pSqQueue psq, void(*callback)(ElemType))
{
	int head = psq->front;
	while (head != psq->rear) 
		(*callback)(psq->base[head++]);
	printf("\n");
}
//输出元素
void qprint(ElemType e)
{
	printf("%d ", e);
}
/*main.cpp*/
#include"sqqueue.h"

int main()
{
	SqQueue psq;
	int i=0, state;
	ElemType e;

	state = initQueue(&psq);
	if (state)
		printf("队列初始化成功!\n");
	else
		printf("队列初始化失败!\n");
	printf("队列长度:%d\n", lengthQueue(&psq));
	printf("队列头指针:%d\n", psq.front);
	printf("队列尾指针:%d\n\n", psq.rear);

	printf("向队列中尾插元素!\n");
	while (1)
	{
		printf("尾插第%d个元素:", ++i);
		scanf("%d", &e);
		if (e == -999)break;
		state=pushQueue(&psq, e);
		if (!state) {
			printf("队列已满!\n");
			break;
		}
	}
	printf("遍历队列中元素为:");
	traverseQueue(&psq, qprint);
	printf("队列长度:%d\n", lengthQueue(&psq));
	printf("队列头指针:%d\n", psq.front);
	printf("队列尾指针:%d\n\n", psq.rear);

	getHeadQueue(&psq, e);
	printf("队列头元素:%d\n\n", e);

	printf("头删队列中一个元素!\n");
	state = popQueue(&psq, e);
	printf("删除成功!删除的元素为%d\n", e);
	printf("遍历队列中元素为:");
	traverseQueue(&psq, qprint);
	printf("队列长度:%d\n", lengthQueue(&psq));
	printf("队列头指针:%d\n", psq.front);
	printf("队列尾指针:%d\n\n", psq.rear);

	getHeadQueue(&psq, e);
	printf("队列头元素:%d\n\n", e);

	printf("清空队列!\n");
	clearQueue(&psq);
	printf("队列长度:%d\n", lengthQueue(&psq));
	printf("队列头指针:%d\n", psq.front);

	printf("队列尾指针:%d\n\n", psq.rear);
	printf("队列是否为空[1-空,0-不空]:%d\n\n", isEmptyQueue(&psq));

	printf("销毁队列!\n");
	destroyQueue(&psq);
	printf("队列长度:%d\n", lengthQueue(&psq));
	printf("队列头指针:%d\n", psq.front);
	printf("队列尾指针:%d\n", psq.rear);

	printf("队列是否为空[1-空,0-不空]:%d\n\n", isEmptyQueue(&psq));

	return 1;
}

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值