队列的链式表示和实现----单链队列
2016-04-01 15:24
330 查看
头文件 head.h
#include<string.h> #include<ctype.h> #include<malloc.h> /* malloc()等 */ #include<limits.h> /* INT_MAX等 */ #include<stdio.h> /* EOF(=^Z或F6),NULL */ #include<stdlib.h> /* atoi() */ #include<io.h> /* eof() */ #include<math.h> /* floor(),ceil(),abs() */ #include<process.h> /* exit() */ /* 函数结果状态代码 */ #define TRUE 1 #define FALSE 0 #define OK 1 #define ERROR 0 #define INFEASIBLE -1 /* #define OVERFLOW -2 因为在math.h中已定义OVERFLOW的值为3,故去掉此行 */ typedef int Status; /* Status是函数的类型,其值是函数结果状态代码,如OK等 */ typedef int Boolean; /* Boolean是布尔类型,其值是TRUE或FALSE */ 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 DestoryQueue(LinkQueue *Q); Status ClearQueue(LinkQueue *Q); Boolean QueueEmtpy(LinkQueue Q); int QueueLength(LinkQueue Q); Status GetHead(LinkQueue Q, QElemType *e); Status EnQueue(LinkQueue *Q, QElemType e); Status DeQueue(LinkQueue *Q, QElemType *e); Status QueueTraverse(LinkQueue Q, Status (*visit)(QElemType e));
算法实现
#include"head.h" Status InitQueue(LinkQueue *Q) { //构造一个空队列Q (*Q).front = (QueuePtr)malloc(sizeof(QNode)); if (!(*Q).front) //判断存储空间非配是否成功 { printf("初始化失败!"); system("pause"); exit(-1); } (*Q).rear = (*Q).front; (*Q).front->next = NULL; //空队列 return OK; } Status DestoryQueue(LinkQueue *Q) { //销毁队列Q while ((*Q).front) { (*Q).rear = (*Q).front->next; free((*Q).front); (*Q).front = (*Q).rear; } return OK; } Status ClearQueue(LinkQueue *Q) { DestoryQueue(Q); InitQueue(Q); return OK; } Boolean QueueEmtpy(LinkQueue Q) { if (Q.front == Q.rear) return TRUE; else return FALSE; } int QueueLength(LinkQueue Q) { int n = 0; while (Q.front != Q.rear) { Q.front = Q.front->next; n++; } return n; } Status GetHead(LinkQueue Q, QElemType *e) { //若队列不为空,则用e返回Q的队头元素,并返回OK;否则,返回FALSE; if (QueueEmtpy(Q)) return FALSE; else { *e = Q.front->next->data; return OK; } } Status EnQueue(LinkQueue *Q, QElemType e) { //插入元素e为Q的新的队尾元素 QueuePtr p = (QueuePtr)malloc(sizeof(QNode)); if (!p) { printf("插入元素失败!"); system("pause"); exit(-2); } p->data = e; p->next = NULL; (*Q).rear->next = p; (*Q).rear = p; return OK; } Status DeQueue(LinkQueue *Q, QElemType *e) { //若队列不为空,则删除Q的队头元素,用e返回其值,并返回OK; //否则,返回ERROR; if (QueueEmtpy(*Q)) return ERROR; else { QueuePtr p = (*Q).front->next; //被删除的元素 (*Q).front->next = p->next; *e = p->data; if ((*Q).rear == p) //如果队列中只有一个元素,应该处理一下队尾元素 (*Q).rear = (*Q).front; free(p); return OK; } } Status QueueTraverse(LinkQueue Q, Status(*visit)(QElemType e)) { if (QueueEmtpy(Q)) { printf("队列为空!"); return ERROR; } else { QueuePtr p = Q.front->next; //第一个元素 while (p) { visit(p->data); p = p->next; } } }
测试文件:
#include"head.h" Status visit(QElemType e) { printf("%d ", e); return OK; } void main() { int i; QElemType d; LinkQueue q; i = InitQueue(&q); if (i) printf("成功地构造了一个空队列!\n"); printf("是否空队列?%d(1:空 0:否) ", QueueEmtpy(q)); printf("队列的长度为%d\n", QueueLength(q)); EnQueue(&q, -5); EnQueue(&q, 5); EnQueue(&q, 10); printf("插入3个元素(-5,5,10)后,队列的长度为%d\n", QueueLength(q)); printf("是否空队列?%d(1:空 0:否) ", QueueEmtpy(q)); printf("队列的元素依次为:"); QueueTraverse(q, &visit); i = GetHead(q, &d); if (i == OK) printf("队头元素是:%d\n", d); DeQueue(&q, &d); printf("删除了队头元素%d\n", d); i = GetHead(q, &d); if (i == OK) printf("新的队头元素是:%d\n", d); ClearQueue(&q); printf("清空队列后,q.front=%u q.rear=%u q.front->next=%u\n", q.front, q.rear, q.front->next); DestoryQueue(&q); printf("销毁队列后,q.front=%u q.rear=%u\n", q.front, q.rear); system("pause"); }
Running Result:
成功地构造了一个空队列! 是否空队列?1(1:空 0:否) 队列的长度为0 插入3个元素(-5,5,10)后,队列的长度为3 是否空队列?0(1:空 0:否) 队列的元素依次为:-5 5 10 队头元素是:-5 删除了队头元素-5 新的队头元素是:5 清空队列后,q.front=5014240 q.rear=5014240 q.front->next=0 销毁队列后,q.front=0 q.rear=0 请按任意键继续. . .
相关文章推荐
- webstorm快捷键
- UIDynamicAnimator简述
- C和指针详解------数组
- 排序算法之鸡尾酒排序
- MIFARE系列8《D8M1.exe》
- Python中用format函数格式化字符串的用法
- 科普:操作系统32位和64位
- Android中列表实体类排序
- Redis3.x集群的搭建
- 一个页面多个bootstrip轮播以及一个页面多个swiper轮播 冲突问题
- 【Android】笔记本
- 常见问题解析
- 刚接触cef,这里是一篇挺好的教程
- Git远程操作详解
- android studio显示设置(外观和字体)
- Andriod中Style/Theme原理以及Activity界面文件选取过程浅析
- 手机卫士 第九天
- 简单的JSP交互程序
- petalinux-build命令
- Cocos2dx引擎9-注册事件监听器