c语言实现队列基本算法,【算法】队列的C语言实现

/***

*ElemType.h - ElemType的定义

*

****/

#ifndef ELEMTYPE_H

#define ELEMTYPE_H

typedef int ElemType;

int compare(ElemType x, ElemType y);

void visit(ElemType e);

#endif /* ELEMTYPE_H */

/***

*ElemType.cpp - ElemType的实现

*

****/

#include

#include "ElemType.h"

int compare(ElemType x, ElemType y)

{

return(x-y);

}

void visit(ElemType e)

{

printf("%d\n", e);

}

/***

*DynaLnkQueue.cpp - 动态链式队列,即队列的动态链式存储实现

*

****/

#include

#include

#include

#include

#include "DynaLnkQueue.h"

#define NULL 0

/*------------------------------------------------------------

操作目的: 初始化队列

初始条件: 无

操作结果: 构造一个空的队列

函数参数:

LinkQueue *Q 待初始化的队列

返回值:

bool 操作是否成功

------------------------------------------------------------*/

bool InitQueue(LinkQueue *Q)

{

Q ->front = Q ->rear = (QueuePtr)malloc(sizeof(QNode));

if(Q ->front == NULL)

return false;

Q ->front ->next = NULL;

return true;

}

/*------------------------------------------------------------

操作目的: 销毁队列

初始条件: 队列Q已存在

操作结果: 销毁队列Q

函数参数:

LinkQueue *Q 待销毁的队列

返回值:

------------------------------------------------------------*/

void DestroyQueue(LinkQueue *Q)

{

assert(Q != NULL);

while(Q ->front)

{

Q ->rear = Q ->front ->next;

free(Q ->front);

Q ->front = Q ->rear;

}

}

/*------------------------------------------------------------

操作目的: 判断队列是否为空

初始条件: 队列Q已存在

操作结果: 若Q为空队列,则返回true,否则返回false

函数参数:

LinkQueue Q 待判断的队列

返回值:

bool 是否为空

------------------------------------------------------------*/

bool QueueEmpty(LinkQueue Q)

{

assert(Q.front != NULL && Q.rear != NULL);

if(Q.front == Q.rear)

return true;

else

return false;

}

/*------------------------------------------------------------

操作目的: 得到队列的长度

初始条件: 队列Q已存在

操作结果: 返回Q中数据元素的个数

函数参数:

LinkQueue Q 队列Q

返回值:

int 数据元素的个数

------------------------------------------------------------*/

int QueueLength(LinkQueue Q)

{

assert(Q.front != NULL);

QueuePtr p = Q.front;

int Length = 0;

while (p != Q.rear)

{

Length++;

p = p->next;

}

return Length;

}

/*------------------------------------------------------------

操作目的: 得到队列首元素

初始条件: 队列Q已存在

操作结果: 用e返回队列首元素

函数参数:

LinkQueue Q 队列Q

ElemType *e 队列首元素的值

返回值:

bool 操作是否成功

------------------------------------------------------------*/

bool GetHead(LinkQueue Q, ElemType *e)

{

assert(Q.front != NULL);

if(QueueEmpty(Q))

return false;

else

{

*e = Q.front ->next ->data;

return true;

}

}

/*------------------------------------------------------------

操作目的: 遍历队列

初始条件: 队列Q已存在

操作结果: 依次对Q的每个元素调用函数fp

函数参数:

LinkQueue Q 队列Q

void (*fp)() 访问每个数据元素的函数指针

返回值:

------------------------------------------------------------*/

void QueueTraverse(LinkQueue Q, void (*fp)(ElemType))

{

assert(Q.front != NULL);

QueuePtr p = Q.front ->next;

while(p)

{

(*fp)(p ->data);

p = p ->next;

}

}

/*------------------------------------------------------------

操作目的: 清空队列

初始条件: 队列Q已存在

操作结果: 将队列清空

函数参数:

LinkQueue *Q 队列Q

返回值:

------------------------------------------------------------*/

void ClearQueue(LinkQueue *Q)

{

assert(Q ->front != NULL);

QueuePtr p = Q ->front ->next;

while(p)

{

Q ->front ->next = p ->next;

free(p);

p = Q ->front ->next;

}

}

/*------------------------------------------------------------

操作目的: 在队列末尾插入元素e

初始条件: 队列Q已存在

操作结果: 插入元素e作为队列新的尾结点

函数参数:

LinkQueue *Q 队列Q

ElemType e 待插入的数据元素

返回值:

bool 操作是否成功

------------------------------------------------------------*/

bool EnQueue(LinkQueue *Q, ElemType e)

{

QueuePtr temp = (QueuePtr )malloc(sizeof(QNode));

if(!temp)

return false;

temp ->data = e;

temp ->next = NULL;

Q->rear ->next = temp;

Q ->rear = temp;

return true;

}

/*------------------------------------------------------------

操作目的: 删除链式队列的头结点

初始条件: 队列Q已存在

操作结果: 删除链式队列的头结点

函数参数:

LinkQueue *Q 队列Q

ElemType *e 待插入的数据元素

返回值:

bool 操作是否成功

------------------------------------------------------------*/

bool DeQueue(LinkQueue *Q, ElemType *e)

{

if(Q ->front == Q->rear)

return false;

QueuePtr temp = Q->front ->next;

*e = temp ->data;

Q ->front ->next= temp ->next;

if(Q ->rear == temp)

Q ->rear = Q ->front;

free(temp);

return true;

}

/***

*DynaLnkQueue.h - 动态链式队列的定义

*

****/

#if !defined(DYNALNKQUEUE_H)

#define DYNALNKQUEUE_H

#include "ElemType.h"

/*------------------------------------------------------------

// 链式队列结构的定义

------------------------------------------------------------*/

typedef struct Node

{

ElemType data; // 元素数据

struct Node *next; // 链式队列中结点元素的指针

} QNode, *QueuePtr;

typedef struct

{

QueuePtr front; // 队列头指针

QueuePtr rear; // 队列尾指针

} LinkQueue;

/*------------------------------------------------------------

// 链式队列的基本操作

------------------------------------------------------------*/

bool InitQueue(LinkQueue *Q);

void DestroyQueue(LinkQueue *Q);

bool QueueEmpty(LinkQueue Q);

int QueueLength(LinkQueue Q);

bool GetHead(LinkQueue Q, ElemType *e);

void QueueTraverse(LinkQueue Q, void (*fp)(ElemType));

void ClearQueue(LinkQueue *Q);

bool EnQueue(LinkQueue *Q, ElemType e);

bool DeQueue(LinkQueue *Q, ElemType *e);

#endif /* DYNALNKQUEUE_H */

#include

#include

#include "DynaLnkQueue.h"

int main()

{

// TODO: Place your test code here

/*LinkQueue W;

InitQueue(&W);

EnQueue(&W,1);

EnQueue(&W,2);

QueueTraverse(W,visit);*/

system("PAUSE");

return 0;

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值