#include "queue_array.h"
#include "fatal.h"
/* Place in implementation file
* Queue implementation is a dynamically allocated array*/
#define MinQueueSize ( 5 )
struct QueueRecord
{
int Capacity;
int Front;
int Rear;
int Size;
ElementType *Array;
};
int
IsEmpty( Queue Q )
{
return Q->Size == 0;
}
void
MakeEmpty( Queue Q )
{
Q->Size = 0;
Q->Front = 1;
Q->Rear = 0;
}
static int
Succ( int value, Queue Q )
{
if ( ++value == Q->Capacity )
value = 0;
return value;
}
void
Enqueue( ElementType X, Queue Q )
{
if( IsFull( Q ) )
Error( "Full queue" );
else
{
Q->Size++;
Q->Rear = Succ( Q->Rear, Q );
Q->Array[ Q-> Rear ] = X;
}
}
void
Dequeue( Queue Q )
{
if ( IsEmpty( Q ) )
Error( "Empty queue" );
else
{
Q->Size--;
Q->Front++;
}
}
ElementType
FrontAndDequeue( Queue Q )
{
ElementType temp = 0;
if ( IsEmpty( Q ) )
Error( "Empty queue" );
else
{
Q->Size--;
temp= Q->Array[ Q->Front ];
Q->Front = Succ( Q->Front, Q );
}
return temp;
}
ElementType
Front( Queue Q )
{
if ( !IsEmpty( Q ) )
return Q->Array[ Q->Front ];
Error( "Empty queue" );
return 0; /* Return value used to avoid warning */
}
int
IsFull( Queue Q )
{
return Q->Size == Q->Capacity;
}
Queue
CreateQueue(int MaxElements )
{
Queue Q;
if ( MaxElements < MinQueueSize )
Error( "Queue size is too small!!!" );
Q = malloc( sizeof( struct QueueRecord ) );
if ( Q == NULL )
FatalError( "Out of space!!!" );
Q->Array = malloc( sizeof( ElementType ) * MaxElements );
if ( Q->Array == NULL )
FatalError( "Out of Space!!!" );
Q->Capacity = MaxElements;
MakeEmpty( Q );
return Q;
}
void
DisposeQueue( Queue Q )
{
if ( Q != NULL )
{
free( Q->Array );
free( Q );
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
共5个文件
c:2个
h:2个
queue_main:1个
像栈一样,队列(queue)也是表。然而,使用队列时插入在一端进行而删除在另一端进行。 队列的基本操作是Enqueue(入队),它是在表的末端(叫做队尾(rear)插入一个元素,还有Dequeue(出队),它是删除(或返回)在表的开头(叫做队头(front)的元素。如图1所示显示一个队列的抽象模型。 原文:http://blog.csdn.net/shuxiao9058/article/details/7173262
资源推荐
资源详情
资源评论
收起资源包目录
queue_array.tar.gz (5个子文件)
queue_array
queue_main.c 552B
fatal.h 156B
queue_array.h 448B
queue_main 7KB
queue_array.c 2KB
共 5 条
- 1
资源评论
- heisitech2013-11-20对,就是要找的资源。参考练习一下!
- fjljfat12016-04-09东西不错,学习的好材料
啸傲居士
- 粉丝: 592
- 资源: 56
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功