数据结构顺序队列

//SeqQueue.h
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
typedef int Datatype;
typedef struct SeqQueue
{
	Datatype* data;
	int front;
	int rear;
	int capacity;
}SQ;

void SeqqueueInit(SQ* ps);
void SeqqueueDestory(SQ* ps);
int SeqqueueLen(SQ* ps);
void SeqqueueCheck(SQ* ps);
void EnSeqqueue(SQ* ps,Datatype x);
void DeSeqqueue(SQ* ps);
int SeqqueueEmpty(SQ* ps);



//SeqQueue.c
#include "SeqQueue.h"

void SeqqueueInit(SQ* ps)
{
	assert(ps);
	ps->data = (Datatype*)malloc(sizeof(Datatype) * 4);
	if (ps->data == NULL)
	{
		printf("失败");
		exit(-1);
	}	
	ps->front = 0;
	ps->rear = 0;
	ps->capacity = 4;
}

void SeqqueueDestory(SQ* ps)
{
	assert(ps);
	free(ps->data);
	ps->data = NULL;
	ps->front = ps->rear =ps->capacity= 0;
}


int SeqqueueLen(SQ* ps)
{
	assert(ps);
	return ps->rear - ps->front;
}

void SeqqueueCheck(SQ* ps)
{
	if (ps->front == (ps->rear+1)%(ps->capacity))
	{
		ps->capacity = ps->capacity * 2;
		ps->data = (Datatype*)realloc(ps->data, sizeof(Datatype) * ps->capacity);
		if (ps->data == NULL)
		{
			printf("失败");
			exit(-1);
		}
	}
}

void EnSeqqueue(SQ* ps, Datatype x)
{
	assert(ps);
	SeqqueueCheck(ps);

	ps->data[ps->rear] = x;
	ps->rear = (ps->rear + 1) % (ps->capacity);
}

void DeSeqqueue(SQ* ps)
{
	assert(ps);
	if (ps->front == ps->rear)
	{
		printf("空队列");
		exit(-1);
	}
	ps->front = (ps->front + 1) % (ps->capacity);
}

int SeqqueueEmpty(SQ* ps)
{
	assert(ps);
	if (ps->front == ps->rear)
		return 1;
	else
		return 0;	
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值