队列定义
队列实现
循环队列
链队列
注意标红的地方
析构除外
Quene.h
#pragma once
#include"tool.h"
#include<iostream>
using namespace std;
template <class T>
class Quene
{
public:
Error_Type serve(T &output);
Error_Type get_Top(T &output);
Error_Type append(T input);
class Node
{public:
T data;
Node* next;
};
~Quene();
Quene();
bool isempty();
protected:
Node* front;
Node* rear;
};
template<class T>
Quene<T>::Quene()
{
front = new Node;
front->next = NULL;
rear = front;
}
template<class T>
Error_Type
Quene<T>:: append(T input)
{
auto new_node = new Node;
new_node->next = NULL;
new_node->data = input;
rear->next=new_node;
rear = new_node;
return Success;
}
template<class T>
Error_Type
Quene<T>::get_Top(T &output)
{
if (isempty())
{
cout<<"队列已空"<<endl;
return Underflow;
}
output = rear->data;
return Success;
}
template<class T>
bool
Quene<T>::isempty()
{
if (rear == front)return 1;
else return 0;
}
template<class T>
Error_Type
Quene<T>::serve(T &output)
{
if (isempty())
{
cout<<"队列已空"<<endl;
return Underflow;
}
auto nodeptr_tmp = front->next;
output = nodeptr_tmp->data;
front->next = nodeptr_tmp->next;
delete nodeptr_tmp;
if (front->next == NULL)rear = front;//只有一个,移回来
return Success;
}
template<class T>
Quene<T>::~Quene()
{
T data_tmp;
while (!isempty())
{
serve(data_tmp);
}
delete front;
}
队列应用
杨辉三角
Yanghui_Triangle.h
#pragma once
#include"Quene.h"
#include<iomanip>
class Yanghui_Triangle
{
public:
bool execute();
private:
int hight;
};
bool
Yanghui_Triangle::execute()
{
int num_1=0, num_2,num_tmp,j;
cout<<"请输入杨辉三角层数"<<endl;
cin >> hight;
hight--;
Quene<int> quene;
cout << setw((hight+1) * 4) << 1 << endl;;
for (int i = 0; i < hight; i++)
{
cout << setw(hight*4 - i - 1)<<" ";
num_1 = 0;
quene.append(1);
for (j = 0; j <= i; j++)
{
quene.serve(num_2);
printf("%3d", num_1 + num_2);
quene.append(num_1 + num_2);
num_1 = num_2;
}
cout <<" "<< "1";//尾部处理
cout << endl;
}
return 0;
}
源。Cpp
#include"Yanghui_Triangle.h"
#include"Quene.h"
int main()
{
//int n[3] = { 1,2,3 };
//int tmp=0;
Yanghui_Triangle().execute();
Quene<int> quene_test;
//for (auto i : n)
//{
//quene_test.append(i);
//quene_test.serve(tmp);
//}
//quene_test.serve(tmp);
return 0;
}