您的位置:首页 > 编程语言 > C语言/C++

队列的链式结构C语言实现

2015-05-17 23:03 459 查看
队列的链式结构C语言实现
/***************************************************
*队列的链式结构C语言实现
****************************************************/
#include <stdio.h>
#include<stdlib.h>
#include<malloc.h>
#include<assert.h>
//#include<bool.h>
//#include<queue>
//链式队列的数据结构定义(结点的定义及队列的定义)
typedef struct QNode
{
int data;
struct QNode *next;
}QNode,*QueuePtr;
typedef struct Queue
{
QueuePtr front;
QueuePtr rear;
}linkQueue;
/*------------------------------------------------------
初始化队列,空队列
操作结果:构造一个空的队列
函数参数:linkQueue *Q 待初始化的队列
返回值:队列指针
--------------------------------------------------------*/
linkQueue *initQueue(linkQueue *Q)
{
;
Q->front=Q->rear=(QueuePtr)malloc(sizeof(QNode));
if(Q->front==NULL) exit(1);
Q->front->next=NULL;//此句很容易忽略
return Q;
}
/*------------------------------------------------------
操作结果:销毁队列,将队列所占空间全部释放
函数参数:linkQueue *Q 待销毁的队列
返回值:无
--------------------------------------------------------*/
//销毁队列,将队列所占空间全部释放
void DestroyQueue(linkQueue *Q)
{
while(Q->front)
{
Q->rear=Q->front->next;
free(Q->front);
Q->front=Q->rear;
}
}
/*------------------------------------------------------
操作结果:清空队列,将队列清空为空队列
函数参数:linkQueue *Q 待清空的队列
返回值:队列指针
--------------------------------------------------------*/
linkQueue *ClearQueue(linkQueue *Q)
{
QueuePtr p=Q->front->next;
while(p)
{
Q->front->next=p->next;
free(p);
p=Q->front->next;
}
Q->front=Q->rear;
}
/*------------------------------------------------------
操作结果:判断是否为空队列,返回1表示是空队列,0非空
--------------------------------------------------------*/
//判断是否为空队列
int IsEmpty(linkQueue *Q)
{
assert(Q->front!=NULL && Q->rear!=NULL);
if(Q->front==Q->rear)
return 1;
else
return 0;
}
/*------------------------------------------------------
操作结果:求队列的长度
--------------------------------------------------------*/
int LengthQueue(linkQueue *Q)
{
QueuePtr p;
int length=0;
assert(Q->front!=NULL);
p=Q->front/*->next*/;
while(p!=Q->rear)
{
length++;
p=p->next;
}
return length;
}
/*------------------------------------------------------
操作结果:将元素e插入队尾
--------------------------------------------------------*/
linkQueue *EnQueue(linkQueue *Q,int e)
{
QueuePtr p;
p=(QueuePtr)malloc(sizeof(QNode));
if(!p) exit(1);
p->data=e;
p->next=NULL;
Q->rear->next=p;
Q->rear=p;
return Q;
}
/*------------------------------------------------------
操作结果:若队列不空,则删除Q的队头元素,并用e返回其值
--------------------------------------------------------*/
int DeQueue(linkQueue *Q,int e)
{
QueuePtr p;
if(Q->front==Q->rear) return;
p=Q->front->next;
e=p->data;
Q->front->next=p->next;
if(Q->rear==p) Q->rear=Q->front;
free(p);
return e;
}
/*------------------------------------------------------
操作结果:队列的遍历与元素输出,用到了函数指针,传指针速度快
--------------------------------------------------------*/
void QueueTraverse(linkQueue *Q,void(*visit)(int ))
{
QueuePtr p;
assert(Q->front!=NULL);
if(Q->front==Q->rear) return;
p=Q->front;
while(p!=Q->rear)
{
p=p->next;
(*visit)(p->data);//
}
}
void visit(int e)
{
printf("%d\n",e);
}
//主函数**********************************************
int main()
{
linkQueue Q;
int length_m;
initQueue(&Q);
length_m=LengthQueue(&Q);
printf("队列长度%d\n",length_m);
printf("将元素1压入空队列队尾\n");
EnQueue(&Q,1);
length_m=LengthQueue(&Q);
printf("队列长度%d\n",length_m);
printf("将元素2压入已有队列队尾\n");
EnQueue(&Q,2);
length_m=LengthQueue(&Q);
printf("队列长度%d\n",length_m);
printf("遍历输出队列元素\n");
QueueTraverse(&Q,(*visit));
ClearQueue(&Q);
printf("遍历输出队列元素\n");
QueueTraverse(&Q,(*visit));
}

程序结果
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: