一、顺序队列的实现(LinearQueue.h)
#pragma once
#include<iostream>
using namespace std;
template<class T>
class LinearQueue
{
public:
LinearQueue(int LQMaxSize)//创建空队列
{
MaxSize = LQMaxSize;
element = new T[MaxSize];
size = 0;
front = 0;
rear = 0;
}
~LinearQueue()//删除队列
{
delete[]element;
}
bool IsEmpty()//判断队列是否为空,空返回true,非空返回false
{
return size == 0;
}
bool IsFull()//判断队列是否为满,满返回true,不满返回false
{
return size == MaxSize;
}
bool Insert(const T& x)//入队,在队列尾部插入元素x
{
if (IsFull())
return false;
else
{
element[rear] = x;
rear = (rear + 1) % (MaxSize);
size++;
return true;
}
}
bool GetElement(T& x)//求队头元素的值放入x中
{
if (IsEmpty())
return false;
else
{
x = element[front];
return true;
}
}
bool Delete(T& x)//出队,从队头删除一个元素,并将该元素的值放入x中
{
if (IsEmpty())
return false;
else
{
x = element[front];
front = (front + 1) % (MaxSize);
size--;
return true;
}
}
void OutPut(ostream& out)const//输出队列
{
int index;
index = front;
for (int i = 0; i < size; i++)
{
out << element[index] << endl;
index = (index + 1) % MaxSize;
}
}
private:
int size;//队列实际元素个数
int MaxSize;//队列中最大元素个数
int front, rear;//队列的队头和队尾指针
T *element;//一维动态数组
};
//重载插入运算符<<
template<class T>
ostream& operator<<(ostream& out, const LinearQueue<T>& x)
{
x.OutPut(out);
return out;
}
二、链接队列的实现(LinkQueue.h)
#pragma once
#include<iostream>
using namespace std;
template<class T>
class LinkNode//节点类
{
template<class T>
friend class LinkQueue;//将链接队列声明为友类
public:
LinkNode()//构造函数
{
next = NULL;
}
private:
T data;//节点元素
LinkNode<T> *next;//指向下一个节点的指针
};
template<class T>
class LinkQueue
{
public:
LinkQueue()//创建空队列
{
front = NULL;
rear = NULL;
size = 0;
}
~LinkQueue()//删除队列
{
T x;
while (front != NULL)//队列非空则元素依次出队
Delete(x);
}
bool IsEmpty()//判断队列是否为空
{
return size == 0;
}
bool Insert(const T&x)//入队,在队列尾插入元素x
{
LinkNode<T> *p = new LinkNode;
if (p == NULL)
return false;
else
{
p->data = x;
if (front == NULL)
{
rear = p;
front = p;
}
else
{
rear->next = p;//插入新结点
rear = p;//指向新队尾
}
size++;
return true;
}
}
bool GetElement(T& x)//求队头元素的值放入x中
{
if (IsEmpty())
return false;
else
{
x = front->data;
return true;
}
}
bool Delete(T& x)//出队,从队头删除一个元素,并将该元素的值放入x中
{
LinkNode<T> *p;
if (IsEmpty())
return false;
else
{
p = front;
x = front->data;
front = front->next;
delete p;//删除头节点
size--;
return true;
}
}
viod OutPut(ostream& out)const//输出队列
{
LinkNode<T> *p;
p = front;
for (int i = 0; i < size; i++)
{
out << p->data << end;
p = p->next;
}
}
private:
int size;//队列实际元素个数
LinkNode<T> *front, *rear;//队列的队头和队尾指针
};
//重载插入运算符<<
template<class T>
ostream& operator<<(ostream& out, const LinkQueue<T>& x)
{
x.OutPut(out);
return out;
}
三、杨辉三角
#include<iostream>
using namespace std;
#include"LinearQueue.h"
/*输出第n行的空格,参数n为要输出的杨辉三角的行数,k为当前输出行数*/
void PrintSpace(int n, int k)
{
for (int i = 1; i <= n - k;i++)
{
cout << " ";
}
}
//输出杨辉三角的前n行(n>0)
void YangHui(int n)
{
LinearQueue<int>Q(n + 2);
int x, y;
PrintSpace(n, 1);//输出第一行前面的空格
cout << "1" << endl;//输出第一行的1
Q.Insert(0);//添加行开始标识
Q.Insert(1);//第二行入队
Q.Insert(1);//第二行入队
for (int i = 2; i <= n; i++)
{
Q.Insert(0);//添加行结束标识
PrintSpace(n, i);//输出第i行数字前面的空格
do
{
Q.Delete(x);
Q.GetElement(y);
if (y)
cout << y << " ";
else
cout << endl;
Q.Insert(x + y);
} while (y);
}
cout << endl;
}
int main()
{
int n;
cout << "请输入要显示的杨辉三角的行数:" << endl;
cin >> n;
YangHui(n);
return 0;
}