I come, I see, I conquer

                    —Gaius Julius Caesar

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

 

 

/*******************************************************************************
/* <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);
}

 

posted on 2011-04-05 15:07  jcsu  阅读(717)  评论(1编辑  收藏  举报