数据结构单链队列——链式存储实现
来源:互联网 发布:超图软件股票怎么样 编辑:程序博客网 时间:2024/05/29 10:49
数据结构作业之四,保留做模板
#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()#include<iostream> // cout,cin// 函数结果状态代码#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或FALSEtypedef int QElemType;//单链队列--队列的链式存储结构typedef struct QNode{ QElemType data; QNode *next;}*QueuePtr;struct LinkQueue{ QueuePtr front,rear; // 队头、队尾指针 int length;};//链队列的基本操作(9个)Status InitQueue(LinkQueue &Q){ /// 构造一个空队列Q if(!(Q.front=Q.rear=(QueuePtr)malloc(sizeof(QNode)))) exit(OVERFLOW); Q.front->next=NULL; Q.length=0; return OK;}Status DestroyQueue(LinkQueue &Q){ /// 销毁队列Q(无论空否均可) Q.length=0; while(Q.front) { Q.rear=Q.front->next; free(Q.front); Q.front=Q.rear; } return OK;}Status ClearQueue(LinkQueue &Q){ /// 将Q清为空队列 Q.front->next=NULL; Q.rear=Q.front->next; Q.length=0; return OK;}Status QueueEmpty(LinkQueue Q){ /// 若Q为空队列,则返回TRUE,否则返回FALSE if(Q.front==Q.rear)return TRUE; else return FALSE;}int QueueLength(LinkQueue Q){ /// 求队列的长度 return Q.length;}Status GetHead(LinkQueue Q,QElemType &e){ /// 若队列不空,则用e返回Q的队头元素,并返回OK,否则返回ERROR QueuePtr p; if(Q.front==Q.rear) return ERROR; p=Q.front->next; e=p->data; return OK;}Status EnQueue(LinkQueue &Q,QElemType e){ /// 插入元素e为Q的新的队尾元素 QueuePtr p; if(!(p=(QueuePtr)malloc(sizeof(QNode)))) // 存储分配失败 exit(OVERFLOW); p->data=e; p->next=NULL; Q.rear->next=p; Q.rear=p; Q.length++; return OK;}Status DeQueue(LinkQueue &Q,QElemType &e){ /// 若队列不空,删除Q的队头元素,用e返回其值,并返回OK,否则返回ERROR QueuePtr p; 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); Q.length--; return OK;}Status QueueTraverse(LinkQueue Q,void(*vi)(QElemType)){ /// 从队头到队尾依次对队列Q中每个元素调用函数vi()。一旦vi失败,则操作失败 QueuePtr p; p=Q.front->next; while(p) { vi(p->data); p=p->next; } printf("\n"); return OK;}void visit(QElemType i){ printf("%d ",i);}int main(){ int i; QElemType d; LinkQueue q; i=InitQueue(q); if(i) printf("成功地构造了一个空队列!\n"); printf("是否空队列?%d(1:空 0:否) ",QueueEmpty(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:否) ",QueueEmpty(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); DestroyQueue(q); printf("销毁队列后,q.front=%u q.rear=%u\n",q.front, q.rear);}
阅读全文
0 0
- 数据结构单链队列——链式存储实现
- 数据结构:单链队列--队列的链式存储结构
- 单链队列——队列的链式存储结构
- 数据结构——队列的链式存储结构以及实现
- 单链队列,队列的链式存储结构C++实现
- 数据结构——队列之链式存储
- 数据结构---队列---链式存储
- 数据结构之线性表——队列的链式存储
- 数据结构六—队列的链式存储结构
- 数据结构—队列的顺序和链式存储
- 链队列——链式存储结构
- 数据结构——链式队列
- 队列链式存储实现
- 队列——链式存储
- 数据结构——队列的链式实现(C语言)
- 数据结构——链式队列模板类实现
- 数据结构学习之队列的链式存储的C++实现
- 数据结构—链表-链式存储
- 通过已有的虚拟机克隆四台虚拟机
- word2vec原理推导与代码分析
- quartz定时执行任务,并配置web.xml的操作方法
- NOIP2017
- storm中的BaseRichSpout
- 数据结构单链队列——链式存储实现
- Android androidannotations注解框架详解
- 两种不同方式解决八皇后问题
- 【USACO题库】1.1.4 Broken Necklace破碎的项链
- 1. 河内之塔
- 深度学习资源
- Python爬虫浅述
- template新特性:可变参数模板,(传入参数个数未知)
- Oozie自带MapReduce示例的运行