数据结构:数组队列

头文件

#ifndef __SQQUENUE_H__
#define __SQQUENUE_H__

#define FALSE 0
#define TRUE 1

#define SIZE 10

typedef int Queue_data ;

typedef struct _queue
{
	Queue_data data[SIZE];
	int front;     //定义头
	int rear;      //定义尾
}QUEUE;

//清空队列(初始化)
int Initqueue(QUEUE * q);

//判断队列空否
int QueueEmpty(QUEUE * q);

//进队
int PushQueue(QUEUE *q,Queue_data x);

//出队
int PopQueue(QUEUE *q,Queue_data *x);

//取队头
int GetQueue(QUEUE *q,Queue_data *x);

#endif


实现函数
#include<stdio.h>
#include<stdlib.h>
#include"SqQuenue.h"
#include"error.h"

//清空队列
int Initqueue(QUEUE* q)
{
	if (q == NULL)
	{	
		errno = ERROR;
		return FALSE;
	}
	
	q->front = 0;
	q->rear = 0;
}

//判断队列空否
int QueueEmpty(QUEUE * q)
{
	if (q == NULL)
	{	
		errno = ERROR;
		return FALSE;
	}
	
	return q->front == q->rear ;
}
	
//判断队列满否
int QueueFull(QUEUE *q)
{
	if (q == NULL)
	{	
		errno = ERROR;
		return FALSE;
	}
	
	return q->front == (q->rear+1)%SIZE;
}

//进队(从rear进队列)
int PushQueue(QUEUE *q,Queue_data x)
{
	if (q == NULL)
	{	
		errno = ERROR;
		return FALSE;
	}
	
	if(QueueFull(q))
	{
		errno = QUEUE_FULL;
		return FALSE;
	}
	
	q->rear = (q->rear+1) % SIZE;
	q->data[q->rear] = x;
	
	return TRUE;
}

//出队(从front出队列)
int PopQueue(QUEUE *q,Queue_data *x)
{
	if (q == NULL)
	{	
		errno = ERROR;
		return FALSE;
	}
	
	if(QueueEmpty(q))
	{
		errno = QUEUE_EMPTY;
		return FALSE;
	}
	
	q->front = (q->front +1) %SIZE;
	*x = q->data[q->front];

	return TRUE;
}

//取队头
int GetQueue(QUEUE *q,Queue_data *x)
{
	if (q == NULL)
	{	
		errno = ERROR;
		return FALSE;
	}
	
	if(QueueEmpty(q) != TRUE)
	{
		errno = QUEUE_EMPTY;
		return FALSE;
	}
	
	int index = (q->front + 1) % SIZE;
	*x = q->data[index];
	
	return TRUE;
}









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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值