star

搜索

RSS

RSS Link

计数器

139726

队列的头文件

2010年10月05日 04:02 | Comments(1) | Category:C语言 | Tags:

/* 数据结构 队列的实现 Queue.h
 * author : star
 */
 
//防止重复定义
#ifndef QUEUE_H
#define QUEUE_H

#include "defs.h"

//初始化大小为n的队列
int InitSqqueue(SQQUEUE *Q, int n)
{
	Q->data = (ElemType *)malloc((n+1) * sizeof(ElemType));
	if(Q->data == NULL)
		return 0;
	Q->front = Q->rear = 0;
	Q->size  = n + 1;
	return 1;
}

//销毁队列
void DestroySqqueue(SQQUEUE *Q)
{
	free(Q->data);
	Q->data = NULL;
	Q->front = Q->rear = Q->size = 0;
}

//判断队空还是满
int IsSqqueueEmpty(SQQUEUE Q)
{
	return Q.front == Q.rear;
}

int IsSqqueueFull(SQQUEUE Q)
{
	return Q.front == (Q.rear+1) % Q.size;
}

//入队
int EnSqqueue(SQQUEUE *Q, ElemType e)
{
	if(IsSqqueueFull(*Q))
		return 0;
	Q->data[Q->rear] = e;
	Q->rear = (Q->rear+1) % Q->size;\
	return 1;
}

//出队
int DeSqqueue(SQQUEUE *Q, ElemType *e)
{
	if(IsSqqueueFull(*Q))
		return 0;
	*e = Q->data[Q->front];
	Q->front = (Q->front+1) % Q->size;
	return 1;
}

//队列长度
int SqqueueLen(SQQUEUE Q)
{
	return (Q.size + Q.rear + Q.front) % Q.size;
}

//初始化空链式队
void InitLkqueue(LKQUEUE *Q)
{
	Q->front = Q->rear = NULL;
}

//销毁链式队列
void DestroyLkqueue(LKQUEUE *Q)
{
	LQNODEPTR p;
	while(Q->front != NULL)
	{
		p = Q->front;
		Q->front = p->next;
		free(p);
	}
	Q->rear = NULL;
}

//入链队
int EnLkqueue(LKQUEUE *Q, ElemType e)
{
	LQNODEPTR p;
	p = (LQNODEPTR)malloc(sizeof(LQNODE));
	if(p == NULL)
		return 0;
	p->data = e;
	p->next = NULL;
	if(Q->front == NULL)
		Q->front      = Q->rear = p;
	else
		Q->rear->next = Q->rear = p;
	return 1;
}

//出链队
int DeLkqueue(LKQUEUE *Q, ElemType *e)
{
	LQNODEPTR p;
	if(Q->front == NULL)
		return 0;
	p = Q->front;
	Q->front = p->next;
	if(Q->front == NULL)
		Q->rear = NULL;
	*e = p->data;
	free(p);
	return 1;
}

#endif