目的:领会环形队列存储结构和掌握环形队列中的各种基本运算算法设计。
内容:编写一个程序,实现环形队列(假设栈中元素类型ElemType 为char)的各种基本运算,并在此基础上完成以下功能
(1)初始化队列q;
(2)判断队列q是否非空;
(3)依次进队元素a、b、c;
(4)出队一个元素,输出该元素;
(5)依次进队元素d、e、f;
(6)输出出队序列;
(7)释放队列。
另外增加两个函数,同时将MAXSIZE=40
1. 函数1:利用队能将一个十进制小数化为二进制小数。测试数据:0.635(最多保留8位)
2. 函数2:利用队列能将一个十进制小数化为36进制以内的任意进制小数(十个数字加26个大写字母作为基数),小数和转化的进制由用户输入。
测试数据: 0.1234567转化为29进制的数是多少?
=================================================
1.定义队列的数据结构
#include <iostream>
#include <cassert>
using namespace std;
#define MAXSIZE 40
typedef char Elemtype;
struct SqQueue
{
Elemtype data[MAXSIZE];
int front, rear;
};
2.队列初始化
void InitSqQueue(SqQueue*& q)
{
q = new SqQueue;
q->front = q->rear = 0;
}
3.销毁队列
void DestroySqQueue(SqQueue*& q)
{
delete q;
}
4.判断队列是否为空
队列为空队的条件: L->front == L->rear;
bool QueueEmpty(SqQueue* q)
{
return q->front == q->rear;
}
5.进队
队列为满队的条件: (L->rear + 1) % MAXSIZE == L->front
因为该队列为环形队列,故需要利用 MAXSIZE 进行取余,否则尾指针将会溢出。
bool enQueue(SqQueue*& q, Elemtype e)
{
if ((q->rear + 1) % MAXSIZE == q->front)
return false;
q->rear = (q->rear + 1) % MAXSIZE;
q->data[q->rear] = e;
return true;
}
6.出队
bool deQueue(SqQueue*& q, Elemtype& e)
{
if (QueueEmpty(L))
return false;
q->front = (q->front + 1) % MAXSIZE;
e = q->data[q->front];
return true;
}
因为该队列为环形队列,故需要利用 MAXSIZE 进行取余,否则头指针将会溢出。
7.函数1:十进制小数化为二进制小数
当 m * 2 > 1 时,进队 1 ,并且 m 取小数部分;
当 m * 2 < 1 时,进队 0,m 不变;
当 m * 2 == 1 时,终止操作。
void change1(SqQueue*& q, double m,int n)//m为测试数据,n为保留的位数
{
for (int i = 1; i <= n; i++)
{
m = m * 2;
if (m > 1)
{
if (!enQueue(q, 1))
cout << "插入失败";
m = m - 1;
}
else if (m < 1)
{
if (!enQueue(q, 0))
cout << "插入失败";
}
else if (m == 1)
{
if (!enQueue(q, 1))
cout << "插入失败";
break;
}
}
}
8.函数2:十进制小数化为三十六进制以内的任意进制小数
当 m > 10时,需要进队字母,由于ElemType 为 char,故根据字母的ASCII码为基准进行转换。
当 m < 10时,需要进队数字(字符),由于ElemType 为 char,故根据数字(字符)的ASCII码为基准进行转换。
void change2(SqQueue*& q,double m, double n)//m为测试数据,n为转换的进制
{
for (int i = 1; i <= 10;i++)//保留十位小数;
{
int a;
m = m * n;
if (m > 10)
{
a = (int)m + 55;//a的最小值为65,A的ASCII码为65;
if (!enQueue(q,a))
cout << "插入失败";
m = m - (int)m;
}
else
{
a=(int)m + 48;//a的最小值为0,'0'的ASCII码为48;
if (!enQueue(q,a))
cout << "插入失败";
m = m - (int)m;
}
}
}
=================================================
主函数
int main()
{
SqQueue *Q;
ElemType e;
ElemType a[MAXSIZE] = { 'a','b','c' };
ElemType b[MAXSIZE] = { 'd','e','f' };
cout << "1.初始化队列q" << endl;
InitQueue(Q);
cout << "2.判断队列q是否非空" << endl;
if (!QueueEmpty(Q))
cout << "队列q不为空" << endl;
else
cout << "队列q为空" << endl;
cout << "3.依次进队元素a、b、c" << endl;
for (int i = 0; i < 3; i++)
if (!enQueue(Q,a[i]))
cout << "插入失败" << endl;
cout << "4.出队一个元素,输出该元素" << endl;
if (deQueue(Q, e))
cout << "出队的元素为" << (char)e << endl;
cout << "5.依次进队元素d、e、f" << endl;
for (int i = 0; i < 3; i++)
if (!enQueue(Q, b[i]))
cout << "插入失败" << endl;
cout << "6.输出出队序列" << endl;
while (!QueueEmpty(Q))
{
if (deQueue(Q, e))
cout << (char)e << '\t';
}
cout << endl;
cout << "7.释放队列" << endl;
DestroyQueue(Q);
cout << "函数1的实现" << endl;
SqQueue* L1;
InitQueue(L1);
change1(L1, 0.635, 8);
cout << "0.";
while (!QueueEmpty(L1))
{
if (deQueue(L1, e))
cout << e;
}
DestroyQueue(L1);
cout << endl;
cout << "函数2的实现" << endl;
SqQueue* L2;
InitQueue(L2);
cout << "请输入测试数据:"; double k; cin >> k;
cout << "请输入转换的进制:"; double j; cin >> j;
change2(L2,k,j);
cout << "0.";
while (!QueueEmpty(L2))
{
if (deQueue(L2, e))
cout << (char)e;
}
DestroyQueue(L2);
}