思维导图
用模板实现栈
#include <iostream>
using namespace std;
template <typename T, int MaxSize>
class Stack {
public:
//构造函数
Stack() : top(-1) {}
//入栈
void push(const T& value);
//出栈
T pop();
//判空
bool empty() const;
//判满
bool full()const;
//清空栈
bool clear();
//获取栈顶元素
T stacktop();
//求栈的大小
int stacksize();
//输出栈元素
bool show();
private:
T data[MaxSize];
int top;
};
int main() {
Stack<int, 3> intStack;
intStack.push(1);
intStack.push(2);
intStack.push(3);
intStack.pop();
intStack.show();
Stack<double, 5> doubleStack;
doubleStack.push(1.1);
doubleStack.push(2.2);
doubleStack.push(3.3);
doubleStack.pop();
doubleStack.show();
return 0;
}
//入栈
template <typename T, int MaxSize>
void Stack<T, MaxSize>::push(const T& value) {
if (top == MaxSize - 1) {
throw std::overflow_error("该栈已满");
}
data[++top] = value;
}
//出栈
template <typename T, int MaxSize>
T Stack<T, MaxSize>::pop() {
if (empty()) {
throw std::underflow_error("该栈为空");
}
return data[top--];
}
//判空
template <typename T, int MaxSize>
bool Stack<T, MaxSize>::empty() const {
return top == -1;
}
//判满
template <typename T, int MaxSize>
bool Stack<T, MaxSize>::full()const
{
return top = MaxSize-1;
}
//清空栈
template <typename T, int MaxSize>
bool Stack<T, MaxSize>::clear()
{
if(empty()){
cout<<"该栈为空"<<endl;
return false;
}else {
top = -1;
return true;
}
}
//获取栈顶元素
template <typename T, int MaxSize>
T Stack<T, MaxSize>::stacktop()
{
return data[top];
}
//求栈的大小
template <typename T, int MaxSize>
int Stack<T, MaxSize>::stacksize()
{
return top+1;
}
//输出栈元素
template <typename T, int MaxSize>
bool Stack<T, MaxSize>::show()
{
if(empty()){
cout<<"该栈为空"<<endl;
return false;
}else {
for(int i=0;i<=top; i++){
cout<<data[i]<<" ";
}
cout<<endl;
return true;
}
}
用模板实现队列
#include <iostream>
#define MAXSIZE 16
using namespace std;
template <typename T, int MaxSize>
class LoopQueue
{
public:
//构造函数
LoopQueue():FrontIndex(0), RearIndex(0){};
//入队列
bool push(T value);
//出队列并返回队列顶元素
T pop();
//清空队列
bool clear();
//判空
bool isEmpty();
//判满
bool isFull();
//求队列的大小
int LoopQueuesize();
//输出队列元素
bool show();
private:
T Data[MaxSize];
int FrontIndex;
int RearIndex;
};
int main()
{
LoopQueue<int, 5> intL;
intL.push(5);
intL.push(7);
intL.push(10);
intL.pop();
intL.show();
LoopQueue<double, 5> doubleL;
doubleL.push(5.5);
doubleL.push(7.4);
doubleL.push(10.3);
doubleL.pop();
doubleL.show();
return 0;
}
//入队列
template <typename T, int MaxSize>
bool LoopQueue<T, MaxSize>::push(T value)
{
if(isFull()){
cout<<"该队列已满"<<endl;
return false;
}else{
Data[RearIndex] = value;
RearIndex++;
return true;
}
}
//出队列并返回队列顶元素
template <typename T, int MaxSize>
T LoopQueue<T, MaxSize>::pop()
{
if(isEmpty()){
throw std::underflow_error("该队列为空");
}else {
int Frontvalue = Data[RearIndex];
FrontIndex++;
return Frontvalue;
}
}
//清空队列
template <typename T, int MaxSize>
bool LoopQueue<T, MaxSize>::clear()
{
if(isEmpty()){
cout<<"该队列为空"<<endl;
return false;
}else {
FrontIndex = -1;
RearIndex = -1;
return true;
}
}
//判空
template <typename T, int MaxSize>
bool LoopQueue<T, MaxSize>::isEmpty()
{
return FrontIndex==RearIndex;
}
//判满
template <typename T, int MaxSize>
bool LoopQueue<T, MaxSize>::isFull()
{
return (RearIndex+1)%MAXSIZE==FrontIndex;
}
//求队列的大小
template <typename T, int MaxSize>
int LoopQueue<T, MaxSize>::LoopQueuesize()
{
return (RearIndex-FrontIndex+MAXSIZE)%MAXSIZE;
}
//输出队列元素
template <typename T, int MaxSize>
bool LoopQueue<T, MaxSize>::show()
{
if(isEmpty()){
cout<<"该队列为空"<<endl;
return false;
}else {
int i=FrontIndex;
cout<<"该队列为>>";
while(1)
{
cout<<Data[i]<<" ";
i++;
if(MAXSIZE==i)
{
i=0;
}
if(i==RearIndex)
{
break;
}
}
cout<<endl;
return true;
}
}