#include "queue_list.h"
#include "fatal.h"
typedef struct QNode
{
ElementType Element;
struct QNode *Next;
}QNode, *QNodePtr;
struct Node {
QNodePtr Front;
QNodePtr Rear;
};
int
IsEmpty( Queue Q )
{
return Q->Front == Q->Rear;
}
Queue
CreateQueue( void )
{
Queue Q;
Q = malloc( sizeof( struct Node ) );
Q->Front = Q->Rear = malloc( sizeof( struct QNode ) );
if (!Q->Front)
FatalError( "Out of space!!!");
Q->Front->Next = NULL;
return Q;
}
void
MakeEmpty( Queue Q )
{
if( Q == NULL )
Error( "Must use CreateQueue first" );
else
while( !IsEmpty( Q ) )
Dequeue( Q );
}
void
DisposeQueue( Queue Q )
{
while( Q->Front ) {
Q->Rear = Q->Front->Next;
free( Q->Front );
Q->Front = Q->Rear;
}
printf( "\nDispose queue completed!!!" );
}
void
Enqueue( ElementType X, Queue Q )
{
QNodePtr p;
p = malloc( sizeof( QNode ) );
if (!p)
FatalError( "Out of space!!!" );
p->Element = X;
p->Next = NULL;
Q->Rear->Next = p;
Q->Rear = p;
}
ElementType
Front( Queue Q )
{
if ( !IsEmpty( Q ) )
return Q->Front->Next->Element;
return 0; /* Return value used to avoid warning */
}
void
Dequeue( Queue Q )
{
if ( !IsEmpty( Q ) )
{
QNodePtr p;
p = malloc( sizeof( QNode ) );
if (!p)
FatalError( "Out of space!!!" );
p = Q->Front->Next;
Q->Front->Next = p->Next;
if ( Q->Rear == p )
Q->Rear = Q->Front;
free( p );
}
}
ElementType
FrontAndDequeue( Queue Q )
{
if ( !IsEmpty( Q ) )
{
QNodePtr p;
p = malloc( sizeof( QNode ) );
if (!p)
FatalError( "Out of space!!!" );
p = Q->Front->Next;
ElementType temp = 0;
temp = p->Element;
Q->Front->Next = p->Next;
if ( Q->Rear == p )
Q->Rear = Q->Front;
free( p );
return temp;
}
Error( "Empty queue!!!" );
return 0; /* Return value used to avoid warning */
}
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
共5个文件
c:2个
h:2个
queue_list_main:1个
像栈一样,队列(queue)也是表。然而,使用队列时插入在一端进行而删除在另一端进行。 队列的基本操作是Enqueue(入队),它是在表的末端(叫做队尾(rear)插入一个元素,还有Dequeue(出队),它是删除(或返回)在表的开头(叫做队头(front)的元素。如图1所示显示一个队列的抽象模型。 原文:http://blog.csdn.net/shuxiao9058/article/details/7173262
资源推荐
资源详情
资源评论
收起资源包目录
queue_list.tar.gz (5个子文件)
queue_list
queue_list_main.c 671B
queue_list.h 446B
fatal.h 156B
queue_list_main 7KB
queue_list.c 2KB
共 5 条
- 1
资源评论
- zy8122482582016-09-21通常我都会选择使用模板,但是有时候也需要手动实现,楼主这个不错,谢谢了
- songyuejie19002014-06-19重要数据结构的实现,值得初学者学习。
- heisitech2013-11-14程序没有问题,可以参考。谢谢
- fjljfat12014-07-09字符串类型有问题,不能用
- czsandyl2015-12-25可以执行,清晰明了,不错,值得推荐
啸傲居士
- 粉丝: 592
- 资源: 56
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功