数据结构-顺序队列实现

头文件SeqQueue.h

#include<stdio.h>
#include<malloc.h>
#include<assert.h>
#define MAXSIZE 10
typedef int ElemType;
typedef struct Queue {
	ElemType *base;
	int front;
	int rear;
}Queue;
void InitQueue(Queue*Q);
void EnQueue(Queue *Q, ElemType x);
void Show(Queue*Q);
void DeQueue(Queue *Q);
ElemType Length(Queue *Q);
void GetHead(Queue *Q, ElemType *x);
void Clear(Queue *Q);
void Destroy(Queue *Q);

函数实现SeqQueue.c

#include"SeqQueue.h"
void InitQueue(Queue*Q) {
	Q->base = (ElemType *)malloc(sizeof(ElemType)* MAXSIZE);
	assert(Q->base != NULL);
	Q->front = Q->rear = 0;
}

void EnQueue(Queue *Q, ElemType x) {
	if (Q->rear >= MAXSIZE)//队列空间已经满
		return;
	else
		Q->base[Q->rear++] = x;
}

void Show(Queue*Q) {
	for (int i = Q->front; i < Q->rear; i++)
		printf("%d ", Q->base[i]);
	printf("\n");
}

void DeQueue(Queue *Q) {
	if (Q->front == Q->rear)//队列已空
		return;
	Q->front++;
}

void GetHead(Queue *Q, ElemType *x) {
	if (Q->front == Q->rear)//队列已空
		return;
	*x = Q->base[Q->front];
}

ElemType Length(Queue *Q) {
	return (Q->rear - Q->front);
}

void Clear(Queue *Q) {
	Q->front = Q->rear = 0;
}

void Destroy(Queue *Q) {
	free(Q->base);
	Q->base = NULL;
}

测试函数Main.c

#include"SeqQueue.h"
int main() {
	Queue Q;
	InitQueue(&Q);
	for (int i = 1; i <= 5; i++) {
		EnQueue(&Q, i);
	}
	Show(&Q);
	DeQueue(&Q);
	Show(&Q);
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值