栈和队列及其应用
"DEC" "OCT" "HEX" "BIN"
typedef int
Status;
typedef unsigned int ElemType;
#endif // !BASE_DEF_H_INCLUDED
2.IO.H
/* FILE_NAME IO.H DATA: 2018.11.6
VERSION: 0.0.1.6 IT_SIZE 16 #define QUEUEINCREMENT 8
第 页,共 页
/*顺序队列存储结构*/ //[协议] //顺序队列首地址块不放置元素,首地址固定,头尾指针移动 //顺序队列已分配容量指可放置元素的地址块总数 typedef struct _SQ_QUEUE_ { QElemType* base; QElemType* front; QElemType* rear; int }Queue; capacity; //队列首地址 //队列头指针 //队尾指针 //当前已分配容量
#endif // QUEUE_H_INCLUDED
4.QUEUE.C
#include"queue.h"
#include<stdio.h> #include<stdlib.h> #include<malloc.h>
第 页,共 页
//初始化队列 Status InitQueue(Queue * q) { if (q) { //额外分配一个空间,首地址块不放数据 q->base = (QElemType*)malloc((QUEUE_INIT_SIZE + 1) * sizeof(QElemType)); if (!q->base) { exit(OVERFLOW); } q->front = q->base; q->rear = q->base; q->capacity = QUEUE_INIT_SIZE; //printf("Queue %X has been created.\n", q); return OK; } else { exit(OVERFLOW); } }
第 页,共 页
(其中:div 为整除运算,mod 为求余运算)
先进后出: 数据生成的顺序:1,1,0,1,0,0,1 读出的顺序:1,0,0,1,0,1,1 二进制数转换成八进制数和十六进制数原理: 三位二进制合为一位八进制数,四位二进制合为一位十六进制数。 队列先进先出。 二进制数转换成八进制数: (1001011)2 = (1,001,011)2=(113)8 二进制数转换成十六进制数: (1001011)2 = (100,1011)2 =(4B)16
#pragma once #ifndef IO_H_INCLUDED #define IO_H_INCLUDED #include"basedef.h" #include<stdio.h> #include<string.h>
第
页,共
页
//[操作]输出一位 base 进制数字(不使用 printf 自带的进制转换) //[条件]base 取值{BIN,OCT,DEC,HEX} bit 在 0-16 之间(左闭右开) Status bitVisit(const ElemType bit, const char* base) { //假设按选择的进制此函数不会发生溢出 if (!strcmp(base, BIN) || !strcmp(base, OCT) || !strcmp(base, DEC)) { printf("%u", bit); return OK; } else if (!strcmp(base, HEX)) { if (bit < 10) { printf("%u", bit); return OK; } else if (bit >= 10 && bit < 16) { //将数值转换为对应的 ASCII 码输出 printf("%c", (char)(bit + 55)); return OK; } else return INFEASIBLE; } return ERROR; }//bitVisit
//[操作]初始化队列 //[条件]传入指针非空 Status InitQueue(Queue * );
//[操作]销毁队列 //[条件]传入指针非空 Status DestroyQueue(Queue * );
//[操作]清空队列 //[条件]传入指针非空 Status ClearQueue(Queue * );
3.QUEUE.H
/* FILE_NAME QUEUE.H DATA: 2018.11.5
VERSION: 0.0.1.3 AUTHOR: */ TOM
#pragma once #ifndef QUEUE_H_INCLUDED #define QUEUE_H_INCLUDED
#include"basedef.h"
第
页,共
页
q->rear = newBase + (q->rear - q->base); q->base = newBase;
q->capacity += QUEUEINCREMENT; } *(++q->rear) = *e; return OK; }
工程代码
/********************************************************/
1.BASEDEF.H
/* FILE_NAME BASEDEF.H DATA: 2018.11.6
VERSION: 0.0.1.4 AUTHOR: */ TOM
#pragma once #ifndef BASE_DEF_H_INCLUDED #define BASE_DEF_H_INCLUDED
//销毁队列 Status DestroyQueue(Queue * q) { if (q) { free(q->base); q->base = NULL; q->front = NULL;
第
页,共
页
q->rear = NULL; q->capacity = 0; //printf("Stack %X has been destroyed.\n", q); return OK; } else return ERROR; }
//获取队首元素
第
页,共
页
Status GetHead(const Queue * q, QElemType * e) { if (QueueEmpty(q) != FALSE || !e) { return ERROR; } *e = *(q->front + 1); return OK; }
//入队 Status EnQueue(Queue * q, const QElemType * e) { QElemType* newBase = NULL; if (!e || QueueEmpty(q) == INFEASIBLE) { return ERROR; }
//清空队列 Status ClearQueue(Queue * q) { //如果队列不空 if (QueueEmpty(q)==FALSE) { //将头指针和尾指针都移至队首基址当做清空 q->front = q->base; q->rear = q->base; return OK; } //空队默认处理成功 else if (QueueEmpty(q) == TRUE) { return OK; } else return INFEASIBLE; }
//获取队列长度 int QueueLength(const Queue * q) { //传入空指针或队列未初始化置队列长为-1 //此时队列不存在 if (!q || q->capacity < 0) { return -1; } else{ return (q->rear - q->front); } }
//[操作]获取键盘输入的一个整数 //[条件]参数非空 Status getElement(ElemType * e)
第
页,共
页
{ if (!e) return ERROR;
printf("请输入一个十进制整数:"); while(scanf("%u", e) != 1) { while (getchar() != '\n'); printf("请重新输入:\n"); } return OK; }//getElement #endif // !IO_H_INCLUDED
//判断队列空 Status QueueEmpty(const Queue * q)
第
页,共
页
{ //队列长度为 0 即队空 if (QueueLength(q) == 0) { return TRUE; } //此时表示队列不存在 else if (QueueLength(q) == -1) { return INFEASIBLE; } else { return FALSE; } }
//若队列已满,重新分配空间 if (q->rear - q->base >= q->capacity) { newBase = (QElemType*)realloc(q->base, (q->capacity + 1 + QUEUEINCREMENT) * sizeof(QElemType)); //若再次分配失败 if (!newBase) { //销毁原队列,防止内存泄露 DestroyQueue(q); exit(OVERFLOW); } //更新队列 q->front = newBase + (q->front - q->base);