谁有做爰网站号营业推广经典案例
1> 将之前定义的栈类和队列类都实现成模板类
栈:
#include <iostream>
#define MAX 128
using namespace std;
template<typename T>
class Stack_s
{
private:T *p=new T[MAX];//栈的数组int top;//记录栈顶的变量
public://构造函数Stack_s(int t=-1){top=t;cout<<"无参构造函数"<<endl;}//析构函数~Stack_s(){cout<<"Stack::析构函数"<<endl;}//拷贝构造函数Stack_s(const Stack_s &other):p(new T(p)),top(other.top){cout<<"拷贝构造函数"<<endl;}//入栈int stack_push(T e);//出栈int stack_pop();//清空栈int stack_delete();//判空bool stack_empty();//判满bool stack_full();//获取栈顶元素int stack_gettop();//栈的大小void stack_getsize();void show(int i){cout<<p[i]<<" ";}
};
//入栈
template<typename T>
int Stack_s<T>::stack_push(T e)
{if(stack_full()){cout<<"入栈失败"<<endl;return -1;}top++;p[top]=e;cout<<"入栈成功"<<endl;return 0;
}
//出栈
template<typename T>
int Stack_s<T>::stack_pop()
{if(stack_empty()){cout<<"出栈失败"<<endl;return -1;}T e=p[top];top--;cout<<e<<" 出栈成功"<<endl;return 0;
}
//清空栈
template<typename T>
int Stack_s<T>::stack_delete()
{while(top!=-1){stack_pop();}delete [] p;p=nullptr;cout<<"清空栈成功"<<endl;return 0;
}
//判空
template<typename T>
bool Stack_s<T>::stack_empty()
{if(top==-1){cout<<"栈空"<<endl;return 1;}return 0;
}
//盘满
template<typename T>
bool Stack_s<T>::stack_full()
{if(top==MAX-1){cout<<"栈满了"<<endl;return 1;}return 0;return 0;
}
//获取栈顶元素
template<typename T>
int Stack_s<T>::stack_gettop()
{cout<<"栈顶元素是:"<<p[top]<<endl;return 0;
}
//栈的大小
template<typename T>
void Stack_s<T>::stack_getsize()
{cout<<"栈的大小为:"<<top+1<<endl;
}
int main()
{//实例一个string类型的栈Stack_s<string> s1;string e;int l;s1.stack_empty();cout<<"请输入要入栈的个数:";cin>>l;for(int i=0;i<l;i++){cout<<"请输入要入栈的元素:";cin>>e;s1.stack_push(e);}s1.stack_gettop();s1.stack_getsize();for(int i=0;i<l;i++){s1.show(i);}cout<<endl;s1.stack_delete();//实例一个char类型的栈Stack_s<char> s2;char e1;int l1;s1.stack_empty();cout<<"请输入要入栈的个数:";cin>>l1;for(int i=0;i<l1;i++){cout<<"请输入要入栈的元素:";cin>>e1;s2.stack_push(e1);}s2.stack_gettop();s2.stack_getsize();for(int i=0;i<l1;i++){s2.show(i);}cout<<endl;s2.stack_delete();return 0;
}
结果:
队列:
#include <iostream>
#define MAX 128
using namespace std;
template<typename T>
class Queue_q
{
private:T *p=new T[MAX];//队列的数组int tail;//记录队尾元素int head;//记录对头元素
public://构造函数Queue_q(int t=0){head=t;tail=t;cout<<"无参构造函数"<<endl;}//析构函数~Queue_q(){cout<<"Stack::析构函数"<<endl;}//拷贝构造函数Queue_q(const Queue_q &other):p(new T(p)),tail(other.tail),head(other.head){cout<<"拷贝构造函数"<<endl;}//入队int queue_push(T e);//出队int queue_pop();//清空队列int queue_delete();//判空bool queue_empty();//判满bool queue_full();//队列的大小void queue_getsize();void show(int i){cout<<p[i]<<" ";}
};
//入队
template<typename T>
int Queue_q<T>::queue_push(T e)
{if(queue_full()){cout<<"入队失败"<<endl;return -1;}p[tail]=e;tail=(tail+1)%MAX;cout<<"入队成功"<<endl;return 0;
}
//出队
template<typename T>
int Queue_q<T>::queue_pop()
{if(queue_empty()){cout<<"出队失败"<<endl;return -1;}T e=p[head];head=(head+1)%MAX;cout<<e<<" 出队成功"<<endl;return 0;
}
//清空队列
template<typename T>
int Queue_q<T>::queue_delete()
{while(head!=tail){queue_pop();}delete [] p;p=nullptr;cout<<"清空队列成功"<<endl;return 0;
}
//判空
template<typename T>
bool Queue_q<T>::queue_empty()
{if(head==tail){cout<<"队列空"<<endl;return 1;}return 0;
}
//判满
template<typename T>
bool Queue_q<T>::queue_full()
{if((tail+1)==0){cout<<"队列满了"<<endl;return 1;}return 0;
}
//队列的大小
template<typename T>
void Queue_q<T>::queue_getsize()
{int size;size=(tail-head+MAX)%MAX;cout<<"队的大小为:"<<size<<endl;
}
int main()
{//实例一个double类型的队列Queue_q<double> q1;double e;int s;q1.queue_empty();cout<<"请输入要入队的个数:";cin>>s;for(int i=0;i<s;i++){cout<<"请输入要入队的元素:";cin>>e;q1.queue_push(e);}q1.queue_getsize();for(int i=0;i<s;i++){q1.show(i);}cout<<endl;q1.queue_delete();//实例一个string类型的队列Queue_q<string> q2;string e1;int s2;q2.queue_empty();cout<<"请输入要入队的个数:";cin>>s2;for(int j=0;j<s2;j++){cout<<"请输入要入队的元素:";cin>>e1;q2.queue_push(e1);}q2.queue_getsize();for(int j=0;j<s2;j++){q2.show(j);}cout<<endl;q2.queue_delete();return 0;}
结果:
思维导图: