数据结构:数组队列

头文件

#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
#include
#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;
}









你可能感兴趣的:(沙僧取金:第一站,Linus,c)