学习笔记------数据结构(C语言版)队列链式存储
2016-02-05 17:36
639 查看
//LinkQueue.cpp
#include"predefined.h"
#include"LinkQueue.h"
Status InitQueue(LinkQueue *Q)
//构造一个新队列Q
{
(*Q).front=(*Q).rear=(QueuePtr)malloc(sizeof(QNode));
if(!(*Q).front)
exit(OVERFLOW);
(*(*Q).front).next=NULL;
return OK;
}
Status DestroyQueue(LinkQueue *Q)
//销毁队列Q,Q不再存在
{
while((*Q).front)
{
(*Q).rear=(*(*Q).front).next;
free((*Q).front);
(*Q).front=(*Q).rear;
}
return OK;
}
Status ClearQueue(LinkQueue *Q)
//将Q清为空队列
{
QueuePtr Z;
while((*Q).front!=(*Q).rear)
{
Z=(*(*Q).front).next;
free((*Q).front);
(*Q).front=Z;
}
return OK;
}
Status QueueEmpty(LinkQueue Q)
//若队列为空队列,则返回TRUE,否则返回FALSE
{
if(Q.front==Q.rear)
return TRUE;
else
return FALSE;
}
int QueueLength(LinkQueue Q)
//返回Q的元素个数,即为队列的长度
{
QueuePtr Z=Q.front;
int count=0;
while(Z!=Q.rear)
{
Z=(*Z).next;
count++;
}
return count;
}
Status GetHead(LinkQueue Q,QElemType *e)
//若队列不空,则用e返回Q的队头元素,并返回OK;否则返回ERROR。
{
if(QueueEmpty(Q)) return ERROR;
*e=(*(*Q.front).next).data;
return OK;
}
Status EnQueue(LinkQueue *Q,QElemType e)
//插入元素e为Q的新的队尾元素
{
QueuePtr N;
N=(QueuePtr)malloc(sizeof(QNode));
if(!N)
exit(OVERFLOW);
(*N).data=e;
(*N).next=NULL;
(*Q).rear=(*(*Q).rear).next=N;
return OK;
}
Status DeQueue(LinkQueue *Q,QElemType *e)
//若队列不为空,则删除Q的队头元素,用e返回其值,并返回OK;
//否则返回ERROR
{
if(QueueEmpty(*Q)) return ERROR;
QueuePtr N;
N=(*(*Q).front).next;
*e=(*N).data;
(*(*Q).front).next=(*N).next;
if((*Q).rear==N) (*Q).rear=(*Q).front;
free(N);
return OK;
}
void QueueTraverse(LinkQueue Q,void (*visit)(QElemType e))
//从队头到队尾,依次对Q的每个数据元素调用函数visit()
{
QueuePtr Z=(*Q.front).next;
QElemType e;
while(Z)
{
e=(*Z).data;
visit(e);
Z=(*Z).next;
}
}
void PrintElem(QElemType e)
{
printf("%d ",e);
}
//main.cpp
#include"predefined.h"
#include"LinkQueue.h"
int main()
{
LinkQueue Q;
Status s;
int i;
QElemType e;
printf("Function 1\n★函数Status InitQueue(LinkQueue *Q)测试...\n");
s=InitQueue(&Q);
printf("▲初始化链队列Q: %d (0:失败 1:成功)\n\n",s);
printf("Function 2\n★函数Status QueueEmpty(LinkQueue Q)测试...\n");
QueueEmpty(Q)?printf("▲链队列Q为空!!!\n\n"):printf("▲链队列Q非空!!!\n\n");
printf("Function 3\n★函数Status EnQueue(LinkQueue *Q,QElemType e)测试...\n");
for(i=1;i<6;i++)
{
printf("▲元素\"%2d\"入队\n",2*i);
EnQueue(&Q,2*i);
printf("▲累计第%d元素\n",QueueLength(Q));
}
printf("\n");
printf("Function 4\n★函数void QueueTraverse(LinkQueue Q,void (*visit)(QElemType e))测试...\n");
printf("▲队列Q中的元素为:Q={");
QueueTraverse(Q,PrintElem);
printf("}\n\n");
printf("Function 5\n★函数Status DeQueue(LinkQueue *Q,QElemType *e)测试...\n");
DeQueue(&Q,&e);
printf("▲队列Q队头元素\"%2d\"出队\n",e);
printf("▲队列Q中的元素为:Q={");
QueueTraverse(Q,PrintElem);
printf("}\n\n");
printf("Function 6\n★函数int QueueLength(LinkQueue Q)测试...\n");
i=QueueLength(Q);
printf("▲队列Q的长度为%d\n\n",i);
printf("Function 7\n★函数Status GetHead(LinkQueue Q,QElemType *e)测试...\n");
GetHead(Q,&e);
printf("▲队列Q队头元素\"%2d\"\n\n",e);
printf("Function 8\n★函数Status ClearQueue(LinkQueue *Q)测试...\n");
printf("▲置空前:");
QueueEmpty(Q)?printf("▲链队列Q为空!!!\n"):printf("▲链队列Q非空!!!\n");
ClearQueue(&Q);
printf("▲置空后:");
QueueEmpty(Q)?printf("▲链队列Q为空!!!\n\n"):printf("▲链队列Q非空!!!\n\n");
printf("Function 9\n★函数Status DestroyQueue(LinkQueue *Q)测试...\n");
printf("▲销毁前:");
Q.front?printf("▲链队列Q存在!!!\n"):printf("▲链队列Q不存在!!!\n");
DestroyQueue(&Q);
printf("▲销毁后:");
Q.front?printf("▲链队列Q存在!!!\n\n"):printf("▲链队列Q不存在!!!\n\n");
return 0;
}
#include"predefined.h"
#include"LinkQueue.h"
Status InitQueue(LinkQueue *Q)
//构造一个新队列Q
{
(*Q).front=(*Q).rear=(QueuePtr)malloc(sizeof(QNode));
if(!(*Q).front)
exit(OVERFLOW);
(*(*Q).front).next=NULL;
return OK;
}
Status DestroyQueue(LinkQueue *Q)
//销毁队列Q,Q不再存在
{
while((*Q).front)
{
(*Q).rear=(*(*Q).front).next;
free((*Q).front);
(*Q).front=(*Q).rear;
}
return OK;
}
Status ClearQueue(LinkQueue *Q)
//将Q清为空队列
{
QueuePtr Z;
while((*Q).front!=(*Q).rear)
{
Z=(*(*Q).front).next;
free((*Q).front);
(*Q).front=Z;
}
return OK;
}
Status QueueEmpty(LinkQueue Q)
//若队列为空队列,则返回TRUE,否则返回FALSE
{
if(Q.front==Q.rear)
return TRUE;
else
return FALSE;
}
int QueueLength(LinkQueue Q)
//返回Q的元素个数,即为队列的长度
{
QueuePtr Z=Q.front;
int count=0;
while(Z!=Q.rear)
{
Z=(*Z).next;
count++;
}
return count;
}
Status GetHead(LinkQueue Q,QElemType *e)
//若队列不空,则用e返回Q的队头元素,并返回OK;否则返回ERROR。
{
if(QueueEmpty(Q)) return ERROR;
*e=(*(*Q.front).next).data;
return OK;
}
Status EnQueue(LinkQueue *Q,QElemType e)
//插入元素e为Q的新的队尾元素
{
QueuePtr N;
N=(QueuePtr)malloc(sizeof(QNode));
if(!N)
exit(OVERFLOW);
(*N).data=e;
(*N).next=NULL;
(*Q).rear=(*(*Q).rear).next=N;
return OK;
}
Status DeQueue(LinkQueue *Q,QElemType *e)
//若队列不为空,则删除Q的队头元素,用e返回其值,并返回OK;
//否则返回ERROR
{
if(QueueEmpty(*Q)) return ERROR;
QueuePtr N;
N=(*(*Q).front).next;
*e=(*N).data;
(*(*Q).front).next=(*N).next;
if((*Q).rear==N) (*Q).rear=(*Q).front;
free(N);
return OK;
}
void QueueTraverse(LinkQueue Q,void (*visit)(QElemType e))
//从队头到队尾,依次对Q的每个数据元素调用函数visit()
{
QueuePtr Z=(*Q.front).next;
QElemType e;
while(Z)
{
e=(*Z).data;
visit(e);
Z=(*Z).next;
}
}
void PrintElem(QElemType e)
{
printf("%d ",e);
}
//main.cpp
#include"predefined.h"
#include"LinkQueue.h"
int main()
{
LinkQueue Q;
Status s;
int i;
QElemType e;
printf("Function 1\n★函数Status InitQueue(LinkQueue *Q)测试...\n");
s=InitQueue(&Q);
printf("▲初始化链队列Q: %d (0:失败 1:成功)\n\n",s);
printf("Function 2\n★函数Status QueueEmpty(LinkQueue Q)测试...\n");
QueueEmpty(Q)?printf("▲链队列Q为空!!!\n\n"):printf("▲链队列Q非空!!!\n\n");
printf("Function 3\n★函数Status EnQueue(LinkQueue *Q,QElemType e)测试...\n");
for(i=1;i<6;i++)
{
printf("▲元素\"%2d\"入队\n",2*i);
EnQueue(&Q,2*i);
printf("▲累计第%d元素\n",QueueLength(Q));
}
printf("\n");
printf("Function 4\n★函数void QueueTraverse(LinkQueue Q,void (*visit)(QElemType e))测试...\n");
printf("▲队列Q中的元素为:Q={");
QueueTraverse(Q,PrintElem);
printf("}\n\n");
printf("Function 5\n★函数Status DeQueue(LinkQueue *Q,QElemType *e)测试...\n");
DeQueue(&Q,&e);
printf("▲队列Q队头元素\"%2d\"出队\n",e);
printf("▲队列Q中的元素为:Q={");
QueueTraverse(Q,PrintElem);
printf("}\n\n");
printf("Function 6\n★函数int QueueLength(LinkQueue Q)测试...\n");
i=QueueLength(Q);
printf("▲队列Q的长度为%d\n\n",i);
printf("Function 7\n★函数Status GetHead(LinkQueue Q,QElemType *e)测试...\n");
GetHead(Q,&e);
printf("▲队列Q队头元素\"%2d\"\n\n",e);
printf("Function 8\n★函数Status ClearQueue(LinkQueue *Q)测试...\n");
printf("▲置空前:");
QueueEmpty(Q)?printf("▲链队列Q为空!!!\n"):printf("▲链队列Q非空!!!\n");
ClearQueue(&Q);
printf("▲置空后:");
QueueEmpty(Q)?printf("▲链队列Q为空!!!\n\n"):printf("▲链队列Q非空!!!\n\n");
printf("Function 9\n★函数Status DestroyQueue(LinkQueue *Q)测试...\n");
printf("▲销毁前:");
Q.front?printf("▲链队列Q存在!!!\n"):printf("▲链队列Q不存在!!!\n");
DestroyQueue(&Q);
printf("▲销毁后:");
Q.front?printf("▲链队列Q存在!!!\n\n"):printf("▲链队列Q不存在!!!\n\n");
return 0;
}
相关文章推荐
- 如何组织构建多文件 C 语言程序(二)
- 如何写好 C main 函数
- C#数据结构之顺序表(SeqList)实例详解
- Lua和C语言的交互详解
- Lua教程(七):数据结构详解
- 解析从源码分析常见的基于Array的数据结构动态扩容机制的详解
- C#数据结构之队列(Quene)实例详解
- C#数据结构揭秘一
- C#数据结构之单链表(LinkList)实例详解
- C#线程队列用法实例分析
- 算法系列15天速成 第九天 队列
- 关于C语言中参数的传值问题
- 简要对比C语言中三个用于退出进程的函数
- 深入C++中API的问题详解
- 基于C语言string函数的详解
- C语言中fchdir()函数和rewinddir()函数的使用详解
- C语言内存对齐实例详解
- C语言编程中统计输入的行数以及单词个数的方法
- C语言自动生成enum值和名字映射代码
- 使用C语言判断英文字符大小写的方法