《数据结构》链队列

 

 

/* ******************************************************************************
/* <PRE>
/* 版权所有    : -
/* 模块名      : 栈和队列
/* 文件名      : lqueue.cpp
/* 功能描述    : 链队列的表示与实现 
/* 作者        : <xxx>
/* 版本        : 1.0
/* -----------------------------------------------------------------------------
/* 备注        : -
/* -----------------------------------------------------------------------------
/* 修改记录    :
/* 日 期        版本     修改人        修改内容
/* 2011/01/01   1.0      <xxx>         创建
/* </PRE>
******************************************************************************
*/
#include 
< stdio.h >
#include 
< stdlib.h >

/* *****************************************************************************
/* 数据类型和常量定义
/*****************************************************************************
*/
#define  OK           1
#define  ERROR        0
#define  OVERFLOW    -2

typedef 
int  Status;
typedef 
int  QElemType;


/* *****************************************************************************
/* 数据结构声明
/*****************************************************************************
*/
/*  单链队列 - 队列的链式存储结构  */
typedef 
struct  QNode{
    QElemType data;
    
struct  QNode  * next;
} QNode, 
* QueuePtr;

typedef 
struct  {
    QueuePtr front; 
/*  队头指针  */
    QueuePtr rear;  
/*  队尾指针  */
} LinkQueue;


/* *****************************************************************************
/* 函数原型声明
/*****************************************************************************
*/
/* 队列操作函数 */
Status InitQueue(LinkQueue 
& Q);
Status DestroyQueue(LinkQueue 
& Q);
Status ClearQueue(LinkQueue 
& Q);
Status QueueEmpty(LinkQueue Q);
int  QueueLength(LinkQueue Q);
Status GetHead(LinkQueue Q, QElemType 
& e);
Status EnQueue(LinkQueue 
& Q, QElemType e);
Status DeQueue(LinkQueue 
& Q, QElemType  & e);


/* ******************************************************************************
/* <FUNC>
/* 函数名   : InitQueue
/* 功能     : 构造空队列
/* 参数     : -
/* 返回值   : -
/* 备注     : 构造一个空队列Q
/* 作者     : <xxx>
/* </FUNC>
******************************************************************************
*/
Status InitQueue(LinkQueue 
& Q) {
    Q.front 
=  Q.rear  =  (QueuePtr)malloc( sizeof (QNode));
    
if  ( ! Q.front) exit(OVERFLOW);
    Q.front
-> next  =  NULL;
    
return  OK;
}

/* ******************************************************************************
/* <FUNC>
/* 函数名   : DestroyQueue
/* 功能     : 销毁队列
/* 参数     : -
/* 返回值   : -
/* 备注     : 销毁队列Q
/* 作者     : <xxx>
/* </FUNC>
******************************************************************************
*/
Status DestroyQueue(LinkQueue 
& Q) {
    
while  (Q.front) {
        Q.rear 
=  Q.front -> next;
        free(Q.front);
        Q.front 
=  Q.rear;
    }
    
return  OK;
}

/* ******************************************************************************
/* <FUNC>
/* 函数名   : EnQueue
/* 功能     : 入队列
/* 参数     : -
/* 返回值   : -
/* 备注     : 插入元素e为Q的新的队尾元素
/* 作者     : <xxx>
/* </FUNC>
******************************************************************************
*/
Status EnQueue(LinkQueue 
& Q, QElemType e) {
    
struct  QNode  * =  (QueuePtr)malloc( sizeof (QNode));
    
if  ( ! p) exit(OVERFLOW);
    p
-> data  =  e; p -> next  =  NULL;
    Q.rear
-> next  =  p;
    Q.rear 
=  p;
    
return  OK;
}

/* ******************************************************************************
/* <FUNC>
/* 函数名   : DeQueue
/* 功能     : 出队列
/* 参数     : -
/* 返回值   : -
/* 备注     : 若队列不空, 则删除Q的队头元素, 用e返回其值, 并返回OK; 否则返回ERROR
/* 作者     : <xxx>
/* </FUNC>
******************************************************************************
*/
Status DeQueue(LinkQueue 
& Q, QElemType  & e) {
    
struct  QNode  * =  NULL;
    
if  (Q.front  ==  Q.rear)  return  ERROR;
    p 
=  Q.front -> next;
    e 
=  p -> data;
    Q.front
-> next  =  p -> next;
    
if  (Q.rear  ==  p) Q.rear  =  Q.front;
    free(p);
    
return  OK;
}


/* ******************************************************************************
/* <FUNC>
/* 函数名   : main
/* 功能     : 测试函数
/* 参数     : -
/* 返回值   : -
/* 备注     : -
/* 作者     : <xxx>
/* </FUNC>
******************************************************************************
*/
void  main()
{
    LinkQueue Q;  QElemType e;
    InitQueue(Q);
    EnQueue(Q, 
10 );
    EnQueue(Q, 
20 );
    EnQueue(Q, 
30 );
    
if (OK  ==  DeQueue(Q, e)) printf( " %d\n " , e);
    
if (OK  ==  DeQueue(Q, e)) printf( " %d\n " , e);
    
if (OK  ==  DeQueue(Q, e)) printf( " %d\n " , e);
    
if (OK  ==  DeQueue(Q, e)) printf( " %d\n " , e);
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值