数据结构——顺序队列的基本操作(C语言实现)

本文详细介绍了如何使用C语言实现顺序队列的基本操作,包括定义、初始化、判空、入队、出队、打印头元素、获取队列元素数量以及摧毁队列等关键步骤。
摘要由CSDN通过智能技术生成

1、定义

#define QUEUE_DEFAULT_SIZE 8
typedef struct SeqQueue
{
	ElemType *base;
	int capacity;
	int front;
	int rear;
}SeqQueue;

2、初始化

void SeqQueueInit(SeqQueue* psq)
{
	assert(psq != NULL);
	psq->capacity = QUEUE_DEFAULT_SIZE;
	psq->base = (ElemType*)malloc(sizeof(ElemType)*psq->capacity);
	psq->front = psq->rear = 0;
}

3、判空

bool SeqQueueEmpty(SeqQueue *psq)
{
	assert(psq != NULL);
	return psq->front == psq->rear;
}

4、入队、出队

void SeqQueueEn(SeqQueue* psq, ElemType x)
{
	assert(psq != NULL);
	if (psq->rear >= psq->capacity)
	{
		printf("队列已满,%d不能入队!\n");
		return;
	}
	psq->base[psq->rear++] = x;
}
void SeqQueueDe(SeqQueue* psq)
{
	assert(psq != NULL);
	if (SeqQueueEmpty(psq))
	{
		printf("队列已空,不能出队!\n");
		return;
	}
	psq->front++;
}</
  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值